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