X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/2924f222d9efdb1b8776225d2d51bc8771d7b077..5dd128f5a57e704e3b3ea5225130ca85bd3bb84c:/sdk/go/arvados/client.go diff --git a/sdk/go/arvados/client.go b/sdk/go/arvados/client.go index 5a498b01f0..d1a87e30e8 100644 --- a/sdk/go/arvados/client.go +++ b/sdk/go/arvados/client.go @@ -23,9 +23,11 @@ import ( "os" "regexp" "strings" + "sync/atomic" "time" "git.arvados.org/arvados.git/sdk/go/httpserver" + "github.com/hashicorp/go-retryablehttp" ) // A Client is an HTTP client with an API endpoint and a set of @@ -64,9 +66,11 @@ type Client struct { // Timeout for requests. NewClientFromConfig and // NewClientFromEnv return a Client with a default 5 minute - // timeout. To disable this timeout and rely on each - // http.Request's context deadline instead, set Timeout to - // zero. + // timeout. Within this time, retryable errors are + // automatically retried with exponential backoff. + // + // To disable automatic retries, set Timeout to zero and use a + // context deadline to establish a maximum request time. Timeout time.Duration dd *DiscoveryDocument @@ -76,6 +80,14 @@ type Client struct { // APIHost and AuthToken were loaded from ARVADOS_* env vars // (used to customize "no host/token" error messages) loadedFromEnv bool + + // Track/limit concurrent outgoing API calls. Note this + // differs from an outgoing connection limit (a feature + // provided by http.Transport) when concurrent calls are + // multiplexed on a single http2 connection. + requestLimiter requestLimiter + + last503 atomic.Value } // InsecureHTTPClient is the default http.Client used by a Client with @@ -220,10 +232,14 @@ func NewClientFromEnv() *Client { var reqIDGen = httpserver.IDGenerator{Prefix: "req-"} -// Do adds Authorization and X-Request-Id headers and then calls -// (*http.Client)Do(). +var nopCancelFunc context.CancelFunc = func() {} + +// Do augments (*http.Client)Do(): adds Authorization and X-Request-Id +// headers, delays in order to comply with rate-limiting restrictions, +// and retries failed requests when appropriate. func (c *Client) Do(req *http.Request) (*http.Response, error) { - if auth, _ := req.Context().Value(contextKeyAuthorization{}).(string); auth != "" { + ctx := req.Context() + if auth, _ := ctx.Value(contextKeyAuthorization{}).(string); auth != "" { req.Header.Add("Authorization", auth) } else if c.AuthToken != "" { req.Header.Add("Authorization", "OAuth2 "+c.AuthToken) @@ -231,7 +247,7 @@ func (c *Client) Do(req *http.Request) (*http.Response, error) { if req.Header.Get("X-Request-Id") == "" { var reqid string - if ctxreqid, _ := req.Context().Value(contextKeyRequestID{}).(string); ctxreqid != "" { + if ctxreqid, _ := ctx.Value(contextKeyRequestID{}).(string); ctxreqid != "" { reqid = ctxreqid } else if c.defaultRequestID != "" { reqid = c.defaultRequestID @@ -244,25 +260,93 @@ func (c *Client) Do(req *http.Request) (*http.Response, error) { req.Header.Set("X-Request-Id", reqid) } } - var cancel context.CancelFunc + + rreq, err := retryablehttp.FromRequest(req) + if err != nil { + return nil, err + } + + cancel := nopCancelFunc + var lastResp *http.Response + var lastRespBody io.ReadCloser + var lastErr error + + rclient := retryablehttp.NewClient() + rclient.HTTPClient = c.httpClient() if c.Timeout > 0 { - ctx := req.Context() + rclient.RetryWaitMax = c.Timeout / 10 + rclient.RetryMax = 32 ctx, cancel = context.WithDeadline(ctx, time.Now().Add(c.Timeout)) - req = req.WithContext(ctx) - } - resp, err := c.httpClient().Do(req) - if err == nil && cancel != nil { - // We need to call cancel() eventually, but we can't - // use "defer cancel()" because the context has to - // stay alive until the caller has finished reading - // the response body. - resp.Body = cancelOnClose{ReadCloser: resp.Body, cancel: cancel} - } else if cancel != nil { + rreq = rreq.WithContext(ctx) + } else { + rclient.RetryMax = 0 + } + rclient.CheckRetry = func(ctx context.Context, resp *http.Response, respErr error) (bool, error) { + if c.requestLimiter.Report(resp, respErr) { + c.last503.Store(time.Now()) + } + if c.Timeout == 0 { + return false, err + } + retrying, err := retryablehttp.DefaultRetryPolicy(ctx, resp, respErr) + if retrying { + lastResp, lastRespBody, lastErr = resp, nil, respErr + if respErr == nil { + // Save the response and body so we + // can return it instead of "deadline + // exceeded". retryablehttp.Client + // will drain and discard resp.body, + // so we need to stash it separately. + buf, err := ioutil.ReadAll(resp.Body) + if err == nil { + lastRespBody = io.NopCloser(bytes.NewReader(buf)) + } else { + lastResp, lastErr = nil, err + } + } + } + return retrying, err + } + rclient.Logger = nil + + c.requestLimiter.Acquire(ctx) + if ctx.Err() != nil { + c.requestLimiter.Release() cancel() + return nil, ctx.Err() + } + resp, err := rclient.Do(rreq) + if (errors.Is(err, context.DeadlineExceeded) || errors.Is(err, context.Canceled)) && (lastResp != nil || lastErr != nil) { + resp, err = lastResp, lastErr + if resp != nil { + resp.Body = lastRespBody + } + } + if err != nil { + c.requestLimiter.Release() + cancel() + return nil, err + } + // We need to call cancel() eventually, but we can't use + // "defer cancel()" because the context has to stay alive + // until the caller has finished reading the response body. + resp.Body = cancelOnClose{ + ReadCloser: resp.Body, + cancel: func() { + c.requestLimiter.Release() + cancel() + }, } return resp, err } +// Last503 returns the time of the most recent HTTP 503 (Service +// Unavailable) response. Zero time indicates never. +func (c *Client) Last503() time.Time { + t, _ := c.last503.Load().(time.Time) + return t +} + // cancelOnClose calls a provided CancelFunc when its wrapped // ReadCloser's Close() method is called. type cancelOnClose struct {