Skip to content

Commit

Permalink
Retry mercury upload requests when we fail to parse the response
Browse files Browse the repository at this point in the history
  • Loading branch information
Fizzadar committed Jul 26, 2024
1 parent b99ef34 commit bc74e44
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions messagix/mercury.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"mime/multipart"
"net/textproto"
"time"

"github.com/google/go-querystring/query"
"github.com/rs/zerolog"
Expand Down Expand Up @@ -51,17 +52,25 @@ func (c *Client) SendMercuryUploadRequest(ctx context.Context, threadID int64, m
h.Set("sec-fetch-mode", "cors")
h.Set("sec-fetch-site", "same-origin") // header is required

_, respBody, err := c.MakeRequest(url, "POST", h, payload, types.NONE)
if err != nil {
return nil, fmt.Errorf("failed to send MercuryUploadRequest: %w", err)
}

resp, err := c.parseMercuryResponse(ctx, respBody)
if err != nil {
return nil, err
var attempts int
for {
attempts += 1
_, respBody, err := c.MakeRequest(url, "POST", h, payload, types.NONE)
if err != nil {
// MakeRequest retries itself, so bail immediately if that fails
return nil, fmt.Errorf("failed to send MercuryUploadRequest: %w", err)
}
resp, err := c.parseMercuryResponse(ctx, respBody)
if err == nil {
return resp, nil
} else if attempts > MaxHTTPRetries {
return nil, err
}
c.Logger.Err(err).
Str("url", url).
Msg("Mercury response parsing failed, retrying")
time.Sleep(time.Duration(attempts) * 3 * time.Second)
}

return resp, nil
}

var antiJSPrefix = []byte("for (;;);")
Expand Down

0 comments on commit bc74e44

Please sign in to comment.