@marcel91200 / caddy Mirror · read-only / commits / d6dad04e
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 14, 05:15 UTC

cache: Make peer addresses configurable

Matthew Holt committed Oct 28, 2019 at 15:09 UTC d6dad04e9647c9ab177c2013ee1aa14b5fa81a5e
1 file changed +19 -9
modules/caddyhttp/httpcache/httpcache.go
+19 -9
@@ -34,7 +34,10 @@ func init() {
34
35 // Cache implements a simple distributed cache.
36 type Cache struct {
37 - group *groupcache.Group
37 + Self string `json:"self,omitempty"`
38 + Peers []string `json:"peers,omitempty"`
39 + MaxSize int64 `json:"max_size,omitempty"`
40 + group *groupcache.Group
41 }
42
43 // CaddyModule returns the Caddy module information.
@@ -47,18 +50,20 @@ func (Cache) CaddyModule() caddy.ModuleInfo {
50
51 // Provision provisions c.
52 func (c *Cache) Provision(ctx caddy.Context) error {
50 - // TODO: proper pool configuration
51 - me := "http://localhost:5555"
52 - // TODO: Make max size configurable
53 - maxSize := int64(512 << 20)
53 + // TODO: use UsagePool so that cache survives config reloads - TODO: a single cache for whole process?
54 + maxSize := c.MaxSize
55 + if maxSize == 0 {
56 + const maxMB = 512
57 + maxSize = int64(maxMB << 20)
58 + }
59 poolMu.Lock()
60 if pool == nil {
56 - pool = groupcache.NewHTTPPool(me)
61 + pool = groupcache.NewHTTPPool(c.Self)
62 c.group = groupcache.NewGroup(groupName, maxSize, groupcache.GetterFunc(c.getter))
63 } else {
64 c.group = groupcache.GetGroup(groupName)
65 }
61 - pool.Set(me)
66 + pool.Set(append(c.Peers, c.Self)...)
67 poolMu.Unlock()
68
69 return nil
@@ -66,13 +71,18 @@ func (c *Cache) Provision(ctx caddy.Context) error {
71
72 // Validate validates c.
73 func (c *Cache) Validate() error {
69 - // TODO: implement
74 + if c.Self == "" {
75 + return fmt.Errorf("address of this instance (self) is required")
76 + }
77 + if c.MaxSize < 0 {
78 + return fmt.Errorf("size must be greater than 0")
79 + }
80 return nil
81 }
82
83 func (c *Cache) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
84 // TODO: proper RFC implementation of cache control headers...
75 - if r.Header.Get("Cache-Control") == "no-cache" || r.Method != "GET" {
85 + if r.Header.Get("Cache-Control") == "no-cache" || (r.Method != "GET" && r.Method != "HEAD") {
86 return next.ServeHTTP(w, r)
87 }
88