Skip to content

Commit

Permalink
TMP Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
evgeniy-scherbina committed Oct 10, 2023
1 parent bfbbf2b commit 8746c28
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
4 changes: 2 additions & 2 deletions service/cachemdw/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type ServiceCache struct {
cacheTTL time.Duration
decodedRequestContextKey any

logger *logging.ServiceLogger
*logging.ServiceLogger
}

func NewServiceCache(
Expand All @@ -38,7 +38,7 @@ func NewServiceCache(
evmClient: evmClient,
cacheTTL: cacheTTL,
decodedRequestContextKey: decodedRequestContextKey,
logger: logger,
ServiceLogger: logger,
}
}

Expand Down
31 changes: 15 additions & 16 deletions service/cachemdw/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cachemdw

import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"strings"
Expand All @@ -13,10 +14,6 @@ type cacheContextKey string

const (
CachedContextKey cacheContextKey = "X-KAVA-PROXY-CACHED"

//CacheHeaderKey = "X-Cache"
//CacheHitHeaderValue = "HIT"
//CacheMissHeaderValue = "MISS"
)

func (c *ServiceCache) Middleware(
Expand All @@ -33,8 +30,7 @@ func (c *ServiceCache) Middleware(
}

cachedQueryResponse, err := c.GetCachedQueryResponse(r.Context(), decodedReq, r.Host)
found := err == nil // TODO: refactor
if found {
if err == nil {
c.respond(next, w, r, cachedQueryResponse)
return
}
Expand All @@ -55,9 +51,11 @@ func (c *ServiceCache) respond(
r *http.Request,
cachedQueryResponse []byte,
) {
//w.Header().Add(CacheHeaderKey, CacheHitHeaderValue)
w.Header().Add("Content-Type", "application/json")
w.Write(cachedQueryResponse)
_, err := w.Write(cachedQueryResponse)
if err != nil {
c.Logger.Error().Msg(fmt.Sprintf("can't write cached response: %v", err))
}

cachedContext := context.WithValue(r.Context(), CachedContextKey, true)
next.ServeHTTP(w, r.WithContext(cachedContext))
Expand All @@ -70,29 +68,30 @@ func (c *ServiceCache) respondAndCache(
r *http.Request,
decodedReq *decode.EVMRPCRequestEnvelope,
) {
//w.Header().Add(CacheHeaderKey, CacheMissHeaderValue)

rec := httptest.NewRecorder()
next.ServeHTTP(rec, r.WithContext(uncachedContext))
result := rec.Result()
recorder := httptest.NewRecorder()
next.ServeHTTP(recorder, r.WithContext(uncachedContext))
result := recorder.Result()

if result.StatusCode != http.StatusOK {
return
}

body := rec.Body.Bytes()
body := recorder.Body.Bytes()
for k, v := range result.Header {
w.Header().Set(k, strings.Join(v, ","))
}
w.WriteHeader(result.StatusCode)
w.Write(body)
_, err := w.Write(body)
if err != nil {
c.Logger.Error().Msg(fmt.Sprintf("can't write response: %v", err))
}

if err := c.ValidateAndCacheQueryResponse(
r.Context(),
decodedReq,
r.Host,
body,
); err != nil {
// TODO
c.Logger.Error().Msg(fmt.Sprintf("can't validate and cache response: %v", err))
}
}

0 comments on commit 8746c28

Please sign in to comment.