From 1012f97f08c3d79c67d8c7a30c3186b810b125a6 Mon Sep 17 00:00:00 2001 From: Neur0toxine Date: Tue, 13 Feb 2024 12:13:49 +0300 Subject: [PATCH] better logging for rate limiting --- v1/request.go | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/v1/request.go b/v1/request.go index 0bf02cf..74f2c7a 100644 --- a/v1/request.go +++ b/v1/request.go @@ -63,16 +63,8 @@ func makeRequest(reqType, url string, buf io.Reader, c *MgClient) ([]byte, int, req.Header.Set("Content-Type", "application/json") req.Header.Set("X-Transport-Token", c.Token) - if c.Debug { - if strings.Contains(url, "/files/upload") { - c.writeLog("MG TRANSPORT API Request: %s %s %s [file data]", reqType, url, c.Token) - } else { - c.writeLog("MG TRANSPORT API Request: %s %s %s %v", reqType, url, c.Token, buf) - } - } - - c.mux.Lock() defer c.mux.Unlock() + c.mux.Lock() attempt := 0 tryAgain: @@ -87,6 +79,14 @@ tryAgain: } c.rps++ + if c.Debug { + if strings.Contains(url, "/files/upload") { + c.writeLog("MG TRANSPORT API Request: %s %s %s [file data]", reqType, url, c.Token) + } else { + c.writeLog("MG TRANSPORT API Request: %s %s %s %v", reqType, url, c.Token, buf) + } + } + resp, err := c.httpClient.Do(req) if err != nil { return res, 0, NewCriticalHTTPError(err) @@ -94,6 +94,7 @@ tryAgain: if resp.StatusCode == http.StatusTooManyRequests && attempt < 3 { attempt++ + c.writeLog("MG TRANSPORT API Request rate limit hit on attempt %d, retrying", attempt) goto tryAgain }