Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Conversation

g-ra
Copy link

@g-ra g-ra commented Jun 16, 2025

This commit modifies the surrogate key storage mechanism to respect the TTL (Time-To-Live) specified in the Caddyfile's ttl directive.

Previously, surrogate keys were stored with a hardcoded, often indefinite, TTL based on the storage backend (e.g., Redis, Badger). This change propagates the TTL from the main cache configuration down to the surrogate key provider.

Key changes include:

  • Modified pkg/surrogate/providers/common.go:
    • baseStorage.init() now accepts a defaultTTL parameter.
    • It uses this defaultTTL for surrogate key duration if positive; otherwise, it falls back to the previous storageToInfiniteTTLMap.
  • Updated pkg/surrogate/surrogate.go, pkg/surrogate/providers/factory.go, and pkg/surrogate/providers/souin.go to pass the defaultTTL through their respective initialization functions.
  • Updated pkg/middleware/middleware.go:
    • NewHTTPCacheHandler now retrieves the defaultTTL from the configuration and passes it to surrogate.InitializeSurrogate.
  • Added tests in pkg/surrogate/providers/common_test.go:
    • Introduced a mock storer to capture the duration passed to Set.
    • New test cases verify that custom TTLs and default fallback TTLs are correctly applied to surrogate keys.

This resolves the issue where surrogate keys did not expire according to the configured cache TTL, potentially leading to stale data and excessive storage usage.

This commit modifies the surrogate key storage mechanism to respect the
TTL (Time-To-Live) specified in the Caddyfile's `ttl` directive.

Previously, surrogate keys were stored with a hardcoded, often indefinite,
TTL based on the storage backend (e.g., Redis, Badger). This change
propagates the TTL from the main cache configuration down to the
surrogate key provider.

Key changes include:
- Modified `pkg/surrogate/providers/common.go`:
    - `baseStorage.init()` now accepts a `defaultTTL` parameter.
    - It uses this `defaultTTL` for surrogate key duration if positive;
      otherwise, it falls back to the previous `storageToInfiniteTTLMap`.
- Updated `pkg/surrogate/surrogate.go`, `pkg/surrogate/providers/factory.go`,
  and `pkg/surrogate/providers/souin.go` to pass the `defaultTTL`
  through their respective initialization functions.
- Updated `pkg/middleware/middleware.go`:
    - `NewHTTPCacheHandler` now retrieves the `defaultTTL` from the
      configuration and passes it to `surrogate.InitializeSurrogate`.
- Added tests in `pkg/surrogate/providers/common_test.go`:
    - Introduced a mock storer to capture the duration passed to `Set`.
    - New test cases verify that custom TTLs and default fallback
      TTLs are correctly applied to surrogate keys.

This resolves the issue where surrogate keys did not expire according to
the configured cache TTL, potentially leading to stale data and excessive
storage usage.
Copy link

netlify bot commented Jun 16, 2025

Deploy Preview for teal-sprinkles-4c7f14 canceled.

Name Link
🔨 Latest commit 5f639fe
🔍 Latest deploy log https://app.netlify.com/projects/teal-sprinkles-4c7f14/deploys/6850679c327d2f00084ce106

This commit addresses compilation errors introduced by the previous
changes for surrogate key TTL handling.

The errors included:
- Missing `time` package imports in provider files.
- Mismatched function signatures for provider generation functions
  (`generate...Instance`) and their calls in `SurrogateFactory`.
- Mismatched arguments for `baseStorage.init()` calls within
  specific providers (Akamai, Cloudflare, Fastly).

Changes made:
- Added `import "time"` to `pkg/surrogate/providers/factory.go`,
  `souin.go`, `akamai.go`, `cloudflare.go`, and `fastly.go`.
- Updated `generateAkamaiInstance`, `generateCloudflareInstance`, and
  `generateFastlyInstance` function signatures to accept the
  `defaultTTL time.Duration` parameter.
- Ensured that these provider generation functions correctly pass the
  `defaultTTL` to their respective embedded `baseStorage.init()` calls.

These changes should resolve the build failures and ensure the TTL
propagation logic compiles successfully.
This commit resolves the final build error related to the surrogate key
TTL propagation changes. The `pkg/surrogate/surrogate.go` file was
missing an `import "time"` statement after its `InitializeSurrogate`
function signature was updated to include a `time.Duration` parameter.

All related files (`common.go`, `surrogate.go`, `factory.go`, `souin.go`,
`akamai.go`, `cloudflare.go`, `fastly.go` in the providers directory,
and `middleware.go`) have been reviewed to ensure consistency in
function signatures, calls, and time package imports.

This should allow your project to build successfully with the new TTL
propagation logic for surrogate keys.
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.

1 participant

Morty Proxy This is a proxified and sanitized view of the page, visit original site.