Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support for min-sigterm-delay #693

Merged
merged 1 commit into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading