Skip to content

feat: add MaxAge and TTLRemaining to CandidateResponse#262

Merged
dgryski merged 3 commits into
fastly:mainfrom
mvanhorn:feat/250-cache-ttl-methods
Jun 12, 2026
Merged

feat: add MaxAge and TTLRemaining to CandidateResponse#262
dgryski merged 3 commits into
fastly:mainfrom
mvanhorn:feat/250-cache-ttl-methods

Conversation

@mvanhorn

@mvanhorn mvanhorn commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

Summary

Adds MaxAge() and TTLRemaining() methods to fsthttp.CandidateResponse and deprecates the ambiguous TTL() method, bringing the Go SDK in line with the Rust SDK's cache-TTL surface.

Why this matters (#250)

CandidateResponse.TTL() reports maxAge - age (the remaining freshness), but its name does not make clear whether it returns the full freshness lifetime of the cached object or the freshness left right now. The Rust SDK already deprecated its TTL accessor and split the concept into MaxAge ("how long the object stays fresh") and TTLRemaining ("how much freshness is left right now"). This change mirrors that surface so callers can distinguish the two.

Changes

  • MaxAge() (uint32, error) — returns the full suggested freshness lifetime (opts.maxAge, or the set TTL when SetTTL/CacheOptions.TTL is in effect).
  • TTLRemaining() (uint32, error) — returns the remaining freshness, maxAge - age, and saturates to 0 for already-stale candidates instead of wrapping the unsigned subtraction.
  • TTL() is retained as a thin wrapper over TTLRemaining() and carries a // Deprecated: doc comment so existing callers keep working.
  • All three read from the existing getSuggestedCacheWriteOptions() result, so there is no ABI change.

Testing

  • go test ./fsthttp/... passes.
  • go vet ./... and gofmt -l are clean on the changed files.
  • integration_tests/httpcache builds for GOOS=wasip1 GOARCH=wasm.
  • testAfterSendCandidateResponsePropertiesUncached now asserts MaxAge() returns the full suggested lifetime (3600) and TTLRemaining() returns maxAge - age, alongside the existing TTL() backward-compatibility assertion.

Fixes #250

Drop the deprecated CandidateResponse.TTL() call in the uncached
candidate test; TTLRemaining() already covers it (age is 0, so
remaining == full freshness lifetime). Fixes staticcheck SA1019.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

@dgryski dgryski left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor suggested tweak, but otherwise LGTM.

Thanks!

Comment thread fsthttp/cache.go
// rather than wrapping around.
func (candidateResponse *CandidateResponse) TTLRemaining() (uint32, error) {
var maxAge, age uint32
if candidateResponse.useTTL {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'd prefer this if block to look like

		opts, err := candidateResponse.getSuggestedCacheWriteOptions()
		if err != nil {
			return 0, err
		}
		maxAge, age = opts.maxAge, opts.age
        if candidateResponse.useTTL {
           maxAge = candidateResponse.overrideTTL
        }

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 00b3a4c. fsthttp tests pass locally.

Fetch the suggested cache write options once and override maxAge when a
TTL is set, per review feedback.

@dgryski dgryski left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@dgryski dgryski merged commit 5a896a3 into fastly:main Jun 12, 2026
4 checks passed
@mvanhorn

Copy link
Copy Markdown
Contributor Author

Appreciate you landing this, @dgryski. Good to get MaxAge and TTLRemaining in and the ambiguous TTL deprecated.

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.

Clarify http cache TTL field

2 participants