Skip to content

Commit

Permalink
httpclient: cut request log messages if len > 512
Browse files Browse the repository at this point in the history
Signed-off-by: p4u <[email protected]>
  • Loading branch information
p4u committed Mar 13, 2024
1 parent 15c06c2 commit ddc2a10
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion apiclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,12 @@ func (c *HTTPclient) SetHostAddr(addr *url.URL) error {
return nil
}

// SetRetries configures the number of retries for the HTTP client.
func (c *HTTPclient) SetRetries(n int) {
c.retries = n
}

// SetTimeout configures the timeout for the HTTP client.
func (c *HTTPclient) SetTimeout(d time.Duration) {
c.c.Timeout = d
if c.c.Transport != nil {
Expand Down Expand Up @@ -202,7 +204,12 @@ func (c *HTTPclient) Request(method string, jsonBody any, urlPath ...string) ([]
}
}

log.Debugw("http request", "type", method, "path", u.Path, "body", jsonBody)
log.Debugw("http request", "type", method, "path", u.Path, "body", func() string {
if len(body) > 512 {
return string(body[:512]) + "..."
}
return string(body)
})
var resp *http.Response
for i := 1; i <= c.retries; i++ {
resp, err = c.c.Do(&http.Request{
Expand All @@ -218,6 +225,7 @@ func (c *HTTPclient) Request(method string, jsonBody any, urlPath ...string) ([]
})
if resp != nil && resp.StatusCode == apirest.HTTPstatusServiceUnavailable { // mempool is full
log.Warnf("mempool is full, will wait and retry (%d/%d)", i, c.retries)
_ = resp.Body.Close()
_ = c.WaitUntilNextBlock()
continue
}
Expand All @@ -226,6 +234,7 @@ func (c *HTTPclient) Request(method string, jsonBody any, urlPath ...string) ([]
if err != nil {
return nil, 0, err
}
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
if err != nil {
return nil, 0, err
Expand Down

0 comments on commit ddc2a10

Please sign in to comment.