1
Fork 0
mirror of https://github.com/caddyserver/caddy.git synced 2024-12-16 21:56:40 -05:00

cache: Make peer addresses configurable

This commit is contained in:
Matthew Holt 2019-10-28 15:09:12 -06:00
parent 442fd748f6
commit d6dad04e96
No known key found for this signature in database
GPG key ID: 2A349DD577D586A5

View file

@ -34,7 +34,10 @@ func init() {
// Cache implements a simple distributed cache. // Cache implements a simple distributed cache.
type Cache struct { type Cache struct {
group *groupcache.Group Self string `json:"self,omitempty"`
Peers []string `json:"peers,omitempty"`
MaxSize int64 `json:"max_size,omitempty"`
group *groupcache.Group
} }
// CaddyModule returns the Caddy module information. // CaddyModule returns the Caddy module information.
@ -47,18 +50,20 @@ func (Cache) CaddyModule() caddy.ModuleInfo {
// Provision provisions c. // Provision provisions c.
func (c *Cache) Provision(ctx caddy.Context) error { func (c *Cache) Provision(ctx caddy.Context) error {
// TODO: proper pool configuration // TODO: use UsagePool so that cache survives config reloads - TODO: a single cache for whole process?
me := "http://localhost:5555" maxSize := c.MaxSize
// TODO: Make max size configurable if maxSize == 0 {
maxSize := int64(512 << 20) const maxMB = 512
maxSize = int64(maxMB << 20)
}
poolMu.Lock() poolMu.Lock()
if pool == nil { if pool == nil {
pool = groupcache.NewHTTPPool(me) pool = groupcache.NewHTTPPool(c.Self)
c.group = groupcache.NewGroup(groupName, maxSize, groupcache.GetterFunc(c.getter)) c.group = groupcache.NewGroup(groupName, maxSize, groupcache.GetterFunc(c.getter))
} else { } else {
c.group = groupcache.GetGroup(groupName) c.group = groupcache.GetGroup(groupName)
} }
pool.Set(me) pool.Set(append(c.Peers, c.Self)...)
poolMu.Unlock() poolMu.Unlock()
return nil return nil
@ -66,13 +71,18 @@ func (c *Cache) Provision(ctx caddy.Context) error {
// Validate validates c. // Validate validates c.
func (c *Cache) Validate() error { func (c *Cache) Validate() error {
// TODO: implement if c.Self == "" {
return fmt.Errorf("address of this instance (self) is required")
}
if c.MaxSize < 0 {
return fmt.Errorf("size must be greater than 0")
}
return nil return nil
} }
func (c *Cache) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error { func (c *Cache) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
// TODO: proper RFC implementation of cache control headers... // TODO: proper RFC implementation of cache control headers...
if r.Header.Get("Cache-Control") == "no-cache" || r.Method != "GET" { if r.Header.Get("Cache-Control") == "no-cache" || (r.Method != "GET" && r.Method != "HEAD") {
return next.ServeHTTP(w, r) return next.ServeHTTP(w, r)
} }