Skip to content

Commit 4ff6752

Browse files
committed
[main] deps update
1 parent 63ffd95 commit 4ff6752

File tree

4 files changed

+31
-20
lines changed

4 files changed

+31
-20
lines changed

cmd/api.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,15 +108,15 @@ func newHTTPServer(port uint) func(handler http.Handler) *http.Server {
108108
shutdownKey := fmt.Sprintf("http:%d", port)
109109

110110
shutdown.MustAdd(shutdownKey, func(ctx context.Context) {
111-
log.NoContext().Infof("Shutting down HTTP on %s...", addr)
111+
log.NoContext().Infof("Shutting down HTTP%s...", addr)
112112

113113
if err := srv.Shutdown(ctx); err != nil {
114114
log.NoContext().Errorf("HTTP shutdown failed: %v", err)
115115

116116
return
117117
}
118118

119-
log.NoContext().Info("HTTP shutdown succeeded!")
119+
log.NoContext().Infof("HTTP%s shutdown succeeded!", addr)
120120
})
121121

122122
return &srv
@@ -149,11 +149,11 @@ func newGRPCServer(port uint) *grpc.Server {
149149
shutdownKey := fmt.Sprintf("grpc:%d", port)
150150

151151
shutdown.MustAdd(shutdownKey, func(ctx context.Context) {
152-
log.NoContext().Infof("Shutting down GRPC on :%d...", port)
152+
log.NoContext().Infof("Shutting down GRPC:%d...", port)
153153

154154
srv.GracefulStop()
155155

156-
log.NoContext().Info("GRPC shutdown succeeded!")
156+
log.NoContext().Infof("GRPC:%d shutdown succeeded!", port)
157157
})
158158

159159
return srv

cmd/main.go

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ import (
1313
)
1414

1515
func main() {
16-
ctx := shutdown.Context()
16+
ctx, cancel := context.WithCancel(context.Background())
17+
defer cancel()
18+
1719
rootCmd := &cobra.Command{
1820
Use: "go-template",
1921
Short: "Main entry-point command for the application",
@@ -32,17 +34,34 @@ func main() {
3234
log.Fatal(ctx, err)
3335
}
3436

35-
defer gracefulShutdown()
36-
3737
rootCmd.AddCommand(
3838
APIServerCMD(cfg),
3939
)
4040

41-
if err = rootCmd.ExecuteContext(shutdown.Context()); err != nil {
42-
log.Errorf(ctx, "failed to execute root cmd: %v", err)
41+
done := make(chan struct{})
42+
gracefulShutdownDone := shutdown.Wait()
43+
44+
go func() {
45+
defer close(done)
46+
47+
defer cancel()
48+
49+
if shutdownErr := <-gracefulShutdownDone; shutdownErr != nil {
50+
log.NoContext().Errorf("failed to shutdown: %v", err)
51+
52+
return
53+
}
54+
55+
log.NoContext().Info("Graceful shutdown completed!")
56+
}()
57+
58+
if err = rootCmd.ExecuteContext(ctx); err != nil {
59+
log.NoContext().Errorf("failed to execute root cmd: %v", err)
4360

4461
return
4562
}
63+
64+
<-done
4665
}
4766

4867
func setupTracing(cfg config.Config) error {
@@ -93,13 +112,3 @@ func setupLogger(cfg config.Config) error {
93112

94113
return nil
95114
}
96-
97-
func gracefulShutdown() {
98-
if shutdownErr := shutdown.Wait(); shutdownErr != nil {
99-
log.NoContext().Error(shutdownErr)
100-
101-
return
102-
}
103-
104-
log.NoContext().Infof("Shutdown completed in %.1f seconds", shutdown.Timeout().Seconds())
105-
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ require (
66
github.com/go-chi/chi/v5 v5.0.7
77
github.com/go-chi/render v1.0.2
88
github.com/go-ozzo/ozzo-validation/v4 v4.3.0
9-
github.com/ra9dev/shutdown v1.0.0
9+
github.com/ra9dev/shutdown v1.0.2
1010
github.com/riandyrn/otelchi v0.5.0
1111
github.com/spf13/cobra v1.5.0
1212
github.com/spf13/viper v1.12.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
211211
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
212212
github.com/ra9dev/shutdown v1.0.0 h1:ihMXVK+UKYnzsgvCkrzIJihuFbTz80ufjaltIi1b6Yg=
213213
github.com/ra9dev/shutdown v1.0.0/go.mod h1:kP40t14LY9eFNGai1Y9mkgSomzmT6KHomklgPXeaWFs=
214+
github.com/ra9dev/shutdown v1.0.2 h1:+E7E3dSI3y8vSMfvxtx2thKbE3T2tEIn/aK1pliEOTA=
215+
github.com/ra9dev/shutdown v1.0.2/go.mod h1:kP40t14LY9eFNGai1Y9mkgSomzmT6KHomklgPXeaWFs=
214216
github.com/riandyrn/otelchi v0.5.0 h1:MJgGWsK8678BEgcFK0bXN0KCT+cmZenrLbfwcVdmMno=
215217
github.com/riandyrn/otelchi v0.5.0/go.mod h1:TdZGrioq34o3UK86q/3v220f4Zv/UOlffx74hSWlZsQ=
216218
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=

0 commit comments

Comments
 (0)