Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rpcclient: cherry-pick prereq fixes #29

Merged
merged 4 commits into from
Mar 29, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
58 changes: 41 additions & 17 deletions rpcclient/infrastructure.go
Original file line number Diff line number Diff line change
Expand Up @@ -761,20 +761,27 @@ 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"
}
url := protocol + "://" + c.config.Host

var err error
var backoff time.Duration
var httpResponse *http.Response
var (
err, lastErr error
backoff time.Duration
httpResponse *http.Response
)

tries := 10
for i := 0; tries == 0 || i < tries; i++ {
for i := 0; i < tries; i++ {
var httpReq *http.Request

bodyReader := bytes.NewReader(jReq.marshalledJSON)
httpReq, err := http.NewRequest("POST", url, bodyReader)
httpReq, err = http.NewRequest("POST", url, bodyReader)
if err != nil {
jReq.responseChan <- &Response{result: nil, err: err}
return
Expand All @@ -794,22 +801,39 @@ 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
}

// Save the last error for the case where we backoff further,
// retry and get an invalid response but no error. If this
// happens the saved last error will be used to enrich the error
// message that we pass back to the caller.
lastErr = err

// 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}
return
}
// We still want to return an error if for any reason the respone
// We still want to return an error if for any reason the response
// remains empty.
if httpResponse == nil {
jReq.responseChan <- &Response{
Expand Down Expand Up @@ -868,7 +892,7 @@ out:
// is closed.
select {
case jReq := <-c.sendPostChan:
c.handleSendPostMessage(jReq)
c.handleSendPostMessage(jReq, c.shutdown)

case <-c.shutdown:
break out
Expand Down