From 570ef653743c1718f47a2dc8d97424c8ad40bae3 Mon Sep 17 00:00:00 2001 From: YarBor Date: Sun, 7 Apr 2024 10:29:27 +0800 Subject: [PATCH 1/2] optimize(hz) : tcp host:port use of closed network connection on server shutdown Signed-off-by: YarBor --- pkg/network/standard/transport.go | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/pkg/network/standard/transport.go b/pkg/network/standard/transport.go index b5f169e6e..5c4422db9 100644 --- a/pkg/network/standard/transport.go +++ b/pkg/network/standard/transport.go @@ -21,6 +21,7 @@ import ( "crypto/tls" "net" "sync" + "sync/atomic" "time" "github.com/cloudwego/hertz/pkg/common/config" @@ -28,6 +29,11 @@ import ( "github.com/cloudwego/hertz/pkg/network" ) +const ( + transportAlive int32 = 0 + transportShutdown int32 = 1 +) + type transport struct { // Per-connection buffer size for requests' reading. // This also limits the maximum header size. @@ -36,6 +42,7 @@ type transport struct { // and/or multi-KB headers (for example, BIG cookies). // // Default buffer size is used if not set. + isCallShutDown int32 readBufferSize int network string addr string @@ -68,8 +75,11 @@ func (t *transport) serve() (err error) { conn, err := t.ln.Accept() var c network.Conn if err != nil { - hlog.SystemLogger().Errorf("Error=%s", err.Error()) - return err + if !t.IsCallShutdown() { + hlog.SystemLogger().Errorf("Error=%s", err.Error()) + return err + } + return nil } if t.OnAccept != nil { @@ -100,7 +110,12 @@ func (t *transport) Close() error { return t.Shutdown(ctx) } +func (t *transport) IsCallShutdown() bool { + return atomic.LoadInt32(&t.isCallShutDown) == transportShutdown +} + func (t *transport) Shutdown(ctx context.Context) error { + atomic.StoreInt32(&t.isCallShutDown, transportShutdown) defer func() { network.UnlinkUdsFile(t.network, t.addr) //nolint:errcheck }() From c2767b6a8628c1bbb94047d63fe32b700e692dc0 Mon Sep 17 00:00:00 2001 From: YarBor Date: Wed, 5 Jun 2024 10:59:33 +0800 Subject: [PATCH 2/2] fix comment Signed-off-by: YarBor --- pkg/network/standard/transport.go | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/pkg/network/standard/transport.go b/pkg/network/standard/transport.go index 5c4422db9..4e565cda8 100644 --- a/pkg/network/standard/transport.go +++ b/pkg/network/standard/transport.go @@ -29,11 +29,6 @@ import ( "github.com/cloudwego/hertz/pkg/network" ) -const ( - transportAlive int32 = 0 - transportShutdown int32 = 1 -) - type transport struct { // Per-connection buffer size for requests' reading. // This also limits the maximum header size. @@ -42,7 +37,7 @@ type transport struct { // and/or multi-KB headers (for example, BIG cookies). // // Default buffer size is used if not set. - isCallShutDown int32 + isShutDown atomic.Bool readBufferSize int network string addr string @@ -75,7 +70,7 @@ func (t *transport) serve() (err error) { conn, err := t.ln.Accept() var c network.Conn if err != nil { - if !t.IsCallShutdown() { + if !t.isCallShutdown() { hlog.SystemLogger().Errorf("Error=%s", err.Error()) return err } @@ -110,12 +105,12 @@ func (t *transport) Close() error { return t.Shutdown(ctx) } -func (t *transport) IsCallShutdown() bool { - return atomic.LoadInt32(&t.isCallShutDown) == transportShutdown +func (t *transport) isCallShutdown() bool { + return t.isShutDown.Load() } func (t *transport) Shutdown(ctx context.Context) error { - atomic.StoreInt32(&t.isCallShutDown, transportShutdown) + t.isShutDown.Store(true) defer func() { network.UnlinkUdsFile(t.network, t.addr) //nolint:errcheck }()