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:
18
tcp.go
18
tcp.go
@@ -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()
|
||||
}()
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user