Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(13)

Side by Side Diff: common/lhttp/client.go

Issue 2568953004: common/lhttp: only retry transient errors (Closed)
Patch Set: Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | common/lhttp/client_test.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The LUCI Authors. All rights reserved. 1 // Copyright 2015 The LUCI Authors. All rights reserved.
2 // Use of this source code is governed under the Apache License, Version 2.0 2 // Use of this source code is governed under the Apache License, Version 2.0
3 // that can be found in the LICENSE file. 3 // that can be found in the LICENSE file.
4 4
5 package lhttp 5 package lhttp
6 6
7 import ( 7 import (
8 "bytes" 8 "bytes"
9 "encoding/json" 9 "encoding/json"
10 "fmt" 10 "fmt"
(...skipping 21 matching lines...) Expand all
32 // multiple times if an operation needs to be retried. The HTTP server is 32 // multiple times if an operation needs to be retried. The HTTP server is
33 // responsible for closing the Request body, as per http.Request Body method 33 // responsible for closing the Request body, as per http.Request Body method
34 // documentation. 34 // documentation.
35 type RequestGen func() (*http.Request, error) 35 type RequestGen func() (*http.Request, error)
36 36
37 // NewRequest returns a retriable request. 37 // NewRequest returns a retriable request.
38 // 38 //
39 // The handler func is responsible for closing the response Body before 39 // The handler func is responsible for closing the response Body before
40 // returning. It should return retry.Error in case of retriable error, for 40 // returning. It should return retry.Error in case of retriable error, for
41 // example if a TCP connection is terminated while receiving the content. 41 // example if a TCP connection is terminated while receiving the content.
42 //
43 // If rFn is nil, NewRequest will use a default exponential backoff strategy onl y
dsansome-google 2016/12/13 02:36:14 Wrap to 80-chars like the other comments
44 // for transient errors.
42 func NewRequest(ctx context.Context, c *http.Client, rFn retry.Factory, rgen Req uestGen, handler Handler) func() (int, error) { 45 func NewRequest(ctx context.Context, c *http.Client, rFn retry.Factory, rgen Req uestGen, handler Handler) func() (int, error) {
43 if rFn == nil { 46 if rFn == nil {
44 » » rFn = retry.Default 47 » » rFn = retry.TransientOnly(retry.Default)
45 } 48 }
46 49
47 return func() (int, error) { 50 return func() (int, error) {
48 status, attempts := 0, 0 51 status, attempts := 0, 0
49 err := retry.Retry(ctx, rFn, func() error { 52 err := retry.Retry(ctx, rFn, func() error {
50 attempts++ 53 attempts++
51 req, err := rgen() 54 req, err := rgen()
52 if err != nil { 55 if err != nil {
53 return err 56 return err
54 } 57 }
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
150 } 153 }
151 154
152 // PostJSON is a shorthand. It returns the HTTP status code and error if any. 155 // PostJSON is a shorthand. It returns the HTTP status code and error if any.
153 func PostJSON(ctx context.Context, rFn retry.Factory, c *http.Client, url string , headers map[string]string, in, out interface{}) (int, error) { 156 func PostJSON(ctx context.Context, rFn retry.Factory, c *http.Client, url string , headers map[string]string, in, out interface{}) (int, error) {
154 req, err := NewRequestJSON(ctx, c, rFn, url, "POST", headers, in, out) 157 req, err := NewRequestJSON(ctx, c, rFn, url, "POST", headers, in, out)
155 if err != nil { 158 if err != nil {
156 return 0, err 159 return 0, err
157 } 160 }
158 return req() 161 return req()
159 } 162 }
OLDNEW
« no previous file with comments | « no previous file | common/lhttp/client_test.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698