Skip to content

[TT-12339] [BE] Implement sub-second timeout for the Enforced Timeout middleware#8151

Merged
radkrawczyk merged 31 commits into
masterfrom
TT-12339-be-implement-sub-second-timeout-for-the-enforced-timeout-middleware
May 15, 2026
Merged

[TT-12339] [BE] Implement sub-second timeout for the Enforced Timeout middleware#8151
radkrawczyk merged 31 commits into
masterfrom
TT-12339-be-implement-sub-second-timeout-for-the-enforced-timeout-middleware

Conversation

@MaciekMis

@MaciekMis MaciekMis commented Apr 24, 2026

Copy link
Copy Markdown
Contributor

Description

Related Issue

Motivation and Context

How This Has Been Tested

Screenshots (if appropriate)

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Refactoring or add test (improvements in base code or adds test coverage to functionality)

Checklist

  • I ensured that the documentation is up to date
  • I explained why this PR updates go.mod in detail with reasoning why it's required
  • I would like a code coverage CI quality gate exception and have explained why

Ticket Details

TT-12339
Status In Test
Summary [BE] Implement sub-second timeout for the Enforced Timeout middleware

Generated at: 2026-05-15 11:50:18

@probelabs

probelabs Bot commented Apr 24, 2026

Copy link
Copy Markdown
Contributor

This pull request introduces sub-second precision for the Enforced Timeout middleware. It adds a new timeout_duration field that accepts duration strings (e.g., "1.5s", "500ms"), allowing for more granular control over upstream request timeouts. The legacy timeout field, which accepts an integer in seconds, is deprecated but retained for backward compatibility.

To ensure older systems continue to function, when timeout_duration is set, the legacy timeout field is automatically populated with the duration rounded up to the nearest second. The core change is in the gateway's reverse proxy, which now uses time.Duration with context.WithTimeout to enable precise, sub-second timeout enforcement.

