Merge pull request #3037 from smarterclayton/hide_spurious_watch_errors

When connections are broken on Watch, write fewer errors to logs
This commit is contained in:
Daniel Smith
2014-12-18 13:33:03 -08:00
5 changed files with 103 additions and 1 deletions

View File

@@ -25,6 +25,7 @@ import (
"net/url"
"path"
"strconv"
"strings"
"time"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
@@ -298,6 +299,9 @@ func (r *Request) Watch() (watch.Interface, error) {
}
resp, err := client.Do(req)
if err != nil {
if isProbableEOF(err) {
return watch.NewEmptyWatch(), nil
}
return nil, err
}
if resp.StatusCode != http.StatusOK {
@@ -310,6 +314,25 @@ func (r *Request) Watch() (watch.Interface, error) {
return watch.NewStreamWatcher(watchjson.NewDecoder(resp.Body, r.codec)), nil
}
// isProbableEOF returns true if the given error resembles a connection termination
// scenario that would justify assuming that the watch is empty. The watch stream
// mechanism handles many common partial data errors, so closed connections can be
// retried in many cases.
func isProbableEOF(err error) bool {
if uerr, ok := err.(*url.Error); ok {
err = uerr.Err
}
switch {
case err == io.EOF:
return true
case err.Error() == "http: can't write HTTP request on broken connection":
return true
case strings.Contains(err.Error(), "connection reset by peer"):
return true
}
return false
}
// Stream formats and executes the request, and offers streaming of the response.
// Returns io.ReadCloser which could be used for streaming of the response, or an error
func (r *Request) Stream() (io.ReadCloser, error) {