From 5a748b9249a817ac31f728be0b884cb9a589034b Mon Sep 17 00:00:00 2001 From: Joshua Kroll Date: Sat, 21 Nov 2015 04:07:13 -0800 Subject: [PATCH] Add exponential backoff to the Red October client, following the pattern in the CFSSL transport package client. --- client/client.go | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/client/client.go b/client/client.go index abb18f3..c12566f 100644 --- a/client/client.go +++ b/client/client.go @@ -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)