Skip to content

Commit

Permalink
chore: add some comments on DialContext
Browse files Browse the repository at this point in the history
  • Loading branch information
andyl committed Mar 14, 2024
1 parent a0050b4 commit 5e4d33f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
6 changes: 3 additions & 3 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,15 +333,15 @@ func startGnetClient(t *testing.T, cli *Client, ev *clientEvents, network, addr
var netConn net.Conn
netConn, err = NetDial(network, addr)
require.NoError(t, err)
c, err = cli.EnrollWithContext(netConn, handler)
c, err = cli.EnrollContext(netConn, handler)
} else {
c, err = cli.DialWithContext(network, addr, handler)
c, err = cli.DialContext(network, addr, handler)
}
require.NoError(t, err)
defer c.Close()
err = c.Wake(nil)
require.NoError(t, err)
var rspCh = handler.rspCh
rspCh := handler.rspCh
duration := time.Duration((rand.Float64()*2+1)*float64(time.Second)) / 2
t.Logf("test duration: %dms", duration/time.Millisecond)
start := time.Now()
Expand Down
12 changes: 7 additions & 5 deletions client_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,23 +140,25 @@ func (cli *Client) Stop() (err error) {

// Dial is like net.Dial().
func (cli *Client) Dial(network, address string) (Conn, error) {
return cli.DialWithContext(network, address, nil)
return cli.DialContext(network, address, nil)
}

func (cli *Client) DialWithContext(network, address string, ctx interface{}) (Conn, error) {
// DialContext is like Dial but also accepts an empty interface ctx that can be obtained later via Conn.Context.
func (cli *Client) DialContext(network, address string, ctx interface{}) (Conn, error) {
c, err := net.Dial(network, address)
if err != nil {
return nil, err
}
return cli.EnrollWithContext(c, ctx)
return cli.EnrollContext(c, ctx)
}

// Enroll converts a net.Conn to gnet.Conn and then adds it into Client.
func (cli *Client) Enroll(c net.Conn) (Conn, error) {
return cli.EnrollWithContext(c, nil)
return cli.EnrollContext(c, nil)
}

func (cli *Client) EnrollWithContext(c net.Conn, ctx interface{}) (Conn, error) {
// EnrollContext is like Enroll but also accepts an empty interface ctx that can be obtained later via Conn.Context.
func (cli *Client) EnrollContext(c net.Conn, ctx interface{}) (Conn, error) {
defer c.Close()

sc, ok := c.(syscall.Conn)
Expand Down
10 changes: 5 additions & 5 deletions client_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,10 @@ func unixAddr(addr string) string {
}

func (cli *Client) Dial(network, addr string) (Conn, error) {
return cli.DialWithContext(network, addr, nil)
return cli.DialContext(network, addr, nil)
}

func (cli *Client) DialWithContext(network, addr string, ctx interface{}) (Conn, error) {
func (cli *Client) DialContext(network, addr string, ctx interface{}) (Conn, error) {
var (
c net.Conn
err error
Expand All @@ -139,14 +139,14 @@ func (cli *Client) DialWithContext(network, addr string, ctx interface{}) (Conn,
return nil, err
}
}
return cli.EnrollWithContext(c, ctx)
return cli.EnrollContext(c, ctx)
}

func (cli *Client) Enroll(nc net.Conn) (gc Conn, err error) {
return cli.EnrollWithContext(nc, nil)
return cli.EnrollContext(nc, nil)
}

func (cli *Client) EnrollWithContext(nc net.Conn, ctx interface{}) (gc Conn, err error) {
func (cli *Client) EnrollContext(nc net.Conn, ctx interface{}) (gc Conn, err error) {
switch v := nc.(type) {
case *net.TCPConn:
if cli.opts.TCPNoDelay == TCPNoDelay {
Expand Down

0 comments on commit 5e4d33f

Please sign in to comment.