Skip to content

Commit

Permalink
chore: refine code
Browse files Browse the repository at this point in the history
  • Loading branch information
panjf2000 committed Mar 23, 2024
1 parent 53192ef commit 9cf2f2d
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
6 changes: 3 additions & 3 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ func (s *testClientServer) OnTraffic(c Conn) (action Action) {
require.Equal(s.tester, string(pingMsg), string(ping), "bad header")
}
v := c.Context()
if v != nil && s.network != "udp" {
if v != nil {
v.(*sync.Once).Do(readHeader)
}

Expand All @@ -275,7 +275,7 @@ func (s *testClientServer) OnTraffic(c Conn) (action Action) {
_ = c.OutboundBuffered()
_, _ = c.Discard(1)
}
if s.network == "udp" && bytes.Equal(buf.Bytes(), pingMsg) {
if v == nil && bytes.Equal(buf.Bytes(), pingMsg) {
atomic.AddInt32(&s.udpReadHeader, 1)
buf.Reset()
}
Expand All @@ -290,7 +290,7 @@ func (s *testClientServer) OnTraffic(c Conn) (action Action) {
}

buf, _ := c.Next(-1)
if s.network == "udp" && bytes.Equal(buf, pingMsg) {
if v == nil && bytes.Equal(buf, pingMsg) {
atomic.AddInt32(&s.udpReadHeader, 1)
buf = nil
}
Expand Down
5 changes: 3 additions & 2 deletions client_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func (cli *Client) EnrollContext(c net.Conn, ctx interface{}) (Conn, error) {

var (
sockAddr unix.Sockaddr
gc Conn
gc *conn
)
switch c.(type) {
case *net.UnixConn:
Expand Down Expand Up @@ -227,7 +227,7 @@ func (cli *Client) EnrollContext(c net.Conn, ctx interface{}) (Conn, error) {
default:
return nil, errorx.ErrUnsupportedProtocol
}
gc.SetContext(ctx)
gc.ctx = ctx

connOpened := make(chan struct{})
ccb := &connWithCallback{c: gc, cb: func() {
Expand All @@ -238,6 +238,7 @@ func (cli *Client) EnrollContext(c net.Conn, ctx interface{}) (Conn, error) {
gc.Close()
return nil, err
}

<-connOpened
return gc, nil
}
4 changes: 2 additions & 2 deletions eventloop_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ func (el *eventloop) closeConns() {
}

type connWithCallback struct {
c Conn
c *conn
cb func()
}

func (el *eventloop) register(itf interface{}) error {
c, ok := itf.(*conn)
if !ok {
ccb := itf.(*connWithCallback)
c = ccb.c.(*conn)
c = ccb.c
defer ccb.cb()
}

Expand Down

0 comments on commit 9cf2f2d

Please sign in to comment.