add network for support unix socket domain

This commit is contained in:
siddontang 2014-03-05 16:47:54 +08:00
parent 209f47d5ec
commit a46d1de902
4 changed files with 15 additions and 11 deletions

View File

@ -10,6 +10,7 @@ import (
type Client struct { type Client struct {
sync.Mutex sync.Mutex
network string
addr string addr string
maxIdleConns int maxIdleConns int
@ -17,10 +18,11 @@ type Client struct {
conns *list.List conns *list.List
} }
func NewClient(addr string, maxIdleConns int) *Client { func NewClient(network, addr string, maxIdleConns int) *Client {
RegisterType(RpcError{}) RegisterType(RpcError{})
c := new(Client) c := new(Client)
c.network = network
c.addr = addr c.addr = addr
c.maxIdleConns = maxIdleConns c.maxIdleConns = maxIdleConns
@ -162,7 +164,7 @@ func (c *Client) popConn() (*conn, error) {
return v.Value.(*conn), nil return v.Value.(*conn), nil
} }
c.Unlock() c.Unlock()
return newConn(c.addr) return newConn(c.network, c.addr)
} }
func (c *Client) pushConn(co *conn) error { func (c *Client) pushConn(co *conn) error {

View File

@ -11,8 +11,8 @@ type conn struct {
co net.Conn co net.Conn
} }
func newConn(addr string) (*conn, error) { func newConn(network, addr string) (*conn, error) {
c, err := net.Dial("tcp", addr) c, err := net.Dial(network, addr)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -14,7 +14,7 @@ var testClient *Client
func newTestServer() *Server { func newTestServer() *Server {
f := func() { f := func() {
testServer = NewServer("127.0.0.1:11182") testServer = NewServer("tcp", "127.0.0.1:11182")
go testServer.Start() go testServer.Start()
} }
@ -25,7 +25,7 @@ func newTestServer() *Server {
func newTestClient() *Client { func newTestClient() *Client {
f := func() { f := func() {
testClient = NewClient("127.0.0.1:11182", 10) testClient = NewClient("tcp", "127.0.0.1:11182", 10)
} }
testClientOnce.Do(f) testClientOnce.Do(f)

View File

@ -10,6 +10,7 @@ import (
type Server struct { type Server struct {
sync.Mutex sync.Mutex
network string
addr string addr string
funcs map[string]reflect.Value funcs map[string]reflect.Value
@ -17,10 +18,11 @@ type Server struct {
running bool running bool
} }
func NewServer(addr string) *Server { func NewServer(network, addr string) *Server {
RegisterType(RpcError{}) RegisterType(RpcError{})
s := new(Server) s := new(Server)
s.network = network
s.addr = addr s.addr = addr
s.funcs = make(map[string]reflect.Value) s.funcs = make(map[string]reflect.Value)
@ -30,7 +32,7 @@ func NewServer(addr string) *Server {
func (s *Server) Start() error { func (s *Server) Start() error {
var err error var err error
s.listener, err = net.Listen("tcp", s.addr) s.listener, err = net.Listen(s.network, s.addr)
if err != nil { if err != nil {
return err return err
} }