use done chan

This commit is contained in:
Денис Евстигнеев 2024-03-07 16:30:52 +03:00
parent 9d321b12b8
commit 8661d62ce8

25
tcp.go
View File

@ -2,7 +2,6 @@
package tcp // import "go.unistack.org/micro-server-tcp/v3" package tcp // import "go.unistack.org/micro-server-tcp/v3"
import ( import (
"context"
"crypto/tls" "crypto/tls"
"fmt" "fmt"
"net" "net"
@ -16,7 +15,6 @@ import (
"go.unistack.org/micro/v3/register" "go.unistack.org/micro/v3/register"
"go.unistack.org/micro/v3/server" "go.unistack.org/micro/v3/server"
"golang.org/x/net/netutil" "golang.org/x/net/netutil"
"golang.org/x/sync/errgroup"
) )
type tcpServer struct { type tcpServer struct {
@ -28,8 +26,6 @@ type tcpServer struct {
sync.RWMutex sync.RWMutex
registered bool registered bool
init bool init bool
wg sync.WaitGroup
} }
func (h *tcpServer) newCodec(ct string) (codec.Codec, error) { func (h *tcpServer) newCodec(ct string) (codec.Codec, error) {
@ -431,24 +427,17 @@ func (h *tcpServer) gracefulStop() {
tm := time.NewTimer(h.opts.GracefulTimeout) tm := time.NewTimer(h.opts.GracefulTimeout)
defer tm.Stop() defer tm.Stop()
g, gctx := errgroup.WithContext(context.Background()) done := make(chan struct{})
go func() {
g.Go(func() error {
h.opts.Wait.Wait() h.opts.Wait.Wait()
return nil done <- struct{}{}
}) }()
g.Go(func() error {
select { select {
case <-tm.C: case <-tm.C:
return nil case <-done:
case <-gctx.Done(): close(done)
return gctx.Err()
} }
})
g.Wait()
return
} }
func (h *tcpServer) String() string { func (h *tcpServer) String() string {
@ -501,8 +490,8 @@ func (h *tcpServer) serve(ln net.Listener, hd Handler) {
h.opts.Wait.Add(1) h.opts.Wait.Add(1)
go func() { go func() {
defer h.opts.Wait.Done()
hd.Serve(c) hd.Serve(c)
h.opts.Wait.Done()
}() }()
} }
} }