| OLD | NEW |
| 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 22 matching lines...) Expand all Loading... |
| 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 // | 42 // |
| 43 // If rFn is nil, NewRequest will use a default exponential backoff strategy onl
y | 43 // If rFn is nil, NewRequest will use a default exponential backoff strategy |
| 44 // for transient errors. | 44 // only for transient errors. |
| 45 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) { |
| 46 if rFn == nil { | 46 if rFn == nil { |
| 47 rFn = retry.TransientOnly(retry.Default) | 47 rFn = retry.TransientOnly(retry.Default) |
| 48 } | 48 } |
| 49 | 49 |
| 50 return func() (int, error) { | 50 return func() (int, error) { |
| 51 status, attempts := 0, 0 | 51 status, attempts := 0, 0 |
| 52 err := retry.Retry(ctx, rFn, func() error { | 52 err := retry.Retry(ctx, rFn, func() error { |
| 53 attempts++ | 53 attempts++ |
| 54 req, err := rgen() | 54 req, err := rgen() |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 153 } | 153 } |
| 154 | 154 |
| 155 // 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. |
| 156 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) { |
| 157 req, err := NewRequestJSON(ctx, c, rFn, url, "POST", headers, in, out) | 157 req, err := NewRequestJSON(ctx, c, rFn, url, "POST", headers, in, out) |
| 158 if err != nil { | 158 if err != nil { |
| 159 return 0, err | 159 return 0, err |
| 160 } | 160 } |
| 161 return req() | 161 return req() |
| 162 } | 162 } |
| OLD | NEW |