From 3d70018179172b897871224c787b214e2866d65c Mon Sep 17 00:00:00 2001 From: Lukasz Raczylo Date: Tue, 24 Oct 2023 10:40:17 +0100 Subject: [PATCH] Add configurable timeout for queries. --- README.md | 1 + main.go | 3 ++- proxy.go | 8 ++++---- struct_config.go | 1 + 4 files changed, 8 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index db8690b..c5f855b 100644 --- a/README.md +++ b/README.md @@ -122,6 +122,7 @@ In this case, both proxy and websockets will be available under the `/v1/graphql | `ENABLE_API` | Enable the monitoring API | `false` | | `API_PORT` | The port to expose the monitoring API | `9090` | | `BANNED_USERS_FILE` | The path to the file with banned users | `/go/src/app/banned_users.json` | +| `PROXIED_CLIENT_TIMEOUT` | The timeout for the proxied client in seconds | `120` | ### Speed diff --git a/main.go b/main.go index eaa0506..cc3c3a8 100644 --- a/main.go +++ b/main.go @@ -50,7 +50,8 @@ func parseConfig() { } return strings.Split(urls, ",") }() - c.Client.FastProxyClient = createFasthttpClient() + c.Client.ClientTimeout = envutil.GetInt("PROXIED_CLIENT_TIMEOUT", 120) + c.Client.FastProxyClient = createFasthttpClient(c.Client.ClientTimeout) c.Server.EnableApi = envutil.GetBool("ENABLE_API", false) c.Server.ApiPort = envutil.GetInt("API_PORT", 9090) c.Api.BannedUsersFile = envutil.Getenv("BANNED_USERS_FILE", "/go/src/app/banned_users.json") diff --git a/proxy.go b/proxy.go index 8f5b4e5..29c165a 100644 --- a/proxy.go +++ b/proxy.go @@ -11,7 +11,7 @@ import ( "github.com/valyala/fasthttp" ) -func createFasthttpClient() *fasthttp.Client { +func createFasthttpClient(timeout int) *fasthttp.Client { return &fasthttp.Client{ Name: "graphql_proxy", NoDefaultUserAgentHeader: true, @@ -19,9 +19,9 @@ func createFasthttpClient() *fasthttp.Client { InsecureSkipVerify: true, }, MaxConnsPerHost: 100, - MaxIdleConnDuration: 2 * time.Minute, - ReadTimeout: time.Second * 10, - WriteTimeout: time.Second * 10, + MaxIdleConnDuration: time.Duration(timeout*5) * time.Minute, + ReadTimeout: time.Second * time.Duration(timeout), + WriteTimeout: time.Second * time.Duration(timeout), DisableHeaderNamesNormalizing: true, } } diff --git a/struct_config.go b/struct_config.go index 7c4f961..5c5a4ae 100644 --- a/struct_config.go +++ b/struct_config.go @@ -34,6 +34,7 @@ type config struct { GQLClient *graphql.BaseClient FastProxyClient *fasthttp.Client proxy string + ClientTimeout int } Cache struct {