From 3907a5579b8b06ea4ffb3cb2d70c08ad61237ae5 Mon Sep 17 00:00:00 2001 From: Dominic Le Bredonchel Date: Thu, 14 Mar 2019 13:15:25 +0000 Subject: [PATCH] Add configuration for worker shutdown timeout (#198) --- cmd/feed-ingress/main.go | 3 +++ nginx/nginx.go | 1 + nginx/nginx.tmpl | 4 ++++ nginx/nginx_test.go | 18 ++++++++++++++++++ 4 files changed, 26 insertions(+) diff --git a/cmd/feed-ingress/main.go b/cmd/feed-ingress/main.go index 066b1b4e..8a0404c8 100644 --- a/cmd/feed-ingress/main.go +++ b/cmd/feed-ingress/main.go @@ -89,6 +89,7 @@ const ( defaultNginxWorkingDir = "/nginx" defaultNginxWorkers = 1 defaultNginxWorkerConnections = 1024 + defaultNginxWorkerShutdownTimeoutSeconds = 0 defaultNginxKeepAliveSeconds = 60 defaultNginxBackendKeepalives = 512 defaultNginxBackendTimeoutSeconds = 60 @@ -170,6 +171,8 @@ func init() { "Number of nginx worker processes.") flag.IntVar(&nginxConfig.WorkerConnections, "nginx-worker-connections", defaultNginxWorkerConnections, "Max number of connections per nginx worker. Includes both client and proxy connections.") + flag.IntVar(&nginxConfig.WorkerShutdownTimeoutSeconds, "nginx-worker-shutdown-timeout-seconds", defaultNginxWorkerShutdownTimeoutSeconds, + "Timeout for a graceful shutdown of worker processes.") flag.IntVar(&nginxConfig.KeepaliveSeconds, "nginx-keepalive-seconds", defaultNginxKeepAliveSeconds, "Keep alive time for persistent client connections to nginx. Should generally be set larger than frontend "+ "keep alive times to prevent stale connections.") diff --git a/nginx/nginx.go b/nginx/nginx.go index a11ce4f3..ed085b2f 100644 --- a/nginx/nginx.go +++ b/nginx/nginx.go @@ -36,6 +36,7 @@ type Conf struct { WorkingDir string WorkerProcesses int WorkerConnections int + WorkerShutdownTimeoutSeconds int KeepaliveSeconds int BackendKeepalives int BackendConnectTimeoutSeconds int diff --git a/nginx/nginx.tmpl b/nginx/nginx.tmpl index 45fae2dd..04c21310 100644 --- a/nginx/nginx.tmpl +++ b/nginx/nginx.tmpl @@ -8,6 +8,10 @@ pid {{ .WorkingDir }}/nginx.pid; load_module modules/ngx_http_opentracing_module.so; {{ end }} +{{ if .WorkerShutdownTimeoutSeconds }} +worker_shutdown_timeout {{ .WorkerShutdownTimeoutSeconds }}; +{{ end }} + events { # Accept connections as fast as possible. multi_accept on; diff --git a/nginx/nginx_test.go b/nginx/nginx_test.go index d39227c1..5357cc70 100644 --- a/nginx/nginx_test.go +++ b/nginx/nginx_test.go @@ -37,6 +37,7 @@ func newConf(tmpDir string, binary string) Conf { BinaryLocation: binary, Ports: []Port{{Name: "http", Port: port}}, WorkerProcesses: 1, + WorkerShutdownTimeoutSeconds: 0, BackendKeepalives: 1024, BackendConnectTimeoutSeconds: 1, ServerNamesHashMaxSize: -1, @@ -224,6 +225,9 @@ func TestNginxConfig(t *testing.T) { incorrectLargeClientHeaderBufferConf := defaultConf incorrectLargeClientHeaderBufferConf.LargeClientHeaderBufferBlocks = 4 + workerShutdowntimeoutConf := defaultConf + workerShutdowntimeoutConf.WorkerShutdownTimeoutSeconds = 10 + var tests = []struct { name string conf Conf @@ -408,6 +412,20 @@ func TestNginxConfig(t *testing.T) { "!large_client_header_buffers", }, }, + { + "Worker shutdown timeout setting not present if default", + defaultConf, + []string{ + "!worker_shutdown_timeout", + }, + }, + { + "Worker shutdown timeout is present if not set to default", + workerShutdowntimeoutConf, + []string{ + "worker_shutdown_timeout 10;", + }, + }, } for _, test := range tests {