Files Changed Analysis

  • Total Changes: 12 files modified, with 229 additions and 28 deletions.
  • API Definitions (apidef/api_definitions.go, apidef/oas/middleware.go): The HardTimeoutMeta and EnforceTimeout structs are updated to include the new duration-based fields (TimeoutDuration, Timeout). Conversion logic is added to handle backward and forward compatibility.
  • Gateway Logic (gateway/reverse_proxy.go): The primary change is in GetHardTimeoutEnforcedSettings (renamed from CheckHardTimeoutEnforced), which now returns a time.Duration. This allows WrappedServeHTTP to leverage context.WithTimeout with sub-second precision.
  • Testing (gateway/reverse_proxy_test.go): Extensive tests (121 new lines) have been added to validate the new sub-second timeout functionality across various scenarios.
  • Schema & Validation (apidef/oas/schema/*.json, apidef/schema.json, apidef/validator.go): The OpenAPI and internal JSON schemas are updated to recognize and validate the new duration-based timeout fields.
  • Utilities (internal/time/duration.go): A SecondsCeil helper function has been added to facilitate the rounding-up logic for backward compatibility.

Architecture & Impact Assessment

  • What this PR accomplishes: It enhances the Enforced Timeout middleware to support sub-second precision, giving API designers more granular control over upstream request timeouts.
  • Key technical changes introduced:
    1. A new timeout_duration field (string) is added to the hard_timeouts configuration in the API definition.
    2. The gateway's reverse proxy now uses time.Duration for timeout calculations, enabling sub-second precision.
    3. Backward compatibility is preserved: if timeout_duration is set, the legacy timeout (integer) field is populated with the duration rounded up to the nearest second.
  • Affected system components:
    • API Gateway: The core request-handling logic in the reverse proxy is directly impacted.
    • API Definition: The data structure is updated. While this change is non-breaking for the gateway, other components like the Tyk Dashboard and Tyk Operator will need to be updated to expose the new field in their respective UIs and controllers.

Request Flow with Sub-Second Timeout

sequenceDiagram
    participant Client
    participant Tyk Gateway
    participant Upstream Service

    Client->>+Tyk Gateway: HTTP Request
    Tyk Gateway->>Tyk Gateway: GetHardTimeoutEnforcedSettings()
    Note right of Tyk Gateway: Reads API spec for the matching path,<br/>retrieves timeout_duration (e.g., "500ms")
    Tyk Gateway->>Tyk Gateway: context.WithTimeout(req.Context(), 500ms)
    Tyk Gateway->>+Upstream Service: Forwards request with new timed context
    alt Upstream takes > 500ms
        Upstream Service-->>-Tyk Gateway: (No response within 500ms)
        Note left of Upstream Service: Gateway's context deadline is exceeded
        Tyk Gateway-->>-Client: HTTP 504 Gateway Timeout
    else Upstream takes < 500ms
        Upstream Service-->>-Tyk Gateway: HTTP 200 OK
        Tyk Gateway-->>-Client: HTTP 200 OK
    end
Loading

Scope Discovery & Context Expansion

  • The primary impact is on the gateway's runtime and the API definition schema. The feature is fully functional within the gateway, but user-facing configuration will be limited to raw API definition edits until the Tyk Dashboard and Tyk Operator are updated to support the new timeout_duration field.
  • The backward compatibility logic (rounding up to the nearest second) is a critical detail. This ensures that older systems reading the deprecated integer field will enforce a timeout that is at least as long as the intended duration, preventing unexpected failures. However, it also means sub-second precision can be lost if a configuration is round-tripped through an older system that only supports the integer field.
  • The OpenAPI schema in apidef/oas/schema/x-tyk-api-gateway.json has been relaxed by removing value from the required list. This is necessary to allow the new timeout field to be used instead. However, the validation in apidef/validator.go only checks for negative timeouts, not whether a positive timeout is set when the middleware is enabled. This could allow a configuration where the middleware is enabled with a timeout of zero, rendering it ineffective.
Metadata
  • Review Effort: 3 / 5
  • Primary Label: enhancement

Powered by Visor from Probelabs

Last updated: 2026-05-15T11:51:47.727Z | Triggered by: pr_updated | Commit: b7f550b

💡 TIP: You can chat with Visor using /visor ask <your question>

@probelabs

probelabs Bot commented Apr 24, 2026

Copy link
Copy Markdown
Contributor

Security Issues (1)

Severity Location Issue
🟡 Warning apidef/validator.go:195-198
The validation for the enforced timeout middleware allows the feature to be enabled with a zero or unspecified timeout value. While the gateway runtime correctly ignores a zero-value timeout, this creates a misleading configuration where a user might believe a timeout is being enforced when it is not. This could lead to unexpected behavior where requests do not time out as intended.
💡 SuggestionStrengthen the validation logic to ensure that if a hard timeout is enabled (i.e., `Disabled` is false), a positive timeout value must be provided in either `TimeOut` or `TimeoutDuration`. This will prevent confusing configurations and ensure the feature behaves as expected when enabled.

✅ Performance Check Passed

No performance issues found – changes LGTM.


Powered by Visor from Probelabs

Last updated: 2026-05-15T11:51:27.689Z | Triggered by: pr_updated | Commit: b7f550b

💡 TIP: You can chat with Visor using /visor ask <your question>

@radkrawczyk radkrawczyk reopened this May 12, 2026
MaciekMis added 4 commits May 12, 2026 12:56
…-timeout-for-the-enforced-timeout-middleware' into TT-12339-be-implement-sub-second-timeout-for-the-enforced-timeout-middleware
@radkrawczyk radkrawczyk reopened this May 12, 2026
@github-actions

github-actions Bot commented May 15, 2026

Copy link
Copy Markdown
Contributor

🎯 Recommended Merge Targets

Based on JIRA ticket TT-12339: [BE] Implement sub-second timeout for the Enforced Timeout middleware

Fix Version: Tyk 5.14.0

⚠️ Warning: Expected release branches not found in repository

Required:

  • master - No matching release branches found. Fix will be included in future releases.

📋 Workflow

  1. Merge this PR to master first

@github-actions

Copy link
Copy Markdown
Contributor

🚨 Jira Linter Failed

Commit: b7f550b
Failed at: 2026-05-15 11:50:19 UTC

The Jira linter failed to validate your PR. Please check the error details below:

🔍 Click to view error details
failed to validate Jira issue: jira ticket TT-12339 has status 'In Test' but must be one of: In Dev, In Code Review, Ready For Dev, Dod Check

Next Steps

  • Ensure your branch name contains a valid Jira ticket ID (e.g., ABC-123)
  • Verify your PR title matches the branch's Jira ticket ID
  • Check that the Jira ticket exists and is accessible

This comment will be automatically deleted once the linter passes.

@sonarqubecloud

Copy link
Copy Markdown

Quality Gate Passed Quality Gate passed

Issues
0 New issues
0 Accepted issues

Measures
0 Security Hotspots
100.0% Coverage on New Code
0.0% Duplication on New Code

See analysis details on SonarQube Cloud

@radkrawczyk
radkrawczyk merged commit 1c91e02 into master May 15, 2026
38 of 55 checks passed
@radkrawczyk
radkrawczyk deleted the TT-12339-be-implement-sub-second-timeout-for-the-enforced-timeout-middleware branch May 15, 2026 14:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants