Skip to content
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

feat: restore cache mutex #25

Merged
merged 5 commits into from
Jan 14, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 34 additions & 21 deletions bin/v0.34.x/rpc/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,6 @@ func (cb *CacheBackend) Purge() {
cb.evictionCount = 0
cb.cacheServeCount = 0
cb.serveCount = 0
cb.resultChan = make(map[string]chan *ResponseCache)
cb.subscribeCount = make(map[string]int)
cb.mtx.Unlock()
}

Expand All @@ -99,6 +97,7 @@ func (cb *CacheBackend) HandleCachedHTTP(writer http.ResponseWriter, request *ht
// see if this request is already made, and in transit
// set response type as json
writer.Header().Set("Content-Type", "application/json")
writer.Header().Set("Connection", "close")

cached := cb.Get(request.URL.String())
// if cached, return as is
Expand All @@ -118,36 +117,45 @@ func (cb *CacheBackend) HandleCachedHTTP(writer http.ResponseWriter, request *ht
// if isInTransit is false, this is the first time we're processing this query
// run actual querier
if !isInTransit {
cb.resultChan[uri] = make(chan *ResponseCache)
c := make(chan *ResponseCache)
cb.resultChan[uri] = c
cb.subscribeCount[uri] = 0
cb.mtx.Unlock()

recorder := httptest.NewRecorder()
var cache *ResponseCache

go func() {
<-request.Context().Done()

// feed all subscriptions
cb.mtx.RLock()
subscribeCount := cb.subscribeCount[uri]
cb.mtx.RUnlock()

if cache != nil {
for i := 0; i < subscribeCount; i++ {
c <- cache
}
}
close(c)

cb.mtx.Lock()
delete(cb.subscribeCount, uri)
delete(cb.resultChan, uri)
cb.mtx.Unlock()
}()

// process request
handler.ServeHTTP(recorder, request)

// set in cache
responseCacheBody := cb.Set(request.URL.String(), recorder.Code, recorder.Body.Bytes())
cache = cb.Set(request.URL.String(), recorder.Code, recorder.Body.Bytes())

// write
writer.WriteHeader(recorder.Code)
writer.Write(recorder.Body.Bytes())

// feed all subscriptions
cb.mtx.RLock()
c := cb.resultChan[uri]
subscribeCount := cb.subscribeCount[uri]
cb.mtx.RUnlock()
for i := 0; i < subscribeCount; i++ {
c <- responseCacheBody
}

cb.mtx.Lock()
delete(cb.subscribeCount, uri)
delete(cb.resultChan, uri)
cb.mtx.Unlock()

return
}

Expand All @@ -156,8 +164,13 @@ func (cb *CacheBackend) HandleCachedHTTP(writer http.ResponseWriter, request *ht
cb.subscribeCount[uri]++
cb.mtx.Unlock()

response := <-resChan
response, ok := <-resChan

writer.WriteHeader(response.status)
writer.Write(response.body)
if ok {
writer.WriteHeader(response.status)
writer.Write(response.body)
} else {
writer.WriteHeader(503)
writer.Write([]byte("Service Unavailable"))
}
}