client: implement exponential backoff on temporary network errors

Uses a fuzzy timer to reduce chance of multiple clients synchronizing.
This commit is contained in:
Michael Marineau 2017-06-02 18:33:30 -07:00
parent c9e5a6a602
commit c88c5916bb
3 changed files with 78 additions and 9 deletions

View file

@ -18,14 +18,40 @@ import (
"encoding/xml" "encoding/xml"
"errors" "errors"
"io" "io"
"net"
"net/http" "net/http"
"time"
) )
var ( var (
bodySizeError = errors.New("http response exceeded 1MB") bodySizeError = errors.New("http response exceeded 1MB")
bodyEmptyError = errors.New("http response was empty") bodyEmptyError = errors.New("http response was empty")
// default parameters for expNetBackoff
backoffStart = time.Second
backoffTries = 7
) )
// retries and exponentially backs off for temporary network errors
func expNetBackoff(f func() error) error {
var (
backoff = backoffStart
tries = backoffTries
)
for {
err := f()
tries--
if tries <= 0 {
return err
}
if neterr, ok := err.(net.Error); !ok || !neterr.Temporary() {
return err
}
FuzzySleep(backoff, backoff)
backoff *= 2
}
}
// xml doesn't return the standard io.ErrUnexpectedEOF so check for both. // xml doesn't return the standard io.ErrUnexpectedEOF so check for both.
func isUnexpectedEOF(err error) bool { func isUnexpectedEOF(err error) bool {
if xerr, ok := err.(*xml.SyntaxError); ok { if xerr, ok := err.(*xml.SyntaxError); ok {

View file

@ -0,0 +1,49 @@
// Copyright 2017 CoreOS, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package client
import (
"testing"
"time"
)
func init() {
// use quicker backoff for testing
backoffStart = time.Millisecond
backoffTries = 3
}
type tmpErr struct{}
func (e tmpErr) Error() string { return "fake temporary error" }
func (e tmpErr) Temporary() bool { return true }
func (e tmpErr) Timeout() bool { return false }
func TestExpNetBackoff(t *testing.T) {
tries := 0
err := expNetBackoff(func() error {
tries++
if tries < 2 {
return tmpErr{}
}
return nil
})
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if tries != 2 {
t.Errorf("unexpected # of tries: %d", tries)
}
}

View file

@ -19,7 +19,6 @@ import (
"encoding/xml" "encoding/xml"
"fmt" "fmt"
"io" "io"
"net"
"net/http" "net/http"
"time" "time"
@ -28,7 +27,6 @@ import (
const ( const (
defaultTimeout = 90 * time.Second defaultTimeout = 90 * time.Second
defaultTries = 7
) )
// httpClient extends the standard http.Client to support xml encoding // httpClient extends the standard http.Client to support xml encoding
@ -79,14 +77,10 @@ func (hc *httpClient) Omaha(url string, req *omaha.Request) (resp *omaha.Respons
return nil, fmt.Errorf("omaha: failed to encode request: %v", err) return nil, fmt.Errorf("omaha: failed to encode request: %v", err)
} }
for i := 0; i < defaultTries; i++ { expNetBackoff(func() error {
resp, err = hc.doPost(url, buf.Bytes()) resp, err = hc.doPost(url, buf.Bytes())
if neterr, ok := err.(net.Error); ok && neterr.Temporary() { return err
// TODO(marineam): add exponential backoff })
continue
}
break
}
if err != nil { if err != nil {
return nil, fmt.Errorf("omaha: request failed: %v", err) return nil, fmt.Errorf("omaha: request failed: %v", err)
} }