fix: OnAccept panic (#514)

This commit is contained in:
Xuran 2023-01-09 13:54:30 +08:00 committed by GitHub
parent 6e1b64174e
commit e0f24cea30
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 20 additions and 6 deletions

View File

@ -803,7 +803,7 @@ func TestOnprepare(t *testing.T) {
assert.DeepEqual(t, "the server closed connection before returning the first response byte. Make sure the server returns 'Connection: close' response header before closing the connection", err.Error()) assert.DeepEqual(t, "the server closed connection before returning the first response byte. Make sure the server returns 'Connection: close' response header before closing the connection", err.Error())
h = New( h = New(
WithOnAccept(func(conn network.Conn) context.Context { WithOnAccept(func(conn net.Conn) context.Context {
conn.Close() conn.Close()
return context.Background() return context.Background()
}), }),
@ -817,4 +817,18 @@ func TestOnprepare(t *testing.T) {
if err == nil { if err == nil {
t.Fatalf("err should not be nil") t.Fatalf("err should not be nil")
} }
h = New(
WithOnAccept(func(conn net.Conn) context.Context {
assert.DeepEqual(t, conn.LocalAddr().String(), "127.0.0.1:9231")
return context.Background()
}),
WithHostPorts("localhost:9231"),
WithTransport(standard.NewTransporter))
h.GET("/ping", func(ctx context.Context, c *app.RequestContext) {
c.JSON(consts.StatusOK, utils.H{"ping": "pong"})
})
go h.Spin()
time.Sleep(time.Second)
c.Get(context.Background(), nil, "http://127.0.0.1:9231/ping")
} }

View File

@ -333,7 +333,7 @@ func WithDisablePrintRoute(b bool) config.Option {
// WithOnAccept sets the callback function when a new connection is accepted but cannot // WithOnAccept sets the callback function when a new connection is accepted but cannot
// receive data in netpoll. In go net, it will be called before converting tls connection // receive data in netpoll. In go net, it will be called before converting tls connection
func WithOnAccept(fn func(conn network.Conn) context.Context) config.Option { func WithOnAccept(fn func(conn net.Conn) context.Context) config.Option {
return config.Option{F: func(o *config.Options) { return config.Option{F: func(o *config.Options) {
o.OnAccept = fn o.OnAccept = fn
}} }}

View File

@ -83,7 +83,7 @@ type Options struct {
// If you'd like to check whether the peer IP is in the blacklist, you can use OnAccept. // If you'd like to check whether the peer IP is in the blacklist, you can use OnAccept.
// In go net, OnAccept is executed after connection accepted but before establishing // In go net, OnAccept is executed after connection accepted but before establishing
// tls connection. OnConnect is executed after establishing tls connection. // tls connection. OnConnect is executed after establishing tls connection.
OnAccept func(conn network.Conn) context.Context OnAccept func(conn net.Conn) context.Context
OnConnect func(ctx context.Context, conn network.Conn) context.Context OnConnect func(ctx context.Context, conn network.Conn) context.Context
// Registry is used for service registry. // Registry is used for service registry.

View File

@ -41,7 +41,7 @@ type transporter struct {
listener net.Listener listener net.Listener
eventLoop netpoll.EventLoop eventLoop netpoll.EventLoop
listenConfig *net.ListenConfig listenConfig *net.ListenConfig
OnAccept func(conn network.Conn) context.Context OnAccept func(conn net.Conn) context.Context
OnConnect func(ctx context.Context, conn network.Conn) context.Context OnConnect func(ctx context.Context, conn network.Conn) context.Context
} }

View File

@ -46,7 +46,7 @@ type transport struct {
tls *tls.Config tls *tls.Config
listenConfig *net.ListenConfig listenConfig *net.ListenConfig
lock sync.Mutex lock sync.Mutex
OnAccept func(conn network.Conn) context.Context OnAccept func(conn net.Conn) context.Context
OnConnect func(ctx context.Context, conn network.Conn) context.Context OnConnect func(ctx context.Context, conn network.Conn) context.Context
} }
@ -73,7 +73,7 @@ func (t *transport) serve() (err error) {
} }
if t.OnAccept != nil { if t.OnAccept != nil {
ctx = t.OnAccept(c) ctx = t.OnAccept(conn)
} }
if t.tls != nil { if t.tls != nil {