Skip to content

Commit

Permalink
fix validation of retry
Browse files Browse the repository at this point in the history
  • Loading branch information
fraidev committed Dec 20, 2023
1 parent 763bd3e commit 31d01fc
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ func main() {
e.Use(middlewares.RateLimit(config))
e.Use(middlewares.DenyRoutes(config))
e.Use(middlewares.Cache(config))
e.Use(middlewares.Gzip(config))
e.Use(middlewares.Retry(config))
e.Use(middlewares.Gzip(config))
e.Use(middleware.ProxyWithConfig(*config.ProxyConfig))

// Start metrics server
Expand Down
7 changes: 6 additions & 1 deletion middlewares/retry.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package middlewares

import (
"strings"

"github.com/labstack/echo/v4"
"github.com/marigold-dev/tzproxy/utils"
)
Expand All @@ -14,9 +16,12 @@ func Retry(config *utils.Config) echo.MiddlewareFunc {
err = next(c)

status := c.Response().Status
if status == 404 || status == 410 {
if status == 404 || status == 410 &&
strings.Contains(c.Request().URL.Path, "blocks") {
c.Set("retry", true)
return next(c)
} else {
c.Set("retry", false)
}

return err
Expand Down
18 changes: 8 additions & 10 deletions utils/balancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (b *sameNodeBalancer) Next(c echo.Context) *middleware.ProxyTarget {
b.mutex.Lock()
defer b.mutex.Unlock()

if b.retryTarget != nil && c.Get("retry") != nil {
if b.retryTarget != nil && c.Get("retry") != nil && c.Get("retry").(bool) {
return b.retryTarget
}

Expand All @@ -67,16 +67,14 @@ func (b *sameNodeBalancer) Next(c echo.Context) *middleware.ProxyTarget {
return b.targets[0]
}

var i int
ipKey := []byte(c.RealIP())

got, err := b.store.Get(c.Request().Context(), ipKey)
ctx := c.Request().Context()
ip := []byte(c.RealIP())
got, err := b.store.Get(ctx, ip)
if err != nil {
i = b.random.Intn(len(b.targets))
b.store.Set(c.Request().Context(), ipKey, []byte{byte(i)}, b.TTL)
} else {
i = int(got[0])
i := b.random.Intn(len(b.targets))
b.store.Set(ctx, ip, []byte{byte(i)}, b.TTL)
return b.targets[i]
}

return b.targets[i]
return b.targets[int(got[0])]
}

0 comments on commit 31d01fc

Please sign in to comment.