add waitGroups for waiting finish all connects (#131)

closes #130

Reviewed-on: #131
Co-authored-by: Evstigneev Denis <danteevstigneev@yandex.ru>
Co-committed-by: Evstigneev Denis <danteevstigneev@yandex.ru>
This commit is contained in:
2024-03-13 15:36:34 +03:00
parent 52e72aef57
commit db00f3bb91
5 changed files with 73 additions and 21 deletions

18
tcp.go
View File

@@ -2,6 +2,7 @@
package tcp // import "go.unistack.org/micro-server-tcp/v3"
import (
"context"
"crypto/tls"
"fmt"
"net"
@@ -14,6 +15,7 @@ import (
"go.unistack.org/micro/v3/logger"
"go.unistack.org/micro/v3/register"
"go.unistack.org/micro/v3/server"
"golang.org/x/net/netutil"
)
@@ -401,6 +403,8 @@ func (h *tcpServer) Start() error {
}
}
h.gracefulStop()
ch <- ts.Close()
// deregister
@@ -422,6 +426,13 @@ func (h *tcpServer) Stop() error {
return <-ch
}
func (h *tcpServer) gracefulStop() {
ctx, cancel := context.WithTimeout(context.Background(), h.opts.GracefulTimeout)
defer cancel()
h.opts.Wait.WaitContext(ctx)
}
func (h *tcpServer) String() string {
return "tcp"
}
@@ -469,7 +480,12 @@ func (h *tcpServer) serve(ln net.Listener, hd Handler) {
config.Logger.Errorf(config.Context, "tcp: accept err: %v", err)
return
}
go hd.Serve(c)
h.opts.Wait.Add(1)
go func() {
hd.Serve(c)
h.opts.Wait.Done()
}()
}
}