From 9fc0c11fcace1afbf4f0dc0515182682f111e753 Mon Sep 17 00:00:00 2001 From: Iceber Gu Date: Tue, 6 Dec 2022 11:27:35 +0800 Subject: [PATCH] Sending signals by closing the channel Signed-off-by: Iceber Gu --- src/cmd/migrate-patch/main.go | 7 +++++-- src/core/main.go | 4 ++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/cmd/migrate-patch/main.go b/src/cmd/migrate-patch/main.go index a95eac205eb0..755fd36a8a11 100644 --- a/src/cmd/migrate-patch/main.go +++ b/src/cmd/migrate-patch/main.go @@ -34,20 +34,23 @@ func main() { log.Fatalf("Failed to connect to Database, error: %v\n", err) } defer db.Close() - c := make(chan struct{}, 1) + + c := make(chan struct{}) go func() { + defer close(c) + err := db.Ping() for ; err != nil; err = db.Ping() { log.Println("Failed to Ping DB, sleep for 1 second.") time.Sleep(1 * time.Second) } - c <- struct{}{} }() select { case <-c: case <-time.After(30 * time.Second): log.Fatal("Failed to connect DB after 30 seconds, time out. \n") } + row := db.QueryRow(pgSQLCheckColStmt) var tblCount, colCount int if err := row.Scan(&tblCount, &colCount); err != nil { diff --git a/src/core/main.go b/src/core/main.go index 101e2ca056bc..c8effb9c9d6b 100755 --- a/src/core/main.go +++ b/src/core/main.go @@ -97,14 +97,14 @@ func gracefulShutdown(closing, done chan struct{}, shutdowns ...func()) { signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT) log.Infof("capture system signal %s, to close \"closing\" channel", <-signals) close(closing) - shutdownChan := make(chan struct{}, 1) + shutdownChan := make(chan struct{}) go func() { + defer close(shutdownChan) for _, s := range shutdowns { s() } <-done log.Infof("Goroutines exited normally") - shutdownChan <- struct{}{} }() select { case <-shutdownChan: