Skip to content

Commit b747c3f

Browse files
committed
fix: handle SIGTERM shutdown correctly
1 parent 96ff926 commit b747c3f

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

cmd/redis-shake/main.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -295,17 +295,24 @@ func waitShutdown(cancel context.CancelFunc) {
295295
sigTimes := 0
296296
for {
297297
sig := <-quitCh
298-
sigTimes++
299-
if sig != syscall.SIGINT {
300-
log.Infof("Got signal: %s.", sig)
301-
} else {
302-
log.Infof("Got signal: %s to exit. Press Ctrl+C again to force exit.", sig)
303-
if sigTimes >= 2 {
304-
os.Exit(0)
305-
}
306-
cancel()
298+
if shouldForceExit := handleShutdownSignal(sig, &sigTimes, cancel); shouldForceExit {
299+
os.Exit(0)
307300
}
301+
}
302+
}
308303

304+
func handleShutdownSignal(sig os.Signal, sigTimes *int, cancel context.CancelFunc) bool {
305+
if sig == syscall.SIGINT {
306+
*sigTimes = *sigTimes + 1
307+
log.Infof("Got signal: %s to exit. Press Ctrl+C again to force exit.", sig)
308+
if *sigTimes >= 2 {
309+
return true
310+
}
311+
cancel()
312+
return false
309313
}
310314

315+
log.Infof("Got signal: %s to exit.", sig)
316+
cancel()
317+
return false
311318
}

0 commit comments

Comments
 (0)