-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathroundtripper.go
More file actions
468 lines (433 loc) · 13.7 KB
/
roundtripper.go
File metadata and controls
468 lines (433 loc) · 13.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
// Copyright (c) 2026 Bart Venter <72999113+bartventer@users.noreply.github.com>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package httpcache provides an implementation of http.RoundTripper that adds
// transparent HTTP response caching according to RFC 9111 (HTTP Caching).
//
// The main entry point is [NewTransport], which returns an [http.RoundTripper] for use with [http.Client].
// httpcache supports the required standard HTTP caching directives, as well as extension directives such as
// stale-while-revalidate, stale-if-error and immutable.
//
// Example usage:
//
// package main
//
// import (
// "log/slog"
// "net/http"
// "time"
//
// "github.com/bartventer/httpcache"
//
// // Register a cache backend by importing the package
// _ "github.com/bartventer/httpcache/store/fscache"
// )
//
// func main() {
// dsn := "fscache://?appname=myapp" // Example DSN for the file system cache backend
// client := &http.Client{
// Transport: httpcache.NewTransport(
// dsn,
// httpcache.WithSWRTimeout(10*time.Second),
// httpcache.WithLogger(slog.Default()),
// ),
// }
// }
package httpcache
import (
"cmp"
"context"
"errors"
"iter"
"log/slog"
"net/http"
"time"
"github.com/bartventer/httpcache/internal"
"github.com/bartventer/httpcache/store"
"github.com/bartventer/httpcache/store/driver"
)
const (
DefaultSWRTimeout = 5 * time.Second
CacheStatusHeader = internal.CacheStatusHeader
)
// transport is an implementation of [http.RoundTripper] that caches HTTP responses
// according to the HTTP caching rules defined in RFC 9111.
type transport struct {
// Configurable options
cache internal.ResponseCache // Cache for storing and retrieving responses
upstream http.RoundTripper // Underlying round tripper for upstream/origin requests
swrTimeout time.Duration // Timeout for Stale-While-Revalidate requests
logger *internal.Logger // Logger for debug output, if needed
// Internal details
rmc internal.RequestMethodChecker // Checks if HTTP request methods are understood
vm internal.VaryMatcher // Matches Vary headers to determine cache validity
uk internal.URLKeyer // Generates unique cache keys for requests
fc internal.FreshnessCalculator // Calculates the freshness of cached responses
ce internal.CacheabilityEvaluator // Evaluates if a response is cacheable
siep internal.StaleIfErrorPolicy // Handles stale-if-error caching policies
ci internal.CacheInvalidator // Invalidates cache entries based on conditions
rs internal.ResponseStorer // Stores HTTP responses in the cache
vrh internal.ValidationResponseHandler // Processes validation responses for revalidation
clock internal.Clock // Provides time-related operations, can be mocked for testing
}
// ErrOpenCache is used as the panic value when the cache cannot be opened.
// You may recover from this panic if you wish to handle the situation gracefully.
//
// Example usage:
//
// defer func() {
// if r := recover(); r != nil {
// if err, ok := r.(error); ok && errors.Is(err, ErrOpenCache) {
// // Handle the error gracefully, e.g., log it or return a default transport
// log.Println("Failed to open cache:", err)
// client := &http.Client{
// Transport: http.DefaultTransport, // Fallback to default transport
// }
// // Use the fallback client as needed
// _ = client
// } else {
// // Re-panic if it's not the expected error
// panic(r)
// }
// }
// }()
var ErrOpenCache = errors.New("httpcache: failed to open cache")
// NewTransport returns an [http.RoundTripper] that caches HTTP responses using
// the specified cache backend.
//
// The dsn parameter follows the format documented in [store.Open].
// Configuration is done via functional options like [WithUpstream] and
// [WithSWRTimeout].
//
// Panics with [ErrOpenCache] if the cache backend cannot be opened.
func NewTransport(dsn string, options ...Option) http.RoundTripper {
cache, err := store.Open(dsn)
if err != nil {
panic(ErrOpenCache)
}
return newTransport(cache, options...)
}
func newTransport(conn driver.Conn, options ...Option) http.RoundTripper {
rt := &transport{
cache: internal.NewResponseCache(conn),
rmc: internal.NewRequestMethodChecker(),
vm: internal.NewVaryMatcher(internal.NewHeaderValueNormalizer()),
uk: internal.NewURLKeyer(),
ce: internal.NewCacheabilityEvaluator(),
clock: internal.NewClock(),
}
for _, opt := range options {
opt.apply(rt)
}
rt.upstream = cmp.Or(rt.upstream, http.DefaultTransport)
rt.swrTimeout = cmp.Or(max(rt.swrTimeout, 0), DefaultSWRTimeout)
if rt.logger == nil {
rt.logger = internal.NewLogger(slog.DiscardHandler)
}
rt.fc = internal.NewFreshnessCalculator(rt.clock)
rt.ci = internal.NewCacheInvalidator(rt.cache, rt.uk)
rt.siep = internal.NewStaleIfErrorPolicy(rt.clock)
rt.rs = internal.NewResponseStorer(
rt.cache,
internal.NewVaryHeaderNormalizer(),
internal.NewVaryKeyer(),
)
rt.vrh = internal.NewValidationResponseHandler(
rt.logger,
rt.clock,
rt.ci,
rt.ce,
rt.siep,
rt.rs,
)
return rt
}
// NewClient returns a new [http.Client], configured with a transport that
// caches HTTP responses, using the specified cache backend DSN.
//
// See [NewTransport] for details on the DSN and available options.
func NewClient(dsn string, options ...Option) *http.Client {
return &http.Client{Transport: NewTransport(dsn, options...)}
}
var _ http.RoundTripper = (*transport)(nil)
func (r *transport) RoundTrip(req *http.Request) (*http.Response, error) {
urlKey := r.uk.URLKey(req.URL)
if !r.rmc.IsRequestMethodUnderstood(req) {
return r.handleUnrecognizedMethod(req, urlKey)
}
refs, err := r.cache.GetRefs(urlKey)
if err != nil || len(refs) == 0 {
return r.handleCacheMiss(req, urlKey, nil, -1)
}
refIndex, found := r.vm.VaryHeadersMatch(refs, req.Header)
if !found {
return r.handleCacheMiss(req, urlKey, refs, -1)
}
entry, err := r.cache.Get(refs[refIndex].ResponseID, req)
if err != nil {
r.logger.LogCacheError(
"Error retrieving cache entry; possible corruption.",
err,
req,
urlKey,
internal.MiscFunc(func() internal.Misc {
return internal.Misc{Refs: refs, RefIndex: refIndex}
}),
)
return r.handleCacheMiss(req, urlKey, refs, refIndex)
}
return r.handleCacheHit(req, entry, urlKey, refs, refIndex)
}
func (r *transport) handleUnrecognizedMethod(
req *http.Request,
urlKey string,
) (*http.Response, error) {
if !internal.IsUnsafeMethod(req.Method) {
resp, err := r.upstream.RoundTrip(req)
if err != nil {
return nil, err
}
internal.CacheStatusBypass.ApplyTo(resp.Header)
r.logger.LogCacheBypass(
"Bypass; unrecognized (safe) method, served from upstream.",
req,
urlKey,
nil,
)
return resp, nil
}
resp, err := r.upstream.RoundTrip(req)
if err != nil {
return nil, err
}
if internal.IsNonErrorStatus(resp.StatusCode) {
refs, _ := r.cache.GetRefs(urlKey)
r.ci.InvalidateCache(req.URL, resp.Header, refs, urlKey)
}
internal.CacheStatusBypass.ApplyTo(resp.Header)
r.logger.LogCacheBypass(
"Bypass; unrecognized (unsafe) method, served from upstream.",
req,
urlKey,
nil,
)
return resp, nil
}
func (r *transport) handleCacheMiss(
req *http.Request,
urlKey string,
refs internal.ResponseRefs,
refIndex int,
) (*http.Response, error) {
ccReq := internal.ParseCCRequestDirectives(req.Header)
if ccReq.OnlyIfCached() {
r.logger.LogCacheMiss(
req,
urlKey,
internal.MiscFunc(func() internal.Misc {
return internal.Misc{
CCReq: ccReq,
Refs: refs,
RefIndex: refIndex,
}
}),
)
return make504Response(req)
}
resp, start, end, err := r.roundTripTimed(req)
if err != nil {
return nil, err
}
ccResp := internal.ParseCCResponseDirectives(resp.Header)
if r.ce.CanStoreResponse(resp, ccReq, ccResp) {
_ = r.rs.StoreResponse(req, resp, urlKey, refs, start, end, refIndex)
}
internal.CacheStatusMiss.ApplyTo(resp.Header)
r.logger.LogCacheMiss(req, urlKey, internal.MiscFunc(func() internal.Misc {
return internal.Misc{
CCReq: ccReq,
CCResp: ccResp,
Refs: refs,
RefIndex: refIndex,
}
}))
return resp, nil
}
//nolint:cyclop // The complexity of this function is justified by the need to handle multiple caching scenarios according to RFC 9111.
func (r *transport) handleCacheHit(
req *http.Request,
stored *internal.Response,
urlKey string,
refs internal.ResponseRefs,
refIndex int,
) (*http.Response, error) {
ccReq := internal.ParseCCRequestDirectives(req.Header)
ccResp := internal.ParseCCResponseDirectives(stored.Data.Header)
freshness := r.fc.CalculateFreshness(stored, ccReq, ccResp)
respNoCacheFieldsRaw, hasRespNoCache := ccResp.NoCache()
respNoCacheFieldsSeq, isRespNoCacheQualified := respNoCacheFieldsRaw.Value()
// RFC 8246: If response is fresh and immutable, always serve from cache unless request has no-cache
if !freshness.IsStale && ccResp.Immutable() && !ccReq.NoCache() {
return r.serveFromCache(
req,
urlKey,
stored,
freshness,
isRespNoCacheQualified,
respNoCacheFieldsSeq,
)
}
if (freshness.IsStale && ccResp.MustRevalidate()) ||
(hasRespNoCache && !isRespNoCacheQualified) { // Unqualified no-cache: must revalidate before serving from cache
goto revalidate
}
if ccReq.OnlyIfCached() || (!freshness.IsStale && !ccReq.NoCache()) {
return r.serveFromCache(
req,
urlKey,
stored,
freshness,
isRespNoCacheQualified,
respNoCacheFieldsSeq,
)
}
if swr, swrValid := ccResp.StaleWhileRevalidate(); freshness.IsStale && swrValid {
age := freshness.Age.Value + r.clock.Since(freshness.Age.Timestamp)
staleFor := age - freshness.UsefulLife
if staleFor >= 0 && staleFor < swr {
return r.handleStaleWhileRevalidate(req, stored, urlKey, freshness, ccReq)
}
}
revalidate:
req = withConditionalHeaders(req, stored.Data.Header)
resp, start, end, err := r.roundTripTimed(req)
if err != nil {
return nil, err
}
ctx := internal.RevalidationContext{
URLKey: urlKey,
Start: start,
End: end,
CCReq: ccReq,
Stored: stored,
Refs: refs,
RefIndex: refIndex,
Freshness: freshness,
}
return r.vrh.HandleValidationResponse(ctx, req, resp)
}
func (r *transport) serveFromCache(
req *http.Request,
urlKey string,
stored *internal.Response,
freshness *internal.Freshness,
noCacheQualified bool,
noCacheFieldsSeq iter.Seq[string],
) (*http.Response, error) {
if noCacheQualified {
//Qualified no-cache: may serve from cache with fields stripped
for field := range noCacheFieldsSeq {
stored.Data.Header.Del(field)
}
}
internal.SetAgeHeader(stored.Data, r.clock, freshness.Age)
internal.CacheStatusHit.ApplyTo(stored.Data.Header)
r.logger.LogCacheHit(req, urlKey, internal.MiscFunc(func() internal.Misc {
return internal.Misc{
Stored: stored,
Freshness: freshness,
}
}))
return stored.Data, nil
}
// handleStaleWhileRevalidate serves a stale cached response immediately and triggers
// background revalidation in a separate goroutine (RFC 5861, §3).
func (r *transport) handleStaleWhileRevalidate(
req *http.Request,
stored *internal.Response,
urlKey string,
freshness *internal.Freshness,
ccReq internal.CCRequestDirectives,
) (*http.Response, error) {
req2 := req.Clone(req.Context())
req2 = withConditionalHeaders(req2, stored.Data.Header)
// Background revalidation is "best effort"; it is not guaranteed to complete
// if the program exits before the goroutine finishes. This design choice was
// made to keep the API simple and avoid requiring explicit shutdown coordination.
//
// Open a discussion at github.com/bartventer/httpcache/issues if your use case requires
// guaranteed completion.
go r.backgroundRevalidate(req2, stored, urlKey, freshness, ccReq)
internal.CacheStatusStale.ApplyTo(stored.Data.Header)
r.logger.LogCacheStaleRevalidate(req, urlKey, internal.MiscFunc(func() internal.Misc {
return internal.Misc{
CCReq: ccReq,
Stored: stored,
Freshness: freshness,
}
}))
return stored.Data, nil
}
func (r *transport) backgroundRevalidate(
req *http.Request,
stored *internal.Response,
urlKey string,
freshness *internal.Freshness,
ccReq internal.CCRequestDirectives,
) {
ctx, cancel := context.WithTimeout(req.Context(), r.swrTimeout)
defer cancel()
req = req.WithContext(ctx)
errc := make(chan error, 1)
go func() {
defer close(errc)
//nolint:bodyclose // The response is not used, so we don't need to close it.
resp, start, end, err := r.roundTripTimed(req)
if err != nil {
errc <- err
return
}
select {
case <-req.Context().Done():
errc <- req.Context().Err()
return
default:
}
revalCtx := internal.RevalidationContext{
URLKey: urlKey,
Start: start,
End: end,
CCReq: ccReq,
Stored: stored,
Freshness: freshness,
}
//nolint:bodyclose // The response is not used, so we don't need to close it.
_, err = r.vrh.HandleValidationResponse(revalCtx, req, resp)
errc <- err
}()
select {
case <-ctx.Done():
case <-errc:
}
}
func (r *transport) roundTripTimed(
req *http.Request,
) (resp *http.Response, start, end time.Time, err error) {
start = r.clock.Now()
resp, err = r.upstream.RoundTrip(req)
end = r.clock.Now()
if resp != nil {
_ = internal.FixDateHeader(resp.Header, end)
}
return
}