From 6f25b6e0ac6b5237e8c0124b421d345cfd9a8b20 Mon Sep 17 00:00:00 2001 From: Arkadiusz Szarama Date: Tue, 1 Dec 2026 13:47:47 +0100 Subject: [PATCH 1/6] Let the agent's HTTP read timeouts be set with environment variables --- README.relay.md | 15 ++++-- agent/config/config.go | 31 ++++++++++++ agent/config/config_test.go | 24 ++++++++++ agent/server/api/cortex_api_proxy_handler.go | 11 ++--- .../api/cortex_api_proxy_read_timeout_test.go | 47 +++++++++++++++++++ agent/server/http/module.go | 15 +++--- agent/server/snykbroker/reflector.go | 12 ++--- 7 files changed, 129 insertions(+), 26 deletions(-) diff --git a/README.relay.md b/README.relay.md index 7575416a..9e9f7537 100644 --- a/README.relay.md +++ b/README.relay.md @@ -264,11 +264,16 @@ relay reflector allows 30s, because the broker writes a request only when its event loop gets to it, and that loop stalls for two to three seconds while it sends a large response. -| Server | Read bound | -|--------|------------| -| Relay reflector | 30s | -| Webhook server | 30s | -| Main server | 1s (30s for a `/cortex-api/` request body) | +| Server | Read bound | Setting | +|--------|------------|---------| +| Relay reflector | 30s | `RELAY_REFLECTOR_READ_TIMEOUT` | +| Webhook server | 30s | `WEBHOOK_READ_TIMEOUT` | +| Main server, `/cortex-api/` request body | 30s | `CORTEX_API_PROXY_READ_TIMEOUT` | +| Main server, other endpoints | 1s | none | + +Each setting takes a Go duration, such as `45s` or `2m`. A value that is not +a duration, or is not greater than zero, stops the agent at startup. Keep the +relay reflector value under the relay dispatcher's 60-second request timeout. ## How it works diff --git a/agent/config/config.go b/agent/config/config.go index 1106916e..8b621b69 100644 --- a/agent/config/config.go +++ b/agent/config/config.go @@ -95,6 +95,12 @@ type AgentConfig struct { ReflectorWebSocketUpgrade bool RelayIdleTimeout time.Duration + // Read timeouts bound how long a server takes to read a request, counted + // from when the connection is accepted. Zero means DefaultReadTimeout. + RelayReflectorReadTimeout time.Duration + WebhookReadTimeout time.Duration + CortexApiProxyReadTimeout time.Duration + // RelayMode selects the tunnel implementation: "snyk-broker" or // "grpc-tunnel". NewAgentEnvConfig sets it only from AXON_RELAY_TRANSPORT // (or RELAY_MODE) or ForcedRelayMode, and leaves it empty otherwise, so @@ -245,6 +251,27 @@ func envFirst(names ...string) string { return "" } +// DefaultReadTimeout is the read timeout of the relay reflector, the webhook +// server, and the Cortex API proxy when no setting overrides it. +const DefaultReadTimeout = 30 * time.Second + +// positiveDurationEnv reads a duration from the named variable. Zero is +// rejected because it would remove the bound. +func positiveDurationEnv(name string, def time.Duration) time.Duration { + v := os.Getenv(name) + if v == "" { + return def + } + d, err := time.ParseDuration(v) + if err != nil { + panic(fmt.Sprintf("%s: %v", name, err)) + } + if d <= 0 { + panic(fmt.Sprintf("%s must be greater than zero, got %q", name, v)) + } + return d +} + func NewAgentEnvConfig() AgentConfig { baseUrl := os.Getenv("CORTEX_API_BASE_URL") @@ -412,6 +439,10 @@ func NewAgentEnvConfig() AgentConfig { cfg.RelayIdleTimeout = rit } + cfg.RelayReflectorReadTimeout = positiveDurationEnv("RELAY_REFLECTOR_READ_TIMEOUT", DefaultReadTimeout) + cfg.WebhookReadTimeout = positiveDurationEnv("WEBHOOK_READ_TIMEOUT", DefaultReadTimeout) + cfg.CortexApiProxyReadTimeout = positiveDurationEnv("CORTEX_API_PROXY_READ_TIMEOUT", DefaultReadTimeout) + // AXON_RELAY_TRANSPORT is the documented name (design doc ยง10); // RELAY_MODE is accepted as an alias. No default here: an unset mode // must stay distinguishable from a customer choice. See RelayMode. diff --git a/agent/config/config_test.go b/agent/config/config_test.go index 5903affc..a2a17b88 100644 --- a/agent/config/config_test.go +++ b/agent/config/config_test.go @@ -130,3 +130,27 @@ func TestEnvConfig_MaxRequestTimeout(t *testing.T) { t.Setenv("AXON_GRPC_TUNNEL_MAX_REQUEST_TIMEOUT", "90s") assert.Equal(t, "1m30s", NewAgentEnvConfig().MaxRequestTimeout.String()) } + +func TestEnvConfig_ReadTimeouts(t *testing.T) { + cfg := NewAgentEnvConfig() + assert.Equal(t, DefaultReadTimeout, cfg.RelayReflectorReadTimeout) + assert.Equal(t, DefaultReadTimeout, cfg.WebhookReadTimeout) + assert.Equal(t, DefaultReadTimeout, cfg.CortexApiProxyReadTimeout) + + t.Setenv("RELAY_REFLECTOR_READ_TIMEOUT", "45s") + t.Setenv("WEBHOOK_READ_TIMEOUT", "2m") + t.Setenv("CORTEX_API_PROXY_READ_TIMEOUT", "90s") + cfg = NewAgentEnvConfig() + assert.Equal(t, "45s", cfg.RelayReflectorReadTimeout.String()) + assert.Equal(t, "2m0s", cfg.WebhookReadTimeout.String()) + assert.Equal(t, "1m30s", cfg.CortexApiProxyReadTimeout.String()) +} + +func TestEnvConfig_ReadTimeoutRejectsBadValues(t *testing.T) { + for _, v := range []string{"0s", "-5s", "thirty"} { + t.Run(v, func(t *testing.T) { + t.Setenv("WEBHOOK_READ_TIMEOUT", v) + assert.Panics(t, func() { NewAgentEnvConfig() }) + }) + } +} diff --git a/agent/server/api/cortex_api_proxy_handler.go b/agent/server/api/cortex_api_proxy_handler.go index 64610efa..437e75e1 100644 --- a/agent/server/api/cortex_api_proxy_handler.go +++ b/agent/server/api/cortex_api_proxy_handler.go @@ -2,6 +2,7 @@ package api import ( "bytes" + "cmp" "compress/gzip" "fmt" "io" @@ -25,11 +26,6 @@ import ( const cortexApiRoot = "cortex-api" const cortexApiPathRoot = "/cortex-api/" -// apiProxyReadTimeout bounds reading a proxied request body. The main server -// keeps a short read timeout for its own fast endpoints, and a large body for -// the Cortex API can take longer to arrive. -const apiProxyReadTimeout = 30 * time.Second - func NewApiProxyHandler(config config.AgentConfig, logger *zap.Logger, httpTransport *http.Transport) cortex_http.RegisterableHandler { targetURL, err := url.Parse(config.CortexApiBaseUrl) if err != nil { @@ -72,7 +68,10 @@ func (a *apiProxyHandler) RegisterRoutes(mux *mux.Router) error { } func (a *apiProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { - _ = http.NewResponseController(w).SetReadDeadline(time.Now().Add(apiProxyReadTimeout)) + // The main server keeps a short read timeout for its own fast endpoints, + // and a large body for the Cortex API can take longer to arrive. + readTimeout := cmp.Or(a.config.CortexApiProxyReadTimeout, config.DefaultReadTimeout) + _ = http.NewResponseController(w).SetReadDeadline(time.Now().Add(readTimeout)) if strings.HasPrefix(r.URL.Path, cortexApiPathRoot) { r.URL.Path = r.URL.Path[len(cortexApiPathRoot):] diff --git a/agent/server/api/cortex_api_proxy_read_timeout_test.go b/agent/server/api/cortex_api_proxy_read_timeout_test.go index 341c487e..e76f53aa 100644 --- a/agent/server/api/cortex_api_proxy_read_timeout_test.go +++ b/agent/server/api/cortex_api_proxy_read_timeout_test.go @@ -52,3 +52,50 @@ func TestServeHTTP_ReadsLateBodyPastServerReadTimeout(t *testing.T) { defer resp.Body.Close() require.Equal(t, http.StatusAccepted, resp.StatusCode) } + +// A shorter configured value must replace the default, so a body that arrives +// after it never reaches the Cortex API. +func TestServeHTTP_UsesConfiguredReadTimeout(t *testing.T) { + reached := make(chan struct{}, 1) + backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if body, err := io.ReadAll(r.Body); err == nil && string(body) == "hello" { + reached <- struct{}{} + } + w.WriteHeader(http.StatusAccepted) + })) + defer backend.Close() + + cfg := config.AgentConfig{ + CortexApiBaseUrl: backend.URL, + CortexApiToken: "test_token", + CortexApiProxyReadTimeout: 200 * time.Millisecond, + } + srv := cortex_http.NewHttpServer(cortex_http.HttpServerParams{ + Logger: zap.NewNop(), + Config: cfg, + Handlers: []cortex_http.RegisterableHandler{NewApiProxyHandler(cfg, zap.NewNop(), nil)}, + }) + port, err := srv.Start() + require.NoError(t, err) + defer srv.Close() + + conn, err := net.Dial("tcp", fmt.Sprintf("127.0.0.1:%d", port)) + require.NoError(t, err) + defer conn.Close() + + _, err = fmt.Fprintf(conn, "POST /cortex-api/test HTTP/1.1\r\nHost: localhost\r\nContent-Length: 5\r\n\r\n") + require.NoError(t, err) + time.Sleep(600 * time.Millisecond) + _, _ = io.WriteString(conn, "hello") + + resp, err := http.ReadResponse(bufio.NewReader(conn), nil) + if err == nil { + resp.Body.Close() + require.NotEqual(t, http.StatusAccepted, resp.StatusCode) + } + select { + case <-reached: + t.Fatal("the body reached the Cortex API after the configured read timeout") + default: + } +} diff --git a/agent/server/http/module.go b/agent/server/http/module.go index 4df42070..c1b3036c 100644 --- a/agent/server/http/module.go +++ b/agent/server/http/module.go @@ -1,10 +1,10 @@ package http import ( + "cmp" "context" - "time" - "github.com/cortexapps/axon/config" + cfg "github.com/cortexapps/axon/config" "github.com/cortexapps/axon/server/cron" "github.com/cortexapps/axon/server/handler" "github.com/prometheus/client_golang/prometheus" @@ -18,11 +18,7 @@ var Module = fx.Module("handler", fx.Invoke(createWebhookHttpServer), ) -// webhookReadTimeout bounds reading a webhook delivery. Senders are outside -// the agent's network and may upload large payloads slowly. -const webhookReadTimeout = 30 * time.Second - -func createWebhookHttpServer(lifecycle fx.Lifecycle, config config.AgentConfig, logger *zap.Logger, handlerManager handler.Manager, registry *prometheus.Registry) Server { +func createWebhookHttpServer(lifecycle fx.Lifecycle, config cfg.AgentConfig, logger *zap.Logger, handlerManager handler.Manager, registry *prometheus.Registry) Server { params := HttpServerParams{ Logger: logger, @@ -32,7 +28,10 @@ func createWebhookHttpServer(lifecycle fx.Lifecycle, config config.AgentConfig, }, Config: config, } - httpServer := NewHttpServer(params, WithName("webhook"), WithPort(config.WebhookServerPort), WithReadTimeout(webhookReadTimeout)) + // Webhook senders are outside the agent's network and may upload large + // payloads slowly. + readTimeout := cmp.Or(config.WebhookReadTimeout, cfg.DefaultReadTimeout) + httpServer := NewHttpServer(params, WithName("webhook"), WithPort(config.WebhookServerPort), WithReadTimeout(readTimeout)) lifecycle.Append(fx.Hook{ OnStart: func(ctx context.Context) error { diff --git a/agent/server/snykbroker/reflector.go b/agent/server/snykbroker/reflector.go index 5fcd3599..2515164d 100644 --- a/agent/server/snykbroker/reflector.go +++ b/agent/server/snykbroker/reflector.go @@ -1,6 +1,7 @@ package snykbroker import ( + "cmp" "context" "fmt" "hash/fnv" @@ -46,12 +47,6 @@ type RegistrationReflectorParams struct { Config config.AgentConfig } -// reflectorReadTimeout bounds reading a request from the broker. The broker -// opens a connection and writes its request only when its event loop gets to -// it, and that loop stalls for two to three seconds while it sends a large -// response. It stays under the relay dispatcher's 60-second request timeout. -const reflectorReadTimeout = 30 * time.Second - func NewRegistrationReflector(p RegistrationReflectorParams) *RegistrationReflector { httpParams := cortexHttp.HttpServerParams{ @@ -68,7 +63,10 @@ func NewRegistrationReflector(p RegistrationReflectorParams) *RegistrationReflec server := cortexHttp.NewHttpServer(httpParams, cortexHttp.WithName("relay-reflector"), cortexHttp.WithLoopbackOnly(), - cortexHttp.WithReadTimeout(reflectorReadTimeout), + // The broker opens a connection and writes its request only when its + // event loop gets to it, and that loop stalls for seconds while it + // sends a large response. + cortexHttp.WithReadTimeout(cmp.Or(p.Config.RelayReflectorReadTimeout, config.DefaultReadTimeout)), ) rr := &RegistrationReflector{ From 3a852e213f44b7078333a13d3df7f0dba8f2ffe4 Mon Sep 17 00:00:00 2001 From: Arkadiusz Szarama Date: Tue, 1 Dec 2026 13:50:00 +0100 Subject: [PATCH 2/6] Keep the config import name and widen the proxy test margin --- .../api/cortex_api_proxy_read_timeout_test.go | 4 ++-- agent/server/http/module.go | 14 +++++++------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/agent/server/api/cortex_api_proxy_read_timeout_test.go b/agent/server/api/cortex_api_proxy_read_timeout_test.go index e76f53aa..56c77ae8 100644 --- a/agent/server/api/cortex_api_proxy_read_timeout_test.go +++ b/agent/server/api/cortex_api_proxy_read_timeout_test.go @@ -68,7 +68,7 @@ func TestServeHTTP_UsesConfiguredReadTimeout(t *testing.T) { cfg := config.AgentConfig{ CortexApiBaseUrl: backend.URL, CortexApiToken: "test_token", - CortexApiProxyReadTimeout: 200 * time.Millisecond, + CortexApiProxyReadTimeout: 100 * time.Millisecond, } srv := cortex_http.NewHttpServer(cortex_http.HttpServerParams{ Logger: zap.NewNop(), @@ -85,7 +85,7 @@ func TestServeHTTP_UsesConfiguredReadTimeout(t *testing.T) { _, err = fmt.Fprintf(conn, "POST /cortex-api/test HTTP/1.1\r\nHost: localhost\r\nContent-Length: 5\r\n\r\n") require.NoError(t, err) - time.Sleep(600 * time.Millisecond) + time.Sleep(time.Second) _, _ = io.WriteString(conn, "hello") resp, err := http.ReadResponse(bufio.NewReader(conn), nil) diff --git a/agent/server/http/module.go b/agent/server/http/module.go index c1b3036c..1b9e88f3 100644 --- a/agent/server/http/module.go +++ b/agent/server/http/module.go @@ -4,7 +4,7 @@ import ( "cmp" "context" - cfg "github.com/cortexapps/axon/config" + "github.com/cortexapps/axon/config" "github.com/cortexapps/axon/server/cron" "github.com/cortexapps/axon/server/handler" "github.com/prometheus/client_golang/prometheus" @@ -18,25 +18,25 @@ var Module = fx.Module("handler", fx.Invoke(createWebhookHttpServer), ) -func createWebhookHttpServer(lifecycle fx.Lifecycle, config cfg.AgentConfig, logger *zap.Logger, handlerManager handler.Manager, registry *prometheus.Registry) Server { +func createWebhookHttpServer(lifecycle fx.Lifecycle, agentConfig config.AgentConfig, logger *zap.Logger, handlerManager handler.Manager, registry *prometheus.Registry) Server { params := HttpServerParams{ Logger: logger, Registry: registry, Handlers: []RegisterableHandler{ - NewWebhookHandler(config, logger, handlerManager, registry), + NewWebhookHandler(agentConfig, logger, handlerManager, registry), }, - Config: config, + Config: agentConfig, } // Webhook senders are outside the agent's network and may upload large // payloads slowly. - readTimeout := cmp.Or(config.WebhookReadTimeout, cfg.DefaultReadTimeout) - httpServer := NewHttpServer(params, WithName("webhook"), WithPort(config.WebhookServerPort), WithReadTimeout(readTimeout)) + readTimeout := cmp.Or(agentConfig.WebhookReadTimeout, config.DefaultReadTimeout) + httpServer := NewHttpServer(params, WithName("webhook"), WithPort(agentConfig.WebhookServerPort), WithReadTimeout(readTimeout)) lifecycle.Append(fx.Hook{ OnStart: func(ctx context.Context) error { _, err := httpServer.Start() - logger.Info("Webhook server started", zap.Int("port", config.WebhookServerPort), zap.Error(err)) + logger.Info("Webhook server started", zap.Int("port", agentConfig.WebhookServerPort), zap.Error(err)) return err }, From 2bb05c250c438ddfbf4572bafb3f73e4b2821b37 Mon Sep 17 00:00:00 2001 From: Arkadiusz Szarama Date: Fri, 4 Dec 2026 13:21:19 +0100 Subject: [PATCH 3/6] Say when the Cortex API proxy read timeout starts counting --- README.relay.md | 3 ++- agent/config/config.go | 6 ++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/README.relay.md b/README.relay.md index 9e9f7537..d62f4859 100644 --- a/README.relay.md +++ b/README.relay.md @@ -260,7 +260,8 @@ exists, takes precedence. The agent's own HTTP servers bound how long they take to read an incoming request, headers and body, counted from when the connection is accepted. The -relay reflector allows 30s, because the broker writes a request only when its +`/cortex-api/` bound is different: it covers only the body and counts from +when the proxy handler starts, after the headers are read. The relay reflector allows 30s, because the broker writes a request only when its event loop gets to it, and that loop stalls for two to three seconds while it sends a large response. diff --git a/agent/config/config.go b/agent/config/config.go index 8b621b69..e2e2a1d5 100644 --- a/agent/config/config.go +++ b/agent/config/config.go @@ -95,8 +95,10 @@ type AgentConfig struct { ReflectorWebSocketUpgrade bool RelayIdleTimeout time.Duration - // Read timeouts bound how long a server takes to read a request, counted - // from when the connection is accepted. Zero means DefaultReadTimeout. + // Read timeouts bound how long a server takes to read a request. The + // reflector and webhook bounds count from when the connection is accepted. + // The Cortex API proxy bound counts from when its handler starts, after the + // headers are read. Zero means DefaultReadTimeout. RelayReflectorReadTimeout time.Duration WebhookReadTimeout time.Duration CortexApiProxyReadTimeout time.Duration From 6ee8afe66b8fc1bda6a12a2d6050ba18b805b201 Mon Sep 17 00:00:00 2001 From: Arkadiusz Szarama Date: Fri, 4 Dec 2026 15:30:45 +0100 Subject: [PATCH 4/6] Clear the Cortex API proxy read deadline once the body is read The deadline was still set when the upstream call ran. For a request without a body, net/http's background read uses it, and when it fires it cancels the request context, so a slow upstream failed. Also correct the read timeout text in README.relay.md and config.go. --- README.relay.md | 12 ++++--- agent/config/config.go | 7 ++-- agent/server/api/cortex_api_proxy_handler.go | 4 +++ .../api/cortex_api_proxy_read_timeout_test.go | 36 +++++++++++++++++++ 4 files changed, 52 insertions(+), 7 deletions(-) diff --git a/README.relay.md b/README.relay.md index d62f4859..97b1b2df 100644 --- a/README.relay.md +++ b/README.relay.md @@ -260,10 +260,14 @@ exists, takes precedence. The agent's own HTTP servers bound how long they take to read an incoming request, headers and body, counted from when the connection is accepted. The -`/cortex-api/` bound is different: it covers only the body and counts from -when the proxy handler starts, after the headers are read. The relay reflector allows 30s, because the broker writes a request only when its -event loop gets to it, and that loop stalls for two to three seconds while it -sends a large response. +relay reflector allows 30s by default, because the broker writes a request +only when its event loop gets to it, and that loop stalls for two to three +seconds while it sends a large response. + +The `/cortex-api/` bound is different: it covers only the request body after +the headers, and counts from when the proxy handler starts. The wait for the +Cortex API response is not bounded by it. The `/cortex-api/` headers keep the +main server's 1s bound, which `CORTEX_API_PROXY_READ_TIMEOUT` does not change. | Server | Read bound | Setting | |--------|------------|---------| diff --git a/agent/config/config.go b/agent/config/config.go index e2e2a1d5..38ea42a5 100644 --- a/agent/config/config.go +++ b/agent/config/config.go @@ -97,8 +97,9 @@ type AgentConfig struct { // Read timeouts bound how long a server takes to read a request. The // reflector and webhook bounds count from when the connection is accepted. - // The Cortex API proxy bound counts from when its handler starts, after the - // headers are read. Zero means DefaultReadTimeout. + // The Cortex API proxy bound covers only the request body after the + // headers, and counts from when its handler starts. The headers keep the + // main server's 1s bound. Zero means DefaultReadTimeout. RelayReflectorReadTimeout time.Duration WebhookReadTimeout time.Duration CortexApiProxyReadTimeout time.Duration @@ -258,7 +259,7 @@ func envFirst(names ...string) string { const DefaultReadTimeout = 30 * time.Second // positiveDurationEnv reads a duration from the named variable. Zero is -// rejected because it would remove the bound. +// rejected because it would silently mean the 30s default. func positiveDurationEnv(name string, def time.Duration) time.Duration { v := os.Getenv(name) if v == "" { diff --git a/agent/server/api/cortex_api_proxy_handler.go b/agent/server/api/cortex_api_proxy_handler.go index 437e75e1..681b233f 100644 --- a/agent/server/api/cortex_api_proxy_handler.go +++ b/agent/server/api/cortex_api_proxy_handler.go @@ -116,6 +116,10 @@ func (a *apiProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } r.Body.Close() } + // The bound is for the body only. net/http's background read of the + // connection also uses the deadline, and when it fires it cancels + // r.Context(), which the upstream call uses. + _ = http.NewResponseController(w).SetReadDeadline(time.Time{}) for { diff --git a/agent/server/api/cortex_api_proxy_read_timeout_test.go b/agent/server/api/cortex_api_proxy_read_timeout_test.go index 56c77ae8..9600dc13 100644 --- a/agent/server/api/cortex_api_proxy_read_timeout_test.go +++ b/agent/server/api/cortex_api_proxy_read_timeout_test.go @@ -7,6 +7,7 @@ import ( "net" "net/http" "net/http/httptest" + "strings" "testing" "time" @@ -99,3 +100,38 @@ func TestServeHTTP_UsesConfiguredReadTimeout(t *testing.T) { default: } } + +// The bound covers only the request body, so an upstream that answers after it +// must still reach the caller. The request without a body is the one at risk: +// net/http's background read is already running when the deadline is set. +func TestServeHTTP_SlowUpstreamPastReadTimeout(t *testing.T) { + backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + time.Sleep(500 * time.Millisecond) + w.WriteHeader(http.StatusAccepted) + })) + defer backend.Close() + + cfg := config.AgentConfig{ + CortexApiBaseUrl: backend.URL, + CortexApiToken: "test_token", + CortexApiProxyReadTimeout: 100 * time.Millisecond, + } + srv := cortex_http.NewHttpServer(cortex_http.HttpServerParams{ + Logger: zap.NewNop(), + Config: cfg, + Handlers: []cortex_http.RegisterableHandler{NewApiProxyHandler(cfg, zap.NewNop(), nil)}, + }) + port, err := srv.Start() + require.NoError(t, err) + defer srv.Close() + + url := fmt.Sprintf("http://127.0.0.1:%d/cortex-api/test", port) + for name, body := range map[string]io.Reader{"no body": nil, "body": strings.NewReader("hello")} { + req, err := http.NewRequest(http.MethodPost, url, body) + require.NoError(t, err) + resp, err := http.DefaultClient.Do(req) + require.NoError(t, err) + resp.Body.Close() + require.Equal(t, http.StatusAccepted, resp.StatusCode, name) + } +} From 6f4ad16562f6054811c584b7aec4992ccb63b76c Mon Sep 17 00:00:00 2001 From: Arkadiusz Szarama Date: Fri, 4 Dec 2026 17:18:40 +0100 Subject: [PATCH 5/6] Shorten the read-timeout docs to what a customer changes --- README.relay.md | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/README.relay.md b/README.relay.md index f73381fc..d0bb6d0a 100644 --- a/README.relay.md +++ b/README.relay.md @@ -238,22 +238,19 @@ exists, takes precedence. | Idle connection kept in the pool | 90s | | Relay registration (`/register`) | 30s | -The agent's own HTTP servers bound how long they take to read an incoming -request, headers and body, counted from when the connection is accepted. - -The `/cortex-api/` bound is different: it covers only the request body after -the headers, and counts from when the proxy handler starts. The wait for the -Cortex API response is not bounded by it. The `/cortex-api/` headers keep the -main server's 1s bound, which `CORTEX_API_PROXY_READ_TIMEOUT` does not change. - -| Server | Read bound | Setting | -|--------|------------|---------| -| Webhook server | 30s | `WEBHOOK_READ_TIMEOUT` | -| Main server, `/cortex-api/` request body | 30s | `CORTEX_API_PROXY_READ_TIMEOUT` | -| Main server, other endpoints | 1s | none | - -Each setting takes a Go duration, such as `45s` or `2m`. A value that is not -a duration, or is not greater than zero, stops the agent at startup. +#### Read timeouts + +The agent gives each incoming request a limited time to arrive. Raise a limit +when large or slow requests fail before they are complete. + +| Setting | Default | Raise it when | +|---------|---------|---------------| +| `WEBHOOK_READ_TIMEOUT` | 30s | Large or slow webhook deliveries to the agent fail. | +| `CORTEX_API_PROXY_READ_TIMEOUT` | 30s | Large requests to the Cortex API through the agent fail. | + +Set the value as an environment variable on the agent container, as a Go +duration such as `45s` or `2m`. The agent does not start if the value is not +a valid duration greater than zero. ## How it works From e406e67e4f349f43e0627e6cf9f19122f076d25f Mon Sep 17 00:00:00 2001 From: Arkadiusz Szarama Date: Fri, 4 Dec 2026 18:41:34 +0100 Subject: [PATCH 6/6] Move the read-timeout docs to README.md, where serve users read --- README.md | 14 ++++++++++++++ README.relay.md | 14 -------------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index d152b736..72745bcc 100644 --- a/README.md +++ b/README.md @@ -182,6 +182,20 @@ response = ctx.cortex_api_call( When you are ready to invoke Cortex APIs, set the `CORTEX_API_TOKEN` enviornment variable and omit `DRYRUN`. For on-premise installs you'll also need to add `CORTEX_API_BASE_URL` which is the DNS name of your cortex instance eg `https://api.cortex.internal` +### Read timeouts + +The agent gives each incoming request a limited time to arrive. Raise a limit +when large or slow requests fail before they are complete. + +| Setting | Default | Raise it when | +|---------|---------|---------------| +| `WEBHOOK_READ_TIMEOUT` | 30s | Large or slow webhook deliveries to port 8081 fail. | +| `CORTEX_API_PROXY_READ_TIMEOUT` | 30s | Large requests to `http://localhost/cortex-api` fail. | + +Set the value as an environment variable on the agent container, as a Go +duration such as `45s` or `2m`. The agent does not start if the value is not +a valid duration greater than zero. + ## Handling Proxy and TLS The agent supports the following environment variables to handle proxy and TLS: diff --git a/README.relay.md b/README.relay.md index d0bb6d0a..150e2436 100644 --- a/README.relay.md +++ b/README.relay.md @@ -238,20 +238,6 @@ exists, takes precedence. | Idle connection kept in the pool | 90s | | Relay registration (`/register`) | 30s | -#### Read timeouts - -The agent gives each incoming request a limited time to arrive. Raise a limit -when large or slow requests fail before they are complete. - -| Setting | Default | Raise it when | -|---------|---------|---------------| -| `WEBHOOK_READ_TIMEOUT` | 30s | Large or slow webhook deliveries to the agent fail. | -| `CORTEX_API_PROXY_READ_TIMEOUT` | 30s | Large requests to the Cortex API through the agent fail. | - -Set the value as an environment variable on the agent container, as a Go -duration such as `45s` or `2m`. The agent does not start if the value is not -a valid duration greater than zero. - ## How it works The agent opens a gRPC tunnel from the internal network to the cloud-hosted