From f5790a5af0edd065380602080a62ae9bb0cabf07 Mon Sep 17 00:00:00 2001 From: p4u Date: Wed, 13 Mar 2024 10:45:08 +0100 Subject: [PATCH] httpclient: cut request log messages if len > 512 Signed-off-by: p4u --- apiclient/client.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/apiclient/client.go b/apiclient/client.go index 99a44aa3e..4121bfa26 100644 --- a/apiclient/client.go +++ b/apiclient/client.go @@ -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 { @@ -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{ @@ -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 } @@ -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