diff options
author | Igor Drozdov <idrozdov@gitlab.com> | 2021-07-09 14:41:41 +0300 |
---|---|---|
committer | Igor Drozdov <idrozdov@gitlab.com> | 2021-07-15 14:39:33 +0300 |
commit | 569a0197cacc75270776217c27e9d709907a9dfa (patch) | |
tree | b693a2244f3d715d48df83eac70bdf6630e51d3a /cmd/gitlab-sshd/main.go | |
parent | d3711d8d7e781dbff01d8ae5c7a1d5b800c5c8a2 (diff) | |
download | gitlab-shell-569a0197cacc75270776217c27e9d709907a9dfa.tar.gz |
Shutdown sshd gracefully
When interruption signal is sent, we are closing ssh listener to
prevent it from accepting new connections
Then after configured grace period, we cancel the context to
cancel all ongoing operations
Diffstat (limited to 'cmd/gitlab-sshd/main.go')
-rw-r--r-- | cmd/gitlab-sshd/main.go | 28 |
1 files changed, 27 insertions, 1 deletions
diff --git a/cmd/gitlab-sshd/main.go b/cmd/gitlab-sshd/main.go index 866bc8d..7cecbf5 100644 --- a/cmd/gitlab-sshd/main.go +++ b/cmd/gitlab-sshd/main.go @@ -3,6 +3,10 @@ package main import ( "flag" "os" + "os/signal" + "context" + "syscall" + "time" log "github.com/sirupsen/logrus" @@ -63,6 +67,8 @@ func main() { ctx, finished := command.Setup("gitlab-sshd", cfg) defer finished() + server := sshd.Server{Config: cfg} + // Startup monitoring endpoint. if cfg.Server.WebListen != "" { go func() { @@ -75,7 +81,27 @@ func main() { }() } - if err := sshd.Run(ctx, cfg); err != nil { + ctx, cancel := context.WithCancel(ctx) + defer cancel() + + done := make(chan os.Signal, 1) + signal.Notify(done, syscall.SIGINT, syscall.SIGTERM) + + go func() { + sig := <-done + signal.Reset(syscall.SIGINT, syscall.SIGTERM) + + log.WithFields(log.Fields{"shutdown_timeout_s": cfg.Server.GracePeriodSeconds, "signal": sig.String()}).Infof("Shutdown initiated") + + server.Shutdown() + + <-time.After(cfg.Server.GracePeriod()) + + cancel() + + }() + + if err := server.ListenAndServe(ctx); err != nil { log.Fatalf("Failed to start GitLab built-in sshd: %v", err) } } |