From 72bbbe3817837e7e04099858d8218ea31df369ee Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Fri, 6 Nov 2020 00:25:09 +0300 Subject: [PATCH] client: remove cache responses Signed-off-by: Vasiliy Tolstov --- client/cache.go | 66 ------------------------------------ client/cache_test.go | 77 ------------------------------------------ client/noop.go | 2 -- client/options.go | 14 -------- client/test_request.go | 22 ------------ 5 files changed, 181 deletions(-) delete mode 100644 client/cache.go delete mode 100644 client/cache_test.go diff --git a/client/cache.go b/client/cache.go deleted file mode 100644 index 4267f748..00000000 --- a/client/cache.go +++ /dev/null @@ -1,66 +0,0 @@ -package client - -import ( - "context" - "encoding/json" - "fmt" - "hash/fnv" - "time" - - cache "github.com/patrickmn/go-cache" - "github.com/unistack-org/micro/v3/metadata" -) - -// NewCache returns an initialised cache. -func NewCache() *Cache { - return &Cache{ - cache: cache.New(cache.NoExpiration, 30*time.Second), - } -} - -// Cache for responses -type Cache struct { - cache *cache.Cache -} - -// Get a response from the cache -func (c *Cache) Get(ctx context.Context, req Request) (interface{}, bool) { - return c.cache.Get(key(ctx, req)) -} - -// Set a response in the cache -func (c *Cache) Set(ctx context.Context, req Request, rsp interface{}, expiry time.Duration) { - c.cache.Set(key(ctx, req), rsp, expiry) -} - -// List the key value pairs in the cache -func (c *Cache) List() map[string]string { - items := c.cache.Items() - - rsp := make(map[string]string, len(items)) - for k, v := range items { - bytes, _ := json.Marshal(v.Object) - rsp[k] = string(bytes) - } - - return rsp -} - -// key returns a hash for the context and request -func key(ctx context.Context, req Request) string { - ns, _ := metadata.Get(ctx, "Micro-Namespace") - - bytes, _ := json.Marshal(map[string]interface{}{ - "namespace": ns, - "request": map[string]interface{}{ - "service": req.Service(), - "endpoint": req.Endpoint(), - "method": req.Method(), - "body": req.Body(), - }, - }) - - h := fnv.New64() - h.Write(bytes) - return fmt.Sprintf("%x", h.Sum(nil)) -} diff --git a/client/cache_test.go b/client/cache_test.go deleted file mode 100644 index decd0ce0..00000000 --- a/client/cache_test.go +++ /dev/null @@ -1,77 +0,0 @@ -package client - -import ( - "context" - "testing" - "time" - - "github.com/unistack-org/micro/v3/metadata" -) - -func TestCache(t *testing.T) { - ctx := context.TODO() - req := &testRequest{service: "go.micro.service.foo", method: "Foo.Bar"} - - t.Run("CacheMiss", func(t *testing.T) { - if _, ok := NewCache().Get(ctx, req); ok { - t.Errorf("Expected to get no result from Get") - } - }) - - t.Run("CacheHit", func(t *testing.T) { - c := NewCache() - - rsp := "theresponse" - c.Set(ctx, req, rsp, time.Minute) - - if res, ok := c.Get(ctx, req); !ok { - t.Errorf("Expected a result, got nothing") - } else if res != rsp { - t.Errorf("Expected '%v' result, got '%v'", rsp, res) - } - }) -} - -func TestCacheKey(t *testing.T) { - ctx := context.TODO() - - req1 := &testRequest{service: "go.micro.service.foo", method: "Foo.Bar"} - req2 := &testRequest{service: "go.micro.service.foo", method: "Foo.Baz"} - req3 := &testRequest{service: "go.micro.service.foo", method: "Foo.Bar", body: "customquery"} - - t.Run("IdenticalRequests", func(t *testing.T) { - key1 := key(ctx, req1) - key2 := key(ctx, req1) - if key1 != key2 { - t.Errorf("Expected the keys to match for identical requests and context") - } - }) - - t.Run("DifferentRequestEndpoints", func(t *testing.T) { - key1 := key(ctx, req1) - key2 := key(ctx, req2) - - if key1 == key2 { - t.Errorf("Expected the keys to differ for different request endpoints") - } - }) - - t.Run("DifferentRequestBody", func(t *testing.T) { - key1 := key(ctx, req2) - key2 := key(ctx, req3) - - if key1 == key2 { - t.Errorf("Expected the keys to differ for different request bodies") - } - }) - - t.Run("DifferentMetadata", func(t *testing.T) { - mdCtx := metadata.Set(context.TODO(), "Micro-Namespace", "bar") - key1 := key(mdCtx, req1) - key2 := key(ctx, req1) - - if key1 == key2 { - t.Errorf("Expected the keys to differ for different metadata") - } - }) -} diff --git a/client/noop.go b/client/noop.go index 91349a9a..59bd5d42 100644 --- a/client/noop.go +++ b/client/noop.go @@ -202,6 +202,4 @@ func (n *noopClient) Publish(ctx context.Context, p Message, opts ...PublishOpti Header: md, Body: body, }, broker.PublishContext(options.Context)) - - return nil } diff --git a/client/options.go b/client/options.go index 5cf1125a..169d0f15 100644 --- a/client/options.go +++ b/client/options.go @@ -34,9 +34,6 @@ type Options struct { PoolSize int PoolTTL time.Duration - // Response cache - Cache *Cache - // Middleware for client Wrappers []Wrapper @@ -61,8 +58,6 @@ type CallOptions struct { Address []string // Backoff func Backoff BackoffFunc - // Duration to cache the response for - CacheExpiry time.Duration // Transport Dial Timeout DialTimeout time.Duration // Number of Call attempts @@ -145,7 +140,6 @@ type RequestOptions struct { func NewOptions(opts ...Option) Options { options := Options{ - Cache: NewCache(), Context: context.Background(), ContentType: "application/protobuf", Codecs: make(map[string]codec.NewCodec), @@ -400,14 +394,6 @@ func WithAuthToken() CallOption { } } -// WithCache is a CallOption which sets the duration the response -// shoull be cached for -func WithCache(c time.Duration) CallOption { - return func(o *CallOptions) { - o.CacheExpiry = c - } -} - // WithNetwork is a CallOption which sets the network attribute func WithNetwork(n string) CallOption { return func(o *CallOptions) { diff --git a/client/test_request.go b/client/test_request.go index 82b9b258..bc4a7f53 100644 --- a/client/test_request.go +++ b/client/test_request.go @@ -14,28 +14,6 @@ type testRequest struct { opts RequestOptions } -func newRequest(service, endpoint string, request interface{}, contentType string, reqOpts ...RequestOption) Request { - var opts RequestOptions - - for _, o := range reqOpts { - o(&opts) - } - - // set the content-type specified - if len(opts.ContentType) > 0 { - contentType = opts.ContentType - } - - return &testRequest{ - service: service, - method: endpoint, - endpoint: endpoint, - body: request, - contentType: contentType, - opts: opts, - } -} - func (r *testRequest) ContentType() string { return r.contentType }