From 8b291f5d7895385b1ef65dd7ea50c67dfd98acb8 Mon Sep 17 00:00:00 2001 From: Vadimka Komissarov Date: Sat, 19 Oct 2024 08:06:24 +0000 Subject: [PATCH] add x-alice-cache for requests --- internal/proxy/handlers.go | 5 +++++ internal/proxy/middlewares.go | 5 +++++ internal/proxy/proxy.go | 3 +++ 3 files changed, 13 insertions(+) diff --git a/internal/proxy/handlers.go b/internal/proxy/handlers.go index 3e6e9a7..147736f 100644 --- a/internal/proxy/handlers.go +++ b/internal/proxy/handlers.go @@ -9,6 +9,7 @@ import ( func (m *Proxy) IsRequestCached(c *fiber.Ctx) (ok bool) { if m.IsCacheBypass(c) { + c.Response().Header.Set("X-Alice-Cache", "BYPASS") return true } @@ -37,6 +38,8 @@ func (m *Proxy) HandleProxyToDst(c *fiber.Ctx) (e error) { } func (m *Proxy) HandleRandomRelease(c *fiber.Ctx) (e error) { + c.Response().Header.Set("X-Alice-Cache", "FAILED") + if m.randomizer == nil { return fiber.NewError(fiber.StatusServiceUnavailable, "BUG! randomizer is not initialized") } @@ -47,6 +50,8 @@ func (m *Proxy) HandleRandomRelease(c *fiber.Ctx) (e error) { "an error occurred in randomizer, maybe it's not ready yet") } + c.Response().Header.Set("X-Alice-Cache", "HIT") + if bytes.Equal(c.Request().PostArgs().Peek("js"), []byte("1")) { fmt.Fprintln(c, release) return respondPlainWithStatus(c, fiber.StatusOK) diff --git a/internal/proxy/middlewares.go b/internal/proxy/middlewares.go index cf43466..152360e 100644 --- a/internal/proxy/middlewares.go +++ b/internal/proxy/middlewares.go @@ -15,10 +15,15 @@ func (m *Proxy) MiddlewareValidation(c *fiber.Ctx) (e error) { return fiber.NewError(fiber.StatusBadRequest, e.Error()) } + // set ALICE cache status + c.Response().Header.Set("X-Alice-Cache", "MISS") + + // hijack all query=random_release queries if v.IsQueryEqual([]byte("random_release")) { if m.randomizer != nil { if release := m.randomizer.Randomize(); release != "" { if e = utils.RespondWithRandomRelease(release, c); e == nil { + c.Response().Header.Set("X-Alice-Cache", "HIT") return respondPlainWithStatus(c, fiber.StatusOK) } rlog(c).Error().Msg("could not respond on random release query - " + e.Error()) diff --git a/internal/proxy/proxy.go b/internal/proxy/proxy.go index d3172e5..f553bf7 100644 --- a/internal/proxy/proxy.go +++ b/internal/proxy/proxy.go @@ -202,12 +202,15 @@ func (m *Proxy) canRespondFromCache(c *fiber.Ctx) (_ bool, e error) { var ok bool if ok, e = m.cache.IsCached(country, key.UnsafeString()); e != nil { + c.Response().Header.Set("X-Alice-Cache", "FAILED") rlog(c).Warn().Msg("there is problems with cache driver") return } else if !ok { + c.Response().Header.Set("X-Alice-Cache", "MISS") return } + c.Response().Header.Set("X-Alice-Cache", "HIT") return true, e }