-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WIP caching #166
base: master
Are you sure you want to change the base?
WIP caching #166
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,13 +38,15 @@ import ( | |
"strconv" | ||
"encoding/json" | ||
"strconv" | ||
"sync/atomic" | ||
"time" | ||
"fmt" | ||
"crypto/md5" | ||
|
||
"{{.PackageName}}/models" | ||
discovery "github.com/Clever/discovery-go" | ||
"github.com/afex/hystrix-go/hystrix" | ||
"github.com/gregjones/httpcache" | ||
logger "gopkg.in/Clever/kayvee-go.v6/logger" | ||
) | ||
|
||
|
@@ -57,7 +59,7 @@ var _ = bytes.Compare | |
type WagClient struct { | ||
basePath string | ||
requestDoer doer | ||
transport *http.Transport | ||
transport http.RoundTripper | ||
timeout time.Duration | ||
// Keep the retry doer around so that we can set the number of retries | ||
retryDoer *retryDoer | ||
|
@@ -111,6 +113,47 @@ func NewFromDiscovery() (*WagClient, error) { | |
return New(url), nil | ||
} | ||
|
||
func newCacheHitCounter(cache httpcache.Cache, basePath string, l logger.KayveeLogger) *cacheHitCounter { | ||
chc := &cacheHitCounter{Cache: cache, basePath: basePath} | ||
go chc.log(l) | ||
return chc | ||
} | ||
|
||
type cacheHitCounter struct { | ||
httpcache.Cache | ||
hits int64 | ||
misses int64 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. probably not worth it yet, but i could see us wanting to know this on a per-endpoint basis |
||
basePath string | ||
} | ||
|
||
func (c *cacheHitCounter) log(l logger.KayveeLogger) { | ||
ticker := time.NewTicker(time.Second * 30) | ||
for _ = range ticker.C { | ||
hits := atomic.LoadInt64(&c.hits) | ||
misses := atomic.LoadInt64(&c.misses) | ||
l.InfoD("wag-cache-stats", map[string]interface{}{ | ||
"hits": hits, | ||
"misses": misses, | ||
"url": c.basePath, | ||
}) | ||
} | ||
} | ||
|
||
func (c *cacheHitCounter) Get(key string) ([]byte, bool) { | ||
resp, ok := c.Cache.Get(key) | ||
if ok { | ||
atomic.AddInt64(&c.hits, 1) | ||
} else { | ||
atomic.AddInt64(&c.misses, 1) | ||
} | ||
return resp, ok | ||
} | ||
|
||
// SetCache enables caching. | ||
func (c *WagClient) SetCache(cache httpcache.Cache) { | ||
c.transport = httpcache.NewTransport(newCacheHitCounter(cache, c.basePath, c.logger)) | ||
} | ||
|
||
// SetRetryPolicy sets a the given retry policy for all requests. | ||
func (c *WagClient) SetRetryPolicy(retryPolicy RetryPolicy) { | ||
c.retryDoer.retryPolicy = retryPolicy | ||
|
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
// Code generated by go-swagger; DO NOT EDIT. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i assume this is from a go-swagger update? |
||
|
||
package models | ||
|
||
// This file was generated by the swagger tool. | ||
|
@@ -9,20 +7,16 @@ import ( | |
strfmt "github.com/go-openapi/strfmt" | ||
|
||
"github.com/go-openapi/errors" | ||
"github.com/go-openapi/swag" | ||
) | ||
|
||
// BadRequest bad request | ||
// swagger:model BadRequest | ||
|
||
type BadRequest struct { | ||
|
||
// message | ||
Message string `json:"message,omitempty"` | ||
} | ||
|
||
/* polymorph BadRequest message false */ | ||
|
||
// Validate validates this bad request | ||
func (m *BadRequest) Validate(formats strfmt.Registry) error { | ||
var res []error | ||
|
@@ -32,21 +26,3 @@ func (m *BadRequest) Validate(formats strfmt.Registry) error { | |
} | ||
return nil | ||
} | ||
|
||
// MarshalBinary interface implementation | ||
func (m *BadRequest) MarshalBinary() ([]byte, error) { | ||
if m == nil { | ||
return nil, nil | ||
} | ||
return swag.WriteJSON(m) | ||
} | ||
|
||
// UnmarshalBinary interface implementation | ||
func (m *BadRequest) UnmarshalBinary(b []byte) error { | ||
var res BadRequest | ||
if err := swag.ReadJSON(b, &res); err != nil { | ||
return err | ||
} | ||
*m = res | ||
return nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: mention that max-age is in seconds