server: graceful shutdown for fast failover. Fixes #308

This commit is contained in:
Armon Dadgar
2015-06-17 18:24:56 -07:00
parent c60889572e
commit 70ee1866ca
2 changed files with 31 additions and 3 deletions

View File

@@ -2,6 +2,8 @@ package cli
import (
"os"
"os/signal"
"syscall"
auditFile "github.com/hashicorp/vault/builtin/audit/file"
auditSyslog "github.com/hashicorp/vault/builtin/audit/syslog"
@@ -68,6 +70,7 @@ func Commands(metaPtr *command.Meta) map[string]cli.CommandFactory {
"transit": transit.Factory,
"mysql": mysql.Factory,
},
ShutdownCh: makeShutdownCh(),
}, nil
},
@@ -268,3 +271,20 @@ func Commands(metaPtr *command.Meta) map[string]cli.CommandFactory {
},
}
}
// makeShutdownCh returns a channel that can be used for shutdown
// notifications for commands. This channel will send a message for every
// interrupt or SIGTERM received.
func makeShutdownCh() <-chan struct{} {
resultCh := make(chan struct{})
signalCh := make(chan os.Signal, 4)
signal.Notify(signalCh, os.Interrupt, syscall.SIGTERM)
go func() {
for {
<-signalCh
resultCh <- struct{}{}
}
}()
return resultCh
}