Merge pull request #103 from cloudflare/jkroll/backoff

Add exponential backoff to the Red October client
This commit is contained in:
Kyle Isom
2015-11-24 15:25:33 -08:00

View File

@@ -8,8 +8,11 @@ import (
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"time"
backoff "github.com/cloudflare/cfssl/transport/core"
"github.com/cloudflare/redoctober/core"
)
@@ -59,9 +62,17 @@ func (c *RemoteServer) getURL(path string) string {
func (c *RemoteServer) doAction(action string, req []byte) ([]byte, error) {
buf := bytes.NewBuffer(req)
url := c.getURL("/" + action)
resp, err := c.client.Post(url, "application/json", buf)
if err != nil {
return nil, err
b := backoff.Backoff{}
var resp *http.Response
var err error
for {
resp, err = c.client.Post(url, "application/json", buf)
if err == nil {
break
}
delay := b.Duration()
log.Printf("Request to server failed. Will try again in %s", delay)
<-time.After(delay)
}
body, err := ioutil.ReadAll(resp.Body)