Skip to content

Commit

Permalink
feat: support for min-sigterm-delay
Browse files Browse the repository at this point in the history
  • Loading branch information
nancynh committed Aug 14, 2024
1 parent cd60317 commit 9e749d8
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
5 changes: 5 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,9 @@ the Proxy will then pick-up automatically.`)
localFlags.Uint64Var(&c.conf.MaxConnections, "max-connections", 0,
`Limits the number of connections by refusing any additional connections.
When this flag is not set, there is no limit.`)
localFlags.DurationVar(&c.conf.WaitBeforeClose, "min-sigterm-delay", 0,
`The number of seconds to accept new connections after receiving a TERM
signal. Defaults to 0s.`)
localFlags.DurationVar(&c.conf.WaitOnClose, "max-sigterm-delay", 0,
`Maximum amount of time to wait after for any open connections
to close after receiving a TERM signal. The proxy will shut
Expand Down Expand Up @@ -1179,8 +1182,10 @@ func runSignalWrapper(cmd *Command) (err error) {
switch {
case errors.Is(err, errSigInt):
cmd.logger.Infof("SIGINT signal received. Shutting down...")
time.Sleep(cmd.conf.WaitBeforeClose)
case errors.Is(err, errSigTerm):
cmd.logger.Infof("SIGTERM signal received. Shutting down...")
time.Sleep(cmd.conf.WaitBeforeClose)
default:
cmd.logger.Errorf("The proxy has encountered a terminal error: %v", err)
}
Expand Down
7 changes: 7 additions & 0 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,13 @@ func TestNewCommandArguments(t *testing.T) {
MaxConnections: 1,
}),
},
{
desc: "using min-sigterm-delay flag",
args: []string{"--min-sigterm-delay", "10s", "projects/proj/locations/region/clusters/clust/instances/inst"},
want: withDefaults(&proxy.Config{
WaitBeforeClose: 10 * time.Second,
}),
},
{
desc: "using wait after signterm flag",
args: []string{"--max-sigterm-delay", "10s", "projects/proj/locations/region/clusters/clust/instances/inst"},
Expand Down
5 changes: 5 additions & 0 deletions internal/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,11 @@ type Config struct {
// connections. A zero-value indicates no limit.
MaxConnections uint64

// WaitBeforeClose sets the duration to wait after receiving a shutdown signal
// but before closing the process. Not setting this field means to initiate
// the shutdown process immediately.
WaitBeforeClose time.Duration

// WaitOnClose sets the duration to wait for connections to close before
// shutting down. Not setting this field means to close immediately
// regardless of any open connections.
Expand Down

0 comments on commit 9e749d8

Please sign in to comment.