From 694077e63104eb45cbbe9ed6cf39279244575201 Mon Sep 17 00:00:00 2001 From: KinjiKawaguchi Date: Sat, 6 Apr 2024 17:11:15 +0900 Subject: [PATCH 1/2] fix: Fix MySQL driver import and update server shutdown logic --- typing-server/api/cmd/main.go | 38 ++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/typing-server/api/cmd/main.go b/typing-server/api/cmd/main.go index 5826503..d9949b1 100644 --- a/typing-server/api/cmd/main.go +++ b/typing-server/api/cmd/main.go @@ -13,11 +13,11 @@ import ( "syscall" "time" - "github.com/go-sql-driver/mysql" "github.com/su-its/typing/typing-server/api/handler" "github.com/su-its/typing/typing-server/api/router" "github.com/su-its/typing/typing-server/domain/repository/ent" "github.com/su-its/typing/typing-server/domain/repository/ent/user" + "gorm.io/driver/mysql" ) func main() { @@ -76,18 +76,21 @@ func main() { } else { logger.Info("シードデータは挿入されませんでした") } - // WaitGroupの宣言 var wg sync.WaitGroup + // エラーを通知するためのチャネル errChan := make(chan error, 1) + // シグナルハンドリングの準備 sigChan := make(chan os.Signal, 1) signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM) + // HTTPサーバーの非同期起動 wg.Add(1) go func() { defer wg.Done() // 関数終了時にWaitGroupをデクリメント + // サーバーの設定 // ルーティングの設定 r := router.SetupRouter() @@ -97,29 +100,32 @@ func main() { Addr: ":8080", Handler: r, } + // 非同期でサーバーを開始 go func() { logger.Info("server is running at Addr :8080") - if err := server.ListenAndServe(); err != http.ErrServerClosed { + if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed { logger.Error("failed to listen and serve: %v", err) errChan <- err // エラーをチャネルに送信 } }() - // シグナルを待機 - <-sigChan - logger.Info("shutting down the server...") - ctx := context.TODO() // Use context.TODO() as a temporary placeholder - if err := server.Shutdown(ctx); err != nil { - logger.Error("error during server shutdown: %v", err) - errChan <- err // エラーをチャネルに送信 + + // エラーまたはシグナルを待機 + select { + case err := <-errChan: + logger.Error("server stopped due to an error: %v", err) + case sig := <-sigChan: + logger.Info("received signal: %v", sig) + // グレースフルシャットダウン + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + if err := server.Shutdown(ctx); err != nil { + logger.Error("error during server shutdown: %v", err) + errChan <- err // エラーをチャネルに送信 + } } }() - select { - case <-errChan: // エラーが発生した場合 - logger.Error("server stopped due to an error") - case sig := <-sigChan: // シグナルを受信した場合 - logger.Info("received signal: %s", sig) - } + wg.Wait() // HTTPサーバーの終了を待機 close(errChan) logger.Info("server exited") From 17e2eec8ebe798c0e1c44df680b3e66777bff471 Mon Sep 17 00:00:00 2001 From: KinjiKawaguchi Date: Sat, 6 Apr 2024 17:21:50 +0900 Subject: [PATCH 2/2] =?UTF-8?q?fix:=20MySQL=E3=83=89=E3=83=A9=E3=82=A4?= =?UTF-8?q?=E3=83=90=E3=81=AE=E3=82=A4=E3=83=B3=E3=83=9D=E3=83=BC=E3=83=88?= =?UTF-8?q?=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- typing-server/api/cmd/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/typing-server/api/cmd/main.go b/typing-server/api/cmd/main.go index d9949b1..eba4b0f 100644 --- a/typing-server/api/cmd/main.go +++ b/typing-server/api/cmd/main.go @@ -13,11 +13,11 @@ import ( "syscall" "time" + "github.com/go-sql-driver/mysql" "github.com/su-its/typing/typing-server/api/handler" "github.com/su-its/typing/typing-server/api/router" "github.com/su-its/typing/typing-server/domain/repository/ent" "github.com/su-its/typing/typing-server/domain/repository/ent/user" - "gorm.io/driver/mysql" ) func main() {