Explorar el Código

Add IMGPROXY_CLIENT_KEEP_ALIVE_TIMEOUT config

DarthSim hace 2 años
padre
commit
4e669c8717
Se han modificado 4 ficheros con 28 adiciones y 16 borrados
  1. 2 0
      CHANGELOG.md
  2. 15 9
      config/config.go
  3. 1 0
      docs/configuration.md
  4. 10 7
      imagedata/download.go

+ 2 - 0
CHANGELOG.md

@@ -1,6 +1,8 @@
 # Changelog
 
 ## [Unreleased]
+### Add
+- Add `IMGPROXY_CLIENT_KEEP_ALIVE_TIMEOUT` config.
 
 ## [3.9.0] - 2022-10-19
 ### Add

+ 15 - 9
config/config.go

@@ -16,15 +16,16 @@ import (
 )
 
 var (
-	Network           string
-	Bind              string
-	ReadTimeout       int
-	WriteTimeout      int
-	KeepAliveTimeout  int
-	DownloadTimeout   int
-	Concurrency       int
-	RequestsQueueSize int
-	MaxClients        int
+	Network                string
+	Bind                   string
+	ReadTimeout            int
+	WriteTimeout           int
+	KeepAliveTimeout       int
+	ClientKeepAliveTimeout int
+	DownloadTimeout        int
+	Concurrency            int
+	RequestsQueueSize      int
+	MaxClients             int
 
 	TTL                     int
 	CacheControlPassthrough bool
@@ -197,6 +198,7 @@ func Reset() {
 	ReadTimeout = 10
 	WriteTimeout = 10
 	KeepAliveTimeout = 10
+	ClientKeepAliveTimeout = 90
 	DownloadTimeout = 5
 	Concurrency = runtime.NumCPU() * 2
 	RequestsQueueSize = 0
@@ -364,6 +366,7 @@ func Configure() error {
 	configurators.Int(&ReadTimeout, "IMGPROXY_READ_TIMEOUT")
 	configurators.Int(&WriteTimeout, "IMGPROXY_WRITE_TIMEOUT")
 	configurators.Int(&KeepAliveTimeout, "IMGPROXY_KEEP_ALIVE_TIMEOUT")
+	configurators.Int(&ClientKeepAliveTimeout, "IMGPROXY_CLIENT_KEEP_ALIVE_TIMEOUT")
 	configurators.Int(&DownloadTimeout, "IMGPROXY_DOWNLOAD_TIMEOUT")
 	configurators.Int(&Concurrency, "IMGPROXY_CONCURRENCY")
 	configurators.Int(&RequestsQueueSize, "IMGPROXY_REQUESTS_QUEUE_SIZE")
@@ -563,6 +566,9 @@ func Configure() error {
 	if KeepAliveTimeout < 0 {
 		return fmt.Errorf("KeepAlive timeout should be greater than or equal to 0, now - %d\n", KeepAliveTimeout)
 	}
+	if ClientKeepAliveTimeout < 0 {
+		return fmt.Errorf("Client KeepAlive timeout should be greater than or equal to 0, now - %d\n", ClientKeepAliveTimeout)
+	}
 
 	if DownloadTimeout <= 0 {
 		return fmt.Errorf("Download timeout should be greater than 0, now - %d\n", DownloadTimeout)

+ 1 - 0
docs/configuration.md

@@ -31,6 +31,7 @@ echo $(xxd -g 2 -l 64 -p /dev/random | tr -d '\n')
 * `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_CLIENT_KEEP_ALIVE_TIMEOUT`: the maximum duration (in seconds) to wait for the next request before closing the HTTP client connection. The HTTP client is used to download source images. When set to `0`, keep-alive is disabled. Default: `90`
 * `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. Requests that exceed this limit are put in the queue. Default: the number of CPU cores multiplied by two
 * `IMGPROXY_REQUESTS_QUEUE_SIZE`: the maximum number of image requests that can be put in the queue. Requests that exceed this limit are rejected with `429` HTTP status. When set to `0`, the requests queue is unlimited. Default: `0`

+ 10 - 7
imagedata/download.go

@@ -5,7 +5,6 @@ import (
 	"crypto/tls"
 	"fmt"
 	"io/ioutil"
-	"net"
 	"net/http"
 	"net/http/cookiejar"
 	"time"
@@ -50,12 +49,16 @@ func (e *ErrorNotModified) Error() string {
 }
 
 func initDownloading() error {
-	transport := &http.Transport{
-		Proxy:               http.ProxyFromEnvironment,
-		MaxIdleConns:        config.Concurrency,
-		MaxIdleConnsPerHost: config.Concurrency,
-		DisableCompression:  true,
-		DialContext:         (&net.Dialer{KeepAlive: 600 * time.Second}).DialContext,
+	transport := http.DefaultTransport.(*http.Transport).Clone()
+	transport.DisableCompression = true
+
+	if config.ClientKeepAliveTimeout > 0 {
+		transport.MaxIdleConns = config.Concurrency
+		transport.MaxIdleConnsPerHost = config.Concurrency
+		transport.IdleConnTimeout = time.Duration(config.ClientKeepAliveTimeout) * time.Second
+	} else {
+		transport.MaxIdleConns = 0
+		transport.MaxIdleConnsPerHost = 0
 	}
 
 	if config.IgnoreSslVerification {