Skip to content

Commit

Permalink
fix/production patch (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
MindHunter86 authored Jul 14, 2024
2 parents 9215747 + 0efcc8e commit 73d6aa1
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
2 changes: 1 addition & 1 deletion internal/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func NewCache(c context.Context) (cache *Cache, e error) {
CleanWindow: cli.Duration("cache-clean-window"),

MaxEntriesInWindow: 1000 * 10 * 60,
MaxEntrySize: 32 * 1024,
MaxEntrySize: 128 * 1024,

// not worked?
Verbose: zerolog.GlobalLevel() == zerolog.TraceLevel,
Expand Down
17 changes: 14 additions & 3 deletions internal/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (m *Proxy) ProxyFiberRequest(c *fiber.Ctx) (e error) {
rsp := fasthttp.AcquireResponse()
defer fasthttp.ReleaseResponse(rsp)

if e = m.doRequest(req, rsp); e != nil {
if e = m.doRequest(c, req, rsp); e != nil {
return
}

Expand Down Expand Up @@ -79,25 +79,36 @@ func (m *Proxy) acquireRewritedRequest(c *fiber.Ctx) *fasthttp.Request {
return req
}

func (m *Proxy) doRequest(req *fasthttp.Request, rsp *fasthttp.Response) (e error) {
func (m *Proxy) doRequest(c *fiber.Ctx, req *fasthttp.Request, rsp *fasthttp.Response) (e error) {
if e = m.client.Do(req, rsp); e != nil {
return
}

status, body := rsp.StatusCode(), rsp.Body()

if status < fasthttp.StatusOK && status >= fasthttp.StatusInternalServerError {
if status < fasthttp.StatusOK || status >= fasthttp.StatusInternalServerError {
e = fmt.Errorf("proxy server respond with status %d", status)
return
} else if status >= fiber.StatusBadRequest {
rlog(c).Warn().Msgf("status %d detected for request, bypass cache", status)

m.bypassCache(c)
return
}

if len(body) == 0 {
e = errors.New("proxy server respond with nil body")
rlog(c).Trace().Msg(req.String())
}

return
}

func (*Proxy) bypassCache(c *fiber.Ctx) {
key := c.Context().UserValue(utils.UVCacheKey).(*Key)
key.Reset()
}

func (m *Proxy) cacheResponse(c *fiber.Ctx, rsp *fasthttp.Response) (e error) {
key := c.Context().UserValue(utils.UVCacheKey).(*Key)

Expand Down
2 changes: 1 addition & 1 deletion internal/service/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func (m *Service) fiberRouterInitialization() {
cacheapi := m.fb.Group("/internal/cache", m.proxy.MiddlewareInternalApi)

cacheapi.Get("/stats", m.proxy.HandleCacheStats)
cacheapi.Post("/stats/reset", m.proxy.HandleCacheStats)
cacheapi.Post("/stats/reset", m.proxy.HandleCacheStatsReset)
cacheapi.Get("/dump", m.proxy.HandleCacheDump)
cacheapi.Get("/dumpkeys", m.proxy.HandleCacheDumpKeys)
cacheapi.Post("/purge", m.proxy.HandleCachePurge)
Expand Down

0 comments on commit 73d6aa1

Please sign in to comment.