Browse Source

Add IMGPROXY_KEEP_ALIVE_TIMEOUT config

DarthSim 6 years ago
parent
commit
8517402e7e
3 changed files with 20 additions and 8 deletions
  1. 13 8
      config.go
  2. 1 0
      docs/configuration.md
  3. 6 0
      server.go

+ 13 - 8
config.go

@@ -128,14 +128,14 @@ func presetFileConfig(p presets, filepath string) {
 }
 
 type config struct {
-	Bind            string
-	ReadTimeout     int
-	WaitTimeout     int
-	WriteTimeout    int
-	DownloadTimeout int
-	Concurrency     int
-	MaxClients      int
-	TTL             int
+	Bind             string
+	ReadTimeout      int
+	WriteTimeout     int
+	KeepAliveTimeout int
+	DownloadTimeout  int
+	Concurrency      int
+	MaxClients       int
+	TTL              int
 
 	MaxSrcDimension    int
 	MaxSrcResolution   int
@@ -211,6 +211,7 @@ var conf = config{
 	Bind:                           ":8080",
 	ReadTimeout:                    10,
 	WriteTimeout:                   10,
+	KeepAliveTimeout:               10,
 	DownloadTimeout:                5,
 	Concurrency:                    runtime.NumCPU() * 2,
 	TTL:                            3600,
@@ -250,6 +251,7 @@ func configure() {
 	strEnvConfig(&conf.Bind, "IMGPROXY_BIND")
 	intEnvConfig(&conf.ReadTimeout, "IMGPROXY_READ_TIMEOUT")
 	intEnvConfig(&conf.WriteTimeout, "IMGPROXY_WRITE_TIMEOUT")
+	intEnvConfig(&conf.KeepAliveTimeout, "IMGPROXY_KEEP_ALIVE_TIMEOUT")
 	intEnvConfig(&conf.DownloadTimeout, "IMGPROXY_DOWNLOAD_TIMEOUT")
 	intEnvConfig(&conf.Concurrency, "IMGPROXY_CONCURRENCY")
 	intEnvConfig(&conf.MaxClients, "IMGPROXY_MAX_CLIENTS")
@@ -362,6 +364,9 @@ func configure() {
 	if conf.WriteTimeout <= 0 {
 		logFatal("Write timeout should be greater than 0, now - %d\n", conf.WriteTimeout)
 	}
+	if conf.KeepAliveTimeout < 0 {
+		logFatal("KeepAlive timeout should be greater than or equal to 0, now - %d\n", conf.KeepAliveTimeout)
+	}
 
 	if conf.DownloadTimeout <= 0 {
 		logFatal("Download timeout should be greater than 0, now - %d\n", conf.DownloadTimeout)

+ 1 - 0
docs/configuration.md

@@ -29,6 +29,7 @@ $ echo $(xxd -g 2 -l 64 -p /dev/random | tr -d '\n')
 * `IMGPROXY_BIND`: TCP address and port to listen on. Default: `:8080`;
 * `IMGPROXY_READ_TIMEOUT`: the maximum duration (in seconds) for reading the entire image request, including the body. Default: `10`;
 * `IMGPROXY_WRITE_TIMEOUT`: the maximum duration (in seconds) for writing the response. Default: `10`;
+* `IMGPROXY_KEEP_ALIVE_TIMEOUT`: the maximum duration (in seconds) to wait for the next request before closing the connection. When set to `0`, keep-alive is disabled. Default: `10`;
 * `IMGPROXY_DOWNLOAD_TIMEOUT`: the maximum duration (in seconds) for downloading the source image. Default: `5`;
 * `IMGPROXY_CONCURRENCY`: the maximum number of image requests to be processed simultaneously. Default: number of CPU cores times two;
 * `IMGPROXY_MAX_CLIENTS`: the maximum number of simultaneous active connections. Default: `IMGPROXY_CONCURRENCY * 10`;

+ 6 - 0
server.go

@@ -42,6 +42,12 @@ func startServer() *http.Server {
 		MaxHeaderBytes: 1 << 20,
 	}
 
+	if conf.KeepAliveTimeout > 0 {
+		s.IdleTimeout = time.Duration(conf.KeepAliveTimeout) * time.Second
+	} else {
+		s.SetKeepAlivesEnabled(false)
+	}
+
 	initProcessingHandler()
 
 	go func() {