Skip to content

Commit

Permalink
Feature/integrate influxdata (#29)
Browse files Browse the repository at this point in the history
* add more tests for cache

* fix cache's exipration mechanism broken and add purge key test
  • Loading branch information
sillygod authored Aug 4, 2020
1 parent f37a969 commit bb26e6c
Show file tree
Hide file tree
Showing 16 changed files with 539 additions and 83 deletions.
12 changes: 9 additions & 3 deletions backends/redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,22 @@ func (suite *RedisBackendTestSuite) SetupSuite() {
log.Fatal(err)
}

resource, err := pool.Run("redis", "5.0.9", []string{})
redisVersion := "5.0.9"
resource, err := pool.Run("redis", redisVersion, []string{})
if err != nil {
log.Fatal(err)
}

suite.pool = pool
suite.resource = resource

port := resource.GetPort("6379/tcp")
InitRedisClient(fmt.Sprintf("localhost:%s", port), "", 0)

err = suite.pool.Retry(func() error {
return InitRedisClient(fmt.Sprintf("localhost:%s", port), "", 0)
})
if err != nil {
log.Fatal(err)
}
}

func (suite *RedisBackendTestSuite) TestParseRedisConfig() {
Expand Down
4 changes: 2 additions & 2 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var (
)

// intend to mock for test
var now = time.Now().UTC
var now = func() time.Time { return time.Now().UTC() }

// RuleMatcherType specifies the type of matching rule to cache.
type RuleMatcherType string
Expand Down Expand Up @@ -431,7 +431,7 @@ func (h *HTTPCache) Get(key string, request *http.Request) (*Entry, bool) {
return nil, false
}

// Keys list the keys holded by this cache
// Keys list the keys holden by this cache
func (h *HTTPCache) Keys() []string {
keys := []string{}
for index, l := range h.entriesLock {
Expand Down
42 changes: 41 additions & 1 deletion cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,15 +202,54 @@ func (suite *EntryTestSuite) TearDownSuite() {
type HTTPCacheTestSuite struct {
suite.Suite
config *Config
cache *HTTPCache
}

func (suite *HTTPCacheTestSuite) SetupSuite() {
err := backends.InitGroupCacheRes(50 * 1024 * 1024)
suite.Nil(err)
suite.config = getDefaultConfig()
suite.cache = NewHTTPCache(suite.config)
}

// TODO: add http cache test
func (suite *HTTPCacheTestSuite) TestGetNonExistEntry() {
req := makeRequest("/", http.Header{})
entry, exists := suite.cache.Get("abc", req)
suite.Nil(entry)
suite.False(exists)
}

func (suite *HTTPCacheTestSuite) TestGetExistEntry() {
req := makeRequest("/", http.Header{})
res := makeResponse(200, http.Header{})
entry := NewEntry("hello", req, res, suite.config)
suite.cache.Put(req, entry)

prevEntry, exists := suite.cache.Get("hello", req)
suite.Equal(prevEntry, entry)
suite.True(exists)
}

func (suite *HTTPCacheTestSuite) TestCleanEntry() {
req := makeRequest("/", http.Header{})
res := makeResponse(200, http.Header{})
key := "friday"

entry := NewEntry(key, req, res, suite.config)
suite.cache.Put(req, entry)

keyInKeys := false
keys := suite.cache.Keys()
for _, k := range keys {
if k == key {
keyInKeys = true
}
}
suite.True(keyInKeys)

err := suite.cache.Del(key)
suite.Nil(err)
}

func (suite *HTTPCacheTestSuite) TearDownSuite() {
err := backends.ReleaseGroupCacheRes()
Expand All @@ -219,6 +258,7 @@ func (suite *HTTPCacheTestSuite) TearDownSuite() {

func TestCacheStatusTestSuite(t *testing.T) {
suite.Run(t, new(CacheStatusTestSuite))
suite.Run(t, new(HTTPCacheTestSuite))
suite.Run(t, new(RuleMatcherTestSuite))
suite.Run(t, new(EntryTestSuite))
}
29 changes: 0 additions & 29 deletions caddyfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,13 +250,6 @@ func (h *Handler) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {

h.DistributedRaw = caddyconfig.JSONModuleObject(unm, "distributed", "consul", nil)

// case keyInfluxLog:
// raw, err := setupInfluxLog(keyInfluxLog, d, args)
// if err != nil {
// return err
// }
// h.InfluxLogRaw = raw

default:
return d.Err("Unknown cache parameter: " + parameter)
}
Expand All @@ -268,28 +261,6 @@ func (h *Handler) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
return nil
}

func setupInfluxLog(key string, d *caddyfile.Dispenser, args []string) (json.RawMessage, error) {

mod, err := caddy.GetModule("caddy.logging.writers." + key)
if err != nil {
return nil, d.Errf("getting influxlog module '%s': '%v'", mod, err)
}

unm, ok := mod.New().(caddyfile.Unmarshaler)
if !ok {
return nil, d.Errf("influxlog module '%s' is not a Caddyfile unmarshaler", mod)
}

err = unm.UnmarshalCaddyfile(d.NewFromNextSegment())
if err != nil {
return nil, err
}

raw := caddyconfig.JSONModuleObject(unm, "mylog", "influxlog", nil)

return raw, nil
}

// Interface guards
var (
_ caddyfile.Unmarshaler = (*Handler)(nil)
Expand Down
45 changes: 41 additions & 4 deletions purge.go → endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"sync"

"github.com/caddyserver/caddy/v2"
"github.com/sillygod/cdp-cache/pkg/helper"
)

var (
Expand Down Expand Up @@ -65,8 +66,8 @@ func (p *PurgePayload) parseURI() {

func (p *PurgePayload) pruneHost() {

if strings.HasPrefix(p.Host, "http") {
p.Host = strings.Split(p.Host, ":")[1]
if strings.HasPrefix(p.Host, "http") || strings.HasPrefix(p.Host, "https") {
p.Host = strings.Split(p.Host, "//")[1]
}

if !strings.HasSuffix(p.Host, "/") {
Expand Down Expand Up @@ -120,8 +121,8 @@ func (c cachePurge) Routes() []caddy.AdminRoute {
Handler: caddy.AdminHandlerFunc(c.handlePurge),
},
{
Pattern: "/caches",
Handler: caddy.AdminHandlerFunc(c.handleListCacheKeys),
Pattern: "/caches/",
Handler: caddy.AdminHandlerFunc(c.handleCacheEndpoints),
},
}
}
Expand All @@ -132,7 +133,43 @@ func health(w http.ResponseWriter, r *http.Request) error {
return nil
}

func (c cachePurge) handleShowCache(w http.ResponseWriter, r *http.Request) error {
var err error

if r.Method != http.MethodGet {
return caddy.APIError{
Code: http.StatusMethodNotAllowed,
Err: fmt.Errorf("method not allowed"),
}
}

key := helper.TrimBy(r.URL.Path, "/", 2)
cache := getHandlerCache()

entry, exists := cache.Get(key, r)
if exists {
err = entry.WriteBodyTo(w)
}

return err
}

func (c cachePurge) handleCacheEndpoints(w http.ResponseWriter, r *http.Request) error {
// a workaround for handling the wildcard endpoint. Caddy uses the standard library's mux
// so it doesn't support this natively.

path := r.URL.Path

switch path {
case "/caches/":
return c.handleListCacheKeys(w, r)
default:
return c.handleShowCache(w, r)
}
}

func (c cachePurge) handleListCacheKeys(w http.ResponseWriter, r *http.Request) error {

if r.Method != http.MethodGet {
return caddy.APIError{
Code: http.StatusMethodNotAllowed,
Expand Down
Loading

0 comments on commit bb26e6c

Please sign in to comment.