Skip to content

Commit b66f813

Browse files
committed
fix: shutting down process
Signed-off-by: Alessandro Yuichi Okimoto <yuichijpn@gmail.com>
1 parent abe9498 commit b66f813

2 files changed

Lines changed: 31 additions & 10 deletions

File tree

pkg/api/cmd/server.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package cmd
1717
import (
1818
"context"
1919
"fmt"
20+
"sync"
2021
"time"
2122

2223
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
@@ -582,16 +583,28 @@ func (s *server) Run(ctx context.Context, metrics metrics.Metrics, logger *zap.L
582583

583584
// Step 2: Gracefully stop all servers in parallel
584585
// Each server will reject new requests and wait for existing requests to complete.
585-
done := make(chan struct{})
586+
var wg sync.WaitGroup
587+
588+
wg.Add(1)
586589
go func() {
587-
defer close(done)
590+
defer wg.Done()
588591
server.Stop(serverShutDownTimeout)
589592
}()
590-
go apiGateway.Stop(serverShutDownTimeout)
591-
go httpServer.Stop(serverShutDownTimeout)
593+
594+
wg.Add(1)
595+
go func() {
596+
defer wg.Done()
597+
apiGateway.Stop(serverShutDownTimeout)
598+
}()
599+
600+
wg.Add(1)
601+
go func() {
602+
defer wg.Done()
603+
httpServer.Stop(serverShutDownTimeout)
604+
}()
592605

593606
// Wait for all servers to complete shutdown
594-
<-done
607+
wg.Wait()
595608

596609
// Step 3: Close clients
597610
// These are fast cleanup operations that can run asynchronously.

pkg/batch/cmd/server/server.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"fmt"
2020
"os"
2121
"strings"
22+
"sync"
2223
"time"
2324

2425
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
@@ -633,15 +634,22 @@ func (s *server) Run(ctx context.Context, metrics metrics.Metrics, logger *zap.L
633634

634635
// Step 2: Gracefully stop all servers in parallel
635636
// Each server will reject new requests and wait for existing requests to complete.
636-
done := make(chan struct{})
637+
var wg sync.WaitGroup
638+
639+
wg.Add(1)
637640
go func() {
638-
defer close(done)
641+
defer wg.Done()
639642
server.Stop(serverShutDownTimeout)
640643
}()
641-
go batchGateway.Stop(serverShutDownTimeout)
642644

643-
// Wait for gRPC server to complete shutdown
644-
<-done
645+
wg.Add(1)
646+
go func() {
647+
defer wg.Done()
648+
batchGateway.Stop(serverShutDownTimeout)
649+
}()
650+
651+
// Wait for all servers to complete shutdown
652+
wg.Wait()
645653

646654
// Step 3: Close clients
647655
// These are fast cleanup operations that can run asynchronously.

0 commit comments

Comments
 (0)