Skip to content

Commit

Permalink
rpcclient: fix backoff logic
Browse files Browse the repository at this point in the history
This commit removes Sleep() from the rety handler so that the shutdown
request is always respected. Furthermore the maximum retry count is corrected.
  • Loading branch information
bhandras committed May 13, 2022
1 parent 9665112 commit 9babf1f
Showing 1 changed file with 24 additions and 13 deletions.
37 changes: 24 additions & 13 deletions rpcclient/infrastructure.go
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,9 @@ out:
// handleSendPostMessage handles performing the passed HTTP request, reading the
// result, unmarshalling it, and delivering the unmarshalled result to the
// provided response channel.
func (c *Client) handleSendPostMessage(jReq *jsonRequest) {
func (c *Client) handleSendPostMessage(jReq *jsonRequest,
shutdown chan struct{}) {

protocol := "http"
if !c.config.DisableTLS {
protocol = "https"
Expand Down Expand Up @@ -799,18 +801,27 @@ func (c *Client) handleSendPostMessage(jReq *jsonRequest) {
httpReq.SetBasicAuth(user, pass)

httpResponse, err = c.httpClient.Do(httpReq)
if err != nil {
backoff = requestRetryInterval * time.Duration(i+1)
if backoff > time.Minute {
backoff = time.Minute
}
log.Debugf("Failed command [%s] with id %d attempt %d."+
" Retrying in %v... \n", jReq.method, jReq.id,
i, backoff)
time.Sleep(backoff)
continue

// Quit the retry loop on success or if we can't retry anymore.
if err == nil || i == tries-1 {
break
}

// Backoff sleep otherwise.
backoff = requestRetryInterval * time.Duration(i+1)
if backoff > time.Minute {
backoff = time.Minute
}
log.Debugf("Failed command [%s] with id %d attempt %d."+
" Retrying in %v... \n", jReq.method, jReq.id,
i, backoff)

select {
case <-time.After(backoff):

case <-shutdown:
return
}
break
}
if err != nil {
jReq.responseChan <- &Response{err: err}
Expand Down Expand Up @@ -874,7 +885,7 @@ out:
// is closed.
select {
case jReq := <-c.sendPostChan:
c.handleSendPostMessage(jReq)
c.handleSendPostMessage(jReq, c.shutdown)

case <-c.shutdown:
break out
Expand Down

0 comments on commit 9babf1f

Please sign in to comment.