Documentation
¶
Overview ¶
Package cdn emulates Cloud CDN (SRS 5.9): the HTTP cache the load balancer puts in front of CDN-enabled backend services and backend buckets. It implements Cloud CDN's cache modes, TTL settings, cache key policies, negative caching, serve-while-stale, request coalescing, byte ranges, revalidation, signed URLs/cookies and invalidation, over a size-limited LRU store with a memory and a disk tier.
The service itself only owns the Cache and its admin endpoints; lb calls (*Cache).Serve for CDN-enabled backends (env.Lookup("cdn") and interface{ Cache() *cdn.Cache }) and (*Cache).Invalidate for urlMaps.invalidateCache.
Index ¶
- Constants
- func New(env *emu.Env) emu.Service
- func ParseSize(v string) (int64, error)
- func SignCookie(prefix, keyName string, key []byte, expires time.Time) string
- func SignURL(u, keyName string, key []byte, expires time.Time) string
- func SignURLPrefix(prefix, keyName string, key []byte, expires time.Time) string
- type Backend
- type BackendStats
- type Cache
- func (c *Cache) BackendStats(project string) []BackendStats
- func (c *Cache) Entries(backend string, limit int) ([]EntryInfo, bool)
- func (c *Cache) Invalidate(backendIDs []string, host, path string, cacheTags []string)
- func (c *Cache) Limit() int64
- func (c *Cache) Purge()
- func (c *Cache) Serve(w http.ResponseWriter, r *http.Request, b Backend, origin http.Handler) string
- func (c *Cache) Stats() (entries int, bytes int64)
- func (c *Cache) Wait()
- type Clock
- type EntryInfo
- type Options
- type Service
Constants ¶
const ( StatusHit = "hit" StatusMiss = "miss" StatusRevalidated = "revalidated" StatusUncacheable = "uncacheable" )
Values of the {cdn_cache_status} header variable (FR-CDN-004).
const ( CacheAllStatic = "CACHE_ALL_STATIC" UseOriginHeaders = "USE_ORIGIN_HEADERS" ForceCacheAll = "FORCE_CACHE_ALL" )
Cache modes (FR-CDN-002).
const DefaultCacheSize = 1 << 30
DefaultCacheSize is the default cache size limit (FR-CDN-008).
const Name = "cdn"
Name is the service name.
const SignedCookieName = "Cloud-CDN-Cookie"
SignedCookieName is the Cloud CDN signed cookie (FR-CDN-007).
Variables ¶
This section is empty.
Functions ¶
func New ¶
New returns the service. The cache exists from construction so lb can obtain it before cdn starts; its disk tier lives under the service data directory and is rebuilt on restart in --data-dir mode.
func ParseSize ¶
ParseSize parses a size such as "1GiB", "512MiB", "100MB", "64k" or a plain byte count. Empty means DefaultCacheSize.
func SignCookie ¶
SignCookie returns a Cloud-CDN-Cookie value for prefix.
Types ¶
type Backend ¶
type Backend struct {
// ID is the backend service or backend bucket selfLink; it namespaces
// the cache and is the unit of invalidation.
ID string
// Kind is "backendService" or "backendBucket".
Kind string
// ServicePolicy or BucketPolicy is the resource's cdnPolicy.
ServicePolicy *compute.BackendServiceCdnPolicy
BucketPolicy *compute.BackendBucketCdnPolicy
// CompressionMode is the resource's compressionMode
// ("AUTOMATIC" enables dynamic gzip compression).
CompressionMode string
// SignedURLKeys maps signed URL key names to their raw 16-byte
// values (FR-CDN-007). Empty means no signature validation.
SignedURLKeys map[string][]byte
// SignedURLCacheMaxAgeSec overrides cdnPolicy.signedUrlCacheMaxAgeSec
// when non-zero (default 3600).
SignedURLCacheMaxAgeSec int64
}
Backend describes the CDN-enabled backend service or backend bucket a request is served by. lb builds it from the compute resource.
type BackendStats ¶ added in v0.4.0
type BackendStats struct {
// Backend is the backend service or bucket's resource path.
Backend string `json:"backend"`
Entries int `json:"entries"`
Bytes int64 `json:"bytes"`
Hits int64 `json:"hits"`
Misses int64 `json:"misses"`
Revalidated int64 `json:"revalidated"`
Uncacheable int64 `json:"uncacheable"`
}
BackendStats is one backend's cache usage and traffic.
type Cache ¶
type Cache struct {
// contains filtered or unexported fields
}
Cache is the Cloud CDN HTTP cache shared by every CDN-enabled backend.
func (*Cache) BackendStats ¶ added in v0.4.0
func (c *Cache) BackendStats(project string) []BackendStats
BackendStats reports each backend with cached entries or traffic, in project ("" for all), sorted by backend.
func (*Cache) Entries ¶ added in v0.4.0
Entries lists up to limit (0 = all) of a backend's cached responses, by host and path, and whether more were left out.
func (*Cache) Invalidate ¶
Invalidate removes the entries of the given backends (all backends when empty) whose host matches host ("" = any) and whose path matches path: exact, or a prefix when path ends in "/*" ("" = any). With cacheTags, only entries carrying one of the tags (Cache-Tag response header) match. It is effective before it returns (FR-CDN-005).
func (*Cache) Purge ¶
func (c *Cache) Purge()
Purge removes every cached object (FR-CDN-008) and returns the count.
func (*Cache) Serve ¶
func (c *Cache) Serve(w http.ResponseWriter, r *http.Request, b Backend, origin http.Handler) string
Serve answers r from cache or via origin (the LB's backend handler) per Cloud CDN rules and returns the {cdn_cache_status} value: "hit", "miss", "revalidated" or "uncacheable" (FR-CDN-001..007). It counts the value for the backend's hit ratio.
type EntryInfo ¶ added in v0.4.0
type EntryInfo struct {
Host string `json:"host"`
Path string `json:"path"`
CacheKey string `json:"cacheKey"`
Status int `json:"status"`
Bytes int64 `json:"bytes"`
ContentType string `json:"contentType,omitempty"`
Stored time.Time `json:"stored"`
Age int64 `json:"age"`
TTL int64 `json:"ttl"`
Vary []string `json:"vary,omitempty"`
Tags []string `json:"tags,omitempty"`
// Tier is "memory" when the body is held in memory, else "disk".
Tier string `json:"tier"`
}
EntryInfo is one cached response, as the console lists it.
type Options ¶
type Options struct {
// Dir is the disk tier directory; "" keeps everything in memory.
Dir string
// MaxBytes is the size limit (default DefaultCacheSize).
MaxBytes int64
// Clock defaults to the wall clock.
Clock Clock
}
Options configure a Cache.
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service is the Cloud CDN service module.
func (*Service) Register ¶
Register mounts the admin endpoints:
POST /_emu/v1/cdn/purge clears the cache (`gcpemu cdn purge`) GET /_emu/v1/cdn reports entries, bytes used and the limit, and each backend's GET /_emu/v1/cdn/entries lists a backend's cached entries
Purging keeps the backends' hit and miss totals; Reset clears them.