Skip to content

Commit 3b1a138

Browse files
committed
ING-1399: Make shutdown timeout configurable through flags
1 parent 85426a2 commit 3b1a138

4 files changed

Lines changed: 29 additions & 17 deletions

File tree

cmd/gateway/main.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ func init() {
9494
configFlags.String("dapi-cert", "", "path to data api tls cert for Data API")
9595
configFlags.String("dapi-key", "", "path to data api private tls key for Data API")
9696
configFlags.Int("rate-limit", 0, "specifies the maximum requests per second to allow")
97+
configFlags.Duration("shutdown-timeout", 30*time.Second, "the graceful shutdown timeout")
9798
configFlags.String("otlp-endpoint", "", "opentelemetry endpoint to send telemetry to")
9899
configFlags.Bool("disable-traces", false, "disable tracing")
99100
configFlags.Bool("disable-metrics", false, "disable metrics")
@@ -269,6 +270,7 @@ type config struct {
269270
clusterCaCertPath string
270271
clientCaCertPath string
271272
rateLimit int
273+
shutdownTimeout time.Duration
272274
otlpEndpoint string
273275
disableTraces bool
274276
disableMetrics bool
@@ -312,6 +314,7 @@ func readConfig(logger *zap.Logger) *config {
312314
clusterCaCertPath: viper.GetString("cluster-cert"),
313315
clientCaCertPath: viper.GetString("client-ca-cert"),
314316
rateLimit: viper.GetInt("rate-limit"),
317+
shutdownTimeout: viper.GetDuration("shutdown-timeout"),
315318
otlpEndpoint: viper.GetString("otlp-endpoint"),
316319
disableTraces: viper.GetBool("disable-traces"),
317320
disableMetrics: viper.GetBool("disable-metrics"),
@@ -354,6 +357,7 @@ func readConfig(logger *zap.Logger) *config {
354357
zap.String("dapiKeyPath", config.dapiKeyPath),
355358
zap.String("clusterCaCertPath", config.clusterCaCertPath),
356359
zap.Int("rateLimit", config.rateLimit),
360+
zap.Duration("shutdownTimeout", config.shutdownTimeout),
357361
zap.String("otlpEndpoint", config.otlpEndpoint),
358362
zap.Bool("disableTraces", config.disableTraces),
359363
zap.Bool("disableMetrics", config.disableMetrics),
@@ -645,6 +649,7 @@ func startGateway() {
645649
BindDapiPort: config.dapiPort,
646650
BindAddress: config.bindAddress,
647651
RateLimit: config.rateLimit,
652+
ShutdownTimeout: config.shutdownTimeout,
648653
GrpcCertificate: grpcCertificate,
649654
DapiCertificate: dapiCertificate,
650655
ClusterCaCert: caCertPool,
@@ -684,8 +689,9 @@ func startGateway() {
684689

685690
if newConfig.bindAddress != config.bindAddress ||
686691
newConfig.dataPort != config.dataPort ||
687-
newConfig.dapiPort != config.dapiPort {
688-
logger.Warn("config changes for bindAddress, dataPort or dapiPort require a restart")
692+
newConfig.dapiPort != config.dapiPort ||
693+
newConfig.shutdownTimeout != config.shutdownTimeout {
694+
logger.Warn("config changes for bindAddress, dataPort, dapiPort or shutdownTimeout require a restart")
689695
}
690696

691697
if newConfig.selfSign != config.selfSign {

gateway/gateway.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ type Config struct {
6666
AdvertiseAddress string
6767
AdvertisePorts ServicePorts
6868

69-
RateLimit int
69+
RateLimit int
70+
ShutdownTimeout time.Duration
7071

7172
GrpcCertificate tls.Certificate
7273
DapiCertificate tls.Certificate
@@ -432,9 +433,10 @@ func (g *Gateway) Run(ctx context.Context) error {
432433
ClientCAs: config.ClientCaCert,
433434
ClientAuth: tls.VerifyClientCertIfGiven,
434435
},
435-
AlphaEndpoints: config.AlphaEndpoints,
436-
Debug: config.Debug,
437-
Cancel: cancel,
436+
ShutdownTimeout: config.ShutdownTimeout,
437+
AlphaEndpoints: config.AlphaEndpoints,
438+
Debug: config.Debug,
439+
Cancel: cancel,
438440
})
439441
if err != nil {
440442
config.Logger.Error("error creating legacy proxy")

gateway/system/system.go

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,12 @@ type SystemOptions struct {
5454
DapiImpl *dapiimpl.Servers
5555
Metrics *metrics.SnMetrics
5656

57-
RateLimiter ratelimiting.RateLimiter
58-
GrpcTlsConfig *tls.Config
59-
DapiTlsConfig *tls.Config
60-
AlphaEndpoints bool
61-
Debug bool
57+
RateLimiter ratelimiting.RateLimiter
58+
GrpcTlsConfig *tls.Config
59+
DapiTlsConfig *tls.Config
60+
ShutdownTimeout time.Duration
61+
AlphaEndpoints bool
62+
Debug bool
6263

6364
Cancel context.CancelFunc
6465
}
@@ -69,7 +70,8 @@ type System struct {
6970
dataServer *grpc.Server
7071
dapiServer *http.Server
7172

72-
cancelFunc context.CancelFunc
73+
shutdownTimeout time.Duration
74+
cancelFunc context.CancelFunc
7375
}
7476

7577
func NewSystem(opts *SystemOptions) (*System, error) {
@@ -243,10 +245,11 @@ func NewSystem(opts *SystemOptions) (*System, error) {
243245
}
244246

245247
s := &System{
246-
logger: opts.Logger,
247-
dataServer: dataSrv,
248-
dapiServer: dapiSrv,
249-
cancelFunc: opts.Cancel,
248+
logger: opts.Logger,
249+
dataServer: dataSrv,
250+
dapiServer: dapiSrv,
251+
shutdownTimeout: opts.ShutdownTimeout,
252+
cancelFunc: opts.Cancel,
250253
}
251254

252255
return s, nil
@@ -304,7 +307,7 @@ func (s *System) Shutdown() {
304307
defer wg.Done()
305308
s.dapiServer.SetKeepAlivesEnabled(false)
306309
time.Sleep(time.Second * 5)
307-
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
310+
ctx, cancel := context.WithTimeout(context.Background(), s.shutdownTimeout)
308311
defer cancel()
309312
_ = s.dapiServer.Shutdown(ctx)
310313
}()

gateway/test/dapi_graceful_shutdown_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ func (s *GatewayOpsTestSuite) TestGracefulShutdown() {
4949
StartupCallback: func(m *gateway.StartupInfo) {
5050
gwStartInfoCh <- m
5151
},
52+
ShutdownTimeout: time.Second * 10,
5253
})
5354
if err != nil {
5455
s.T().Fatalf("failed to initialize graceful-shutdown-gateway: %s", err)

0 commit comments

Comments
 (0)