Skip to content

Commit

Permalink
add x-alice-cache for requests
Browse files Browse the repository at this point in the history
  • Loading branch information
MindHunter86 committed Oct 19, 2024
1 parent ca4b0cb commit 8b291f5
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
5 changes: 5 additions & 0 deletions internal/proxy/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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")
}
Expand All @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions internal/proxy/middlewares.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
3 changes: 3 additions & 0 deletions internal/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 8b291f5

Please sign in to comment.