Skip to content

Commit

Permalink
pre-compiled regex and log retry status
Browse files Browse the repository at this point in the history
  • Loading branch information
cyrilevos committed Mar 28, 2024
1 parent ae923bb commit d747812
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions middlewares/retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import (
"github.com/marigold-dev/tzproxy/config"
)

// Compile the regular expression once and store it in a global variable
var statusCodeRegex = regexp.MustCompile(`code=(\d+)`)

func Retry(config *config.Config) echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) (err error) {
Expand All @@ -36,8 +39,7 @@ func Retry(config *config.Config) echo.MiddlewareFunc {
status := c.Response().Status
// Extract the status code from the error message
if err != nil {
re := regexp.MustCompile(`code=(\d+)`)
match := re.FindStringSubmatch(err.Error())
match := statusCodeRegex.FindStringSubmatch(err.Error())
if len(match) > 1 {
statusFromMsg, _ = strconv.Atoi(match[1])
}
Expand All @@ -47,10 +49,7 @@ func Retry(config *config.Config) echo.MiddlewareFunc {
path := c.Request().URL.Path
shouldRetry := (method == http.MethodGet && (status == http.StatusNotFound || status == http.StatusForbidden)) ||
(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/simulate_operation" ||
path == "/chains/main/blocks/head/helpers/scripts/pack_data"))
strings.Contains(path, "/chains/main/blocks/head/helpers/scripts/"))

if err != nil && statusFromMsg == 502 {
c.Logger().Infof("Error occurred with status 502. Request method: %s, path: %s, response status: %d", method, path, statusFromMsg)
Expand All @@ -73,6 +72,8 @@ func Retry(config *config.Config) echo.MiddlewareFunc {
c.Request().Body = io.NopCloser(bytes.NewBuffer(bodyBytes))

err = next(c)
// Log the status after the retry attempt
c.Logger().Infof("Retry attempt status: %d", c.Response().Status)
}

c.Set("retry", nil)
Expand Down

0 comments on commit d747812

Please sign in to comment.