Skip to content

Commit

Permalink
create a HTTPclient method to modify the requests timeout (#1269)
Browse files Browse the repository at this point in the history
* create a HTTPclient method to modify the requests timeout
* simple check to avoid panic
  • Loading branch information
lucasmenendez authored Mar 7, 2024
1 parent 79356ab commit ae3efeb
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions apiclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ const (
// DefaultRetries this enables Request() to handle the situation where the server replies
// "mempool is full", it will wait for next block and retry sending the tx
DefaultRetries = 3
// DefaultTimeout is the default timeout for the HTTP client
DefaultTimeout = 10 * time.Second
)

// HTTPclient is the Vocdoni API HTTP client.
Expand All @@ -48,13 +50,13 @@ type HTTPclient struct {
// NewHTTPclient creates a new HTTP(s) API Vocdoni client.
func NewHTTPclient(addr *url.URL, bearerToken *uuid.UUID) (*HTTPclient, error) {
tr := &http.Transport{
IdleConnTimeout: 10 * time.Second,
IdleConnTimeout: DefaultTimeout,
DisableCompression: false,
WriteBufferSize: 1 * 1024 * 1024, // 1 MiB
ReadBufferSize: 1 * 1024 * 1024, // 1 MiB
}
c := &HTTPclient{
c: &http.Client{Transport: tr, Timeout: time.Second * 8},
c: &http.Client{Transport: tr, Timeout: DefaultTimeout},
token: bearerToken,
addr: addr,
retries: DefaultRetries,
Expand Down Expand Up @@ -144,6 +146,15 @@ func (c *HTTPclient) SetRetries(n int) {
c.retries = n
}

func (c *HTTPclient) SetTimeout(d time.Duration) {
c.c.Timeout = d
if c.c.Transport != nil {
if _, ok := c.c.Transport.(*http.Transport); ok {
c.c.Transport.(*http.Transport).ResponseHeaderTimeout = d
}
}
}

// Request performs a `method` type raw request to the endpoint specified in urlPath parameter.
// Method is either GET or POST. If POST, a JSON struct should be attached. Returns the response,
// the status code and an error.
Expand Down

0 comments on commit ae3efeb

Please sign in to comment.