Skip to content

Commit

Permalink
add memorycache back
Browse files Browse the repository at this point in the history
  • Loading branch information
localvar committed Apr 15, 2022
1 parent 16e9d8b commit 08c7345
Show file tree
Hide file tree
Showing 10 changed files with 246 additions and 220 deletions.
33 changes: 17 additions & 16 deletions pkg/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import (
const (
// InitialRequestID is the ID of the initial request.
InitialRequestID = "initial"
// InitialResponseID is the ID of the initial response.
InitialResponseID = "initial"
// DefaultResponseID is the ID of the default response.
DefaultResponseID = "default"
)

// Handler is the common interface for all traffic handlers,
Expand All @@ -53,7 +53,6 @@ type Context struct {
requests map[string]protocols.Request

targetResponseID string
response protocols.Response
responses map[string]protocols.Response

kv map[interface{}]interface{}
Expand Down Expand Up @@ -81,12 +80,8 @@ func (ctx *Context) AddTag(tag string) {
ctx.lazyTags = append(ctx.lazyTags, func() string { return tag })
}

// AddLazyTag add a lazy tag to the Context.
// questions about tags
// how to access statistics data from every requests?
// add new method when finish?
// add tag to every single request or add tag to Context
func (ctx *Context) AddLazyTag(lazyTagFunc func() string) {
// LazyAddTag add a tag to the Context in a lazy fashion.
func (ctx *Context) LazyAddTag(lazyTagFunc func() string) {
ctx.lazyTags = append(ctx.lazyTags, lazyTagFunc)
}

Expand Down Expand Up @@ -135,7 +130,8 @@ func (ctx *Context) Requests() map[string]protocols.Request {
return ctx.requests
}

// GetRequest returns the request for id.
// GetRequest returns the request for id, the function returns nil if
// there's no such request.
func (ctx *Context) GetRequest(id string) protocols.Request {
return ctx.requests[id]
}
Expand Down Expand Up @@ -165,10 +161,10 @@ func (ctx *Context) DeleteRequest(id string) {
// response producer creates a new response and save it as the target
// response.
//
// If target is an empty string, InitialResponseID will be used;
// If target is an empty string, DefaultResponseID will be used;
func (ctx *Context) UseResponse(target string) {
if target == "" {
target = InitialResponseID
target = DefaultResponseID
}

ctx.targetResponseID = target
Expand All @@ -179,17 +175,19 @@ func (ctx *Context) TargetResponseID() string {
return ctx.targetResponseID
}

// Response returns the default response.
// Response returns the default response, and the return value could
// be nil.
func (ctx *Context) Response() protocols.Response {
return ctx.response
return ctx.GetRequest(DefaultResponseID)
}

// Responses returns all responses.
func (ctx *Context) Responses() map[string]protocols.Response {
return ctx.responses
}

// GetResponse returns the response for id.
// GetResponse returns the response for id, the function returns nil if
// there's no such response.
func (ctx *Context) GetResponse(id string) protocols.Response {
return ctx.responses[id]
}
Expand Down Expand Up @@ -229,7 +227,6 @@ func (ctx *Context) OnFinish(fn func()) {

// Finish calls all finish functions.
func (ctx *Context) Finish() {
// TODO: add tags here
for _, req := range ctx.requests {
req.Close()
}
Expand All @@ -239,4 +236,8 @@ func (ctx *Context) Finish() {
for _, fn := range ctx.finishFuncs {
fn()
}
for _, fn := range ctx.lazyTags {
// TODO: add tags here
fn()
}
}
4 changes: 2 additions & 2 deletions pkg/context/contexttest/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@ func (c *MockedHTTPContext) AddTag(tag string) {
}
}

// AddLazyTag mocks the AddLazyTag function of HTTPContext
func (c *MockedHTTPContext) AddLazyTag(lazyTag func() string) {
// LazyAddTag mocks the LazyAddTag function of HTTPContext
func (c *MockedHTTPContext) LazyAddTag(lazyTag func() string) {
if c.MockedAddLazyTag != nil {
c.MockedAddLazyTag(lazyTag)
}
Expand Down
62 changes: 24 additions & 38 deletions pkg/filters/proxy/memorycache.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,18 @@ func NewMemoryCache(spec *MemoryCacheSpec) *MemoryCache {
}
}

func (mc *MemoryCache) key(req *http.Request) string {
return stringtool.Cat(httpprot.RequestScheme(req), req.Host, req.URL.Path, req.Method)
func (mc *MemoryCache) key(req *httpprot.Request) string {
return stringtool.Cat(req.Scheme(), req.Host(), req.Path(), req.Method())
}

// Load tries to load cache for HTTPContext.
func (mc *MemoryCache) Load(req *http.Request) *CacheEntry {
func (mc *MemoryCache) Load(req *httpprot.Request) *CacheEntry {
// Reference: https://tools.ietf.org/html/rfc7234#section-5.2

matched := false

for _, method := range mc.spec.Methods {
if req.Method == method {
if req.Method() == method {
matched = true
break
}
Expand All @@ -98,7 +98,7 @@ func (mc *MemoryCache) Load(req *http.Request) *CacheEntry {
return nil
}

for _, value := range req.Header.Values(keyCacheControl) {
for _, value := range req.HTTPHeader().Values(keyCacheControl) {
if strings.Contains(value, "no-cache") {
return nil
}
Expand All @@ -111,67 +111,53 @@ func (mc *MemoryCache) Load(req *http.Request) *CacheEntry {
return nil
}

// NeedStore returns whether the response need to be stored.
func (mc *MemoryCache) NeedStore(req *http.Request, resp *http.Response) bool {
// Store tries to cache the response.
func (mc *MemoryCache) Store(req *httpprot.Request, resp *httpprot.Response) {
if len(resp.RawPayload()) > int(mc.spec.MaxEntryBytes) {
return
}

matched := false
for _, method := range mc.spec.Methods {
if req.Method == method {
if req.Method() == method {
matched = true
break
}
}
if !matched {
return false
return
}

matched = false
for _, code := range mc.spec.Codes {
if resp.StatusCode == code {
if resp.StatusCode() == code {
matched = true
break
}
}
if !matched {
return false
return
}

for _, value := range req.Header.Values(keyCacheControl) {
for _, value := range req.HTTPHeader().Values(keyCacheControl) {
if strings.Contains(value, "no-store") ||
strings.Contains(value, "no-cache") {
return false
return
}
}
for _, value := range resp.Header.Values(keyCacheControl) {
for _, value := range resp.HTTPHeader().Values(keyCacheControl) {
if strings.Contains(value, "no-store") ||
strings.Contains(value, "no-cache") ||
strings.Contains(value, "must-revalidate") {
return false
return
}
}

return true
}

/*
key := mc.key(r)
key := mc.key(req)
entry := &CacheEntry{
StatusCode: w.StatusCode(),
Header: w.HTTPHeader().Clone(),
StatusCode: resp.StatusCode(),
Header: resp.HTTPHeader().Clone(),
Body: resp.RawPayload(),
}
bodyLength := 0
w.OnFlushBody(func(body []byte, complete bool) []byte {
bodyLength += len(body)
if bodyLength > int(mc.spec.MaxEntryBytes) {
return body
}
entry.body = append(entry.body, body...)
if complete {
mc.cache.SetDefault(key, entry)
}
return body
})
mc.cache.SetDefault(key, entry)
}
*/
Loading

0 comments on commit 08c7345

Please sign in to comment.