Skip to content

Commit

Permalink
extract 502 code error from message to perform retries pt2
Browse files Browse the repository at this point in the history
  • Loading branch information
cyrilevos committed Mar 28, 2024
1 parent 1a18c60 commit e9a6ef8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func NewConfig() *Config {
Str("uri", c.Request().URL.Path).
Str("ip", c.RealIP()).
Str("user_agent", c.Request().UserAgent()).
Msg("proxy error - detailed log")
Msg("proxy error")
return err
},
}
Expand Down
17 changes: 13 additions & 4 deletions middlewares/retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package middlewares

import (
"bytes"
"io"
"net/http"
"strings"
"regexp"
Expand All @@ -24,6 +25,11 @@ func Retry(config *config.Config) echo.MiddlewareFunc {
delayedResponse := echo.NewResponse(&writer, c.Echo())
c.SetResponse(delayedResponse)

// Read the request body
bodyBytes, _ := io.ReadAll(c.Request().Body)
// Replace the request body with a buffer
c.Request().Body = io.NopCloser(bytes.NewBuffer(bodyBytes))

var statusFromMsg int
err = next(c)

Expand All @@ -40,18 +46,18 @@ func Retry(config *config.Config) echo.MiddlewareFunc {
method := c.Request().Method
path := c.Request().URL.Path
shouldRetry := (method == http.MethodGet && (status == http.StatusNotFound || status == http.StatusForbidden)) ||
(method == http.MethodPost && status == http.StatusOK && statusFromMsg == http.StatusBadGateway &&
(method == http.MethodPost && statusFromMsg == 502 &&
(path == "/chains/main/blocks/head/helpers/scripts/run_script_view" ||
path == "/chains/main/blocks/head/helpers/scripts/run_view" ||
path == "/chains/main/blocks/head/helpers/scripts/pack_data"))

if err != nil && statusFromMsg == http.StatusBadGateway {
c.Logger().Infof("Error occurred with status 502. Request method: %s, path: %s, response status: %d", method, path, status)
if err != nil && statusFromMsg == 502 {
c.Logger().Infof("Error occurred with status 502. Request method: %s, path: %s, response status: %d", method, path, statusFromMsg)
c.Logger().Infof("Should retry: %v", shouldRetry)
}

if shouldRetry {
c.Logger().Infof("Triggering retry for status %d", status)
c.Logger().Infof("Triggering retry for http status %d", status)
writer.Reset()
delayedResponse.Committed = false
delayedResponse.Size = 0
Expand All @@ -62,6 +68,9 @@ func Retry(config *config.Config) echo.MiddlewareFunc {
// so we need to reset this as well.
c.Set("_error", nil)

// Reset the request body
c.Request().Body = io.NopCloser(bytes.NewBuffer(bodyBytes))

err = next(c)
}

Expand Down

0 comments on commit e9a6ef8

Please sign in to comment.