handler

package
v0.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 9, 2026 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CacheKeyParts

type CacheKeyParts struct {
	Path string
	Vary map[string]string
}

CacheKeyParts holds the components used to build a cache key. Path is the primary identifier (typically the upstream URL) and Vary captures request-derived dimensions like Accept-Encoding.

func NewCacheKeyParts

func NewCacheKeyParts(path string) CacheKeyParts

func (CacheKeyParts) Key

func (p CacheKeyParts) Key() cache.Key

type Handler

type Handler struct {
	// contains filtered or unexported fields
}

Handler provides a fluent API for creating cache-backed HTTP handlers.

Example usage:

h := handler.New(client, cache).
	CacheKey(func(r *http.Request) string {
		return "custom-key"
	}).
	Transform(func(r *http.Request) (*http.Request, error) {
		// Modify request before fetching
		return modifiedRequest, nil
	})

func New

func New(client *http.Client, c cache.Cache, spools *spool.Registry) *Handler

New creates a new Handler with the given HTTP client and cache. Concurrent cache misses for the same key share one upstream fetch through a spool group. By default: - Cache key is derived from the request URL - No request transformation is performed - Standard error handling is used.

func (*Handler) CacheKey

func (h *Handler) CacheKey(f func(*http.Request) string) *Handler

CacheKey sets the function used to determine the cache key for a request. The function receives the original incoming request.

func (*Handler) OnError

func (h *Handler) OnError(f func(error, http.ResponseWriter, *http.Request)) *Handler

OnError sets a custom error handler for the built handler. If not set, a default error handler is used.

func (*Handler) ParallelFetch added in v0.7.0

func (h *Handler) ParallelFetch(chunkSize int64, concurrency int) *Handler

ParallelFetch speeds up cold fills of large objects from upstreams that limit throughput per connection. It is opt-in because it multiplies upstream requests and only helps some upstreams. Objects that fit in one chunk cost a single request. Ranges are reassembled in order, so caching, streaming and verification are unaffected.

func (*Handler) Revalidate added in v0.7.0

func (h *Handler) Revalidate(header string, freshFor func(*http.Request) time.Duration) *Handler

Revalidate caches mutable upstream resources without serving them stale indefinitely or refetching unchanged content. Unlike TTL expiry, a stale entry is kept: it is revalidated with a cheap HEAD and served as-is when the upstream is unchanged or unavailable.

header names the response header that identifies the content (e.g. Docker-Content-Digest). It cannot be ETag, which the cache replaces with its own. freshFor returns how long an entry may be served without revalidation; zero marks it as immutable.

func (*Handler) ServeHTTP

func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements http.Handler. The handler will: 1. Determine the cache key using the configured function 2. Check if the content exists in cache 3. If cached, stream from cache 4. If not cached, transform the request and fetch from upstream 5. Cache the response while streaming to the client.

HEAD misses are forwarded upstream without caching, so metadata probes never trigger full downloads.

func (*Handler) TTL

func (h *Handler) TTL(f func(*http.Request) time.Duration) *Handler

TTL sets the function used to determine the cache TTL for a request. The function receives the original incoming request. If not set or returns 0, the cache's default/maximum TTL is used.

func (*Handler) Transform

func (h *Handler) Transform(f func(*http.Request) (*http.Request, error)) *Handler

Transform sets the function used to transform the incoming request before fetching. This is where you can modify the request URL, headers, etc. The function receives the original incoming request and should return the request that will be sent to the upstream server.

func (*Handler) Vary added in v0.7.0

func (h *Handler) Vary(f func(*http.Request) map[string]string) *Handler

Vary keys the cache on request headers that change the upstream response, so a variant fetched for one client is never served to a client that asked for another. Accept-Encoding is always included.

func (*Handler) Verify added in v0.7.0

func (h *Handler) Verify(f func(*http.Request) Verifier) *Handler

Verify keeps corrupt or truncated upstream responses out of the shared cache, where they would otherwise be served to every later client. The requesting client still receives the bytes. Return nil for requests whose expected content is unknown.

type Verifier added in v0.7.0

type Verifier interface {
	io.Writer
	Verify() error
}

Verifier checks a response body before it is committed to the cache.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL