diff options
Diffstat (limited to 'vendor/github.com/googleapis/gax-go')
-rw-r--r-- | vendor/github.com/googleapis/gax-go/v2/LICENSE | 27 | ||||
-rw-r--r-- | vendor/github.com/googleapis/gax-go/v2/call_option.go | 161 | ||||
-rw-r--r-- | vendor/github.com/googleapis/gax-go/v2/gax.go | 39 | ||||
-rw-r--r-- | vendor/github.com/googleapis/gax-go/v2/go.mod | 3 | ||||
-rw-r--r-- | vendor/github.com/googleapis/gax-go/v2/go.sum | 26 | ||||
-rw-r--r-- | vendor/github.com/googleapis/gax-go/v2/header.go | 53 | ||||
-rw-r--r-- | vendor/github.com/googleapis/gax-go/v2/invoke.go | 99 |
7 files changed, 408 insertions, 0 deletions
diff --git a/vendor/github.com/googleapis/gax-go/v2/LICENSE b/vendor/github.com/googleapis/gax-go/v2/LICENSE new file mode 100644 index 0000000..6d16b65 --- /dev/null +++ b/vendor/github.com/googleapis/gax-go/v2/LICENSE | |||
@@ -0,0 +1,27 @@ | |||
1 | Copyright 2016, Google Inc. | ||
2 | All rights reserved. | ||
3 | Redistribution and use in source and binary forms, with or without | ||
4 | modification, are permitted provided that the following conditions are | ||
5 | met: | ||
6 | |||
7 | * Redistributions of source code must retain the above copyright | ||
8 | notice, this list of conditions and the following disclaimer. | ||
9 | * Redistributions in binary form must reproduce the above | ||
10 | copyright notice, this list of conditions and the following disclaimer | ||
11 | in the documentation and/or other materials provided with the | ||
12 | distribution. | ||
13 | * Neither the name of Google Inc. nor the names of its | ||
14 | contributors may be used to endorse or promote products derived from | ||
15 | this software without specific prior written permission. | ||
16 | |||
17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
18 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
19 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
20 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
21 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
22 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
23 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
24 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
25 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
26 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
27 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
diff --git a/vendor/github.com/googleapis/gax-go/v2/call_option.go b/vendor/github.com/googleapis/gax-go/v2/call_option.go new file mode 100644 index 0000000..b1d53dd --- /dev/null +++ b/vendor/github.com/googleapis/gax-go/v2/call_option.go | |||
@@ -0,0 +1,161 @@ | |||
1 | // Copyright 2016, Google Inc. | ||
2 | // All rights reserved. | ||
3 | // | ||
4 | // Redistribution and use in source and binary forms, with or without | ||
5 | // modification, are permitted provided that the following conditions are | ||
6 | // met: | ||
7 | // | ||
8 | // * Redistributions of source code must retain the above copyright | ||
9 | // notice, this list of conditions and the following disclaimer. | ||
10 | // * Redistributions in binary form must reproduce the above | ||
11 | // copyright notice, this list of conditions and the following disclaimer | ||
12 | // in the documentation and/or other materials provided with the | ||
13 | // distribution. | ||
14 | // * Neither the name of Google Inc. nor the names of its | ||
15 | // contributors may be used to endorse or promote products derived from | ||
16 | // this software without specific prior written permission. | ||
17 | // | ||
18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
29 | |||
30 | package gax | ||
31 | |||
32 | import ( | ||
33 | "math/rand" | ||
34 | "time" | ||
35 | |||
36 | "google.golang.org/grpc" | ||
37 | "google.golang.org/grpc/codes" | ||
38 | "google.golang.org/grpc/status" | ||
39 | ) | ||
40 | |||
41 | // CallOption is an option used by Invoke to control behaviors of RPC calls. | ||
42 | // CallOption works by modifying relevant fields of CallSettings. | ||
43 | type CallOption interface { | ||
44 | // Resolve applies the option by modifying cs. | ||
45 | Resolve(cs *CallSettings) | ||
46 | } | ||
47 | |||
48 | // Retryer is used by Invoke to determine retry behavior. | ||
49 | type Retryer interface { | ||
50 | // Retry reports whether a request should be retriedand how long to pause before retrying | ||
51 | // if the previous attempt returned with err. Invoke never calls Retry with nil error. | ||
52 | Retry(err error) (pause time.Duration, shouldRetry bool) | ||
53 | } | ||
54 | |||
55 | type retryerOption func() Retryer | ||
56 | |||
57 | func (o retryerOption) Resolve(s *CallSettings) { | ||
58 | s.Retry = o | ||
59 | } | ||
60 | |||
61 | // WithRetry sets CallSettings.Retry to fn. | ||
62 | func WithRetry(fn func() Retryer) CallOption { | ||
63 | return retryerOption(fn) | ||
64 | } | ||
65 | |||
66 | // OnCodes returns a Retryer that retries if and only if | ||
67 | // the previous attempt returns a GRPC error whose error code is stored in cc. | ||
68 | // Pause times between retries are specified by bo. | ||
69 | // | ||
70 | // bo is only used for its parameters; each Retryer has its own copy. | ||
71 | func OnCodes(cc []codes.Code, bo Backoff) Retryer { | ||
72 | return &boRetryer{ | ||
73 | backoff: bo, | ||
74 | codes: append([]codes.Code(nil), cc...), | ||
75 | } | ||
76 | } | ||
77 | |||
78 | type boRetryer struct { | ||
79 | backoff Backoff | ||
80 | codes []codes.Code | ||
81 | } | ||
82 | |||
83 | func (r *boRetryer) Retry(err error) (time.Duration, bool) { | ||
84 | st, ok := status.FromError(err) | ||
85 | if !ok { | ||
86 | return 0, false | ||
87 | } | ||
88 | c := st.Code() | ||
89 | for _, rc := range r.codes { | ||
90 | if c == rc { | ||
91 | return r.backoff.Pause(), true | ||
92 | } | ||
93 | } | ||
94 | return 0, false | ||
95 | } | ||
96 | |||
97 | // Backoff implements exponential backoff. | ||
98 | // The wait time between retries is a random value between 0 and the "retry envelope". | ||
99 | // The envelope starts at Initial and increases by the factor of Multiplier every retry, | ||
100 | // but is capped at Max. | ||
101 | type Backoff struct { | ||
102 | // Initial is the initial value of the retry envelope, defaults to 1 second. | ||
103 | Initial time.Duration | ||
104 | |||
105 | // Max is the maximum value of the retry envelope, defaults to 30 seconds. | ||
106 | Max time.Duration | ||
107 | |||
108 | // Multiplier is the factor by which the retry envelope increases. | ||
109 | // It should be greater than 1 and defaults to 2. | ||
110 | Multiplier float64 | ||
111 | |||
112 | // cur is the current retry envelope | ||
113 | cur time.Duration | ||
114 | } | ||
115 | |||
116 | // Pause returns the next time.Duration that the caller should use to backoff. | ||
117 | func (bo *Backoff) Pause() time.Duration { | ||
118 | if bo.Initial == 0 { | ||
119 | bo.Initial = time.Second | ||
120 | } | ||
121 | if bo.cur == 0 { | ||
122 | bo.cur = bo.Initial | ||
123 | } | ||
124 | if bo.Max == 0 { | ||
125 | bo.Max = 30 * time.Second | ||
126 | } | ||
127 | if bo.Multiplier < 1 { | ||
128 | bo.Multiplier = 2 | ||
129 | } | ||
130 | // Select a duration between 1ns and the current max. It might seem | ||
131 | // counterintuitive to have so much jitter, but | ||
132 | // https://www.awsarchitectureblog.com/2015/03/backoff.html argues that | ||
133 | // that is the best strategy. | ||
134 | d := time.Duration(1 + rand.Int63n(int64(bo.cur))) | ||
135 | bo.cur = time.Duration(float64(bo.cur) * bo.Multiplier) | ||
136 | if bo.cur > bo.Max { | ||
137 | bo.cur = bo.Max | ||
138 | } | ||
139 | return d | ||
140 | } | ||
141 | |||
142 | type grpcOpt []grpc.CallOption | ||
143 | |||
144 | func (o grpcOpt) Resolve(s *CallSettings) { | ||
145 | s.GRPC = o | ||
146 | } | ||
147 | |||
148 | // WithGRPCOptions allows passing gRPC call options during client creation. | ||
149 | func WithGRPCOptions(opt ...grpc.CallOption) CallOption { | ||
150 | return grpcOpt(append([]grpc.CallOption(nil), opt...)) | ||
151 | } | ||
152 | |||
153 | // CallSettings allow fine-grained control over how calls are made. | ||
154 | type CallSettings struct { | ||
155 | // Retry returns a Retryer to be used to control retry logic of a method call. | ||
156 | // If Retry is nil or the returned Retryer is nil, the call will not be retried. | ||
157 | Retry func() Retryer | ||
158 | |||
159 | // CallOptions to be forwarded to GRPC. | ||
160 | GRPC []grpc.CallOption | ||
161 | } | ||
diff --git a/vendor/github.com/googleapis/gax-go/v2/gax.go b/vendor/github.com/googleapis/gax-go/v2/gax.go new file mode 100644 index 0000000..8040dcb --- /dev/null +++ b/vendor/github.com/googleapis/gax-go/v2/gax.go | |||
@@ -0,0 +1,39 @@ | |||
1 | // Copyright 2016, Google Inc. | ||
2 | // All rights reserved. | ||
3 | // | ||
4 | // Redistribution and use in source and binary forms, with or without | ||
5 | // modification, are permitted provided that the following conditions are | ||
6 | // met: | ||
7 | // | ||
8 | // * Redistributions of source code must retain the above copyright | ||
9 | // notice, this list of conditions and the following disclaimer. | ||
10 | // * Redistributions in binary form must reproduce the above | ||
11 | // copyright notice, this list of conditions and the following disclaimer | ||
12 | // in the documentation and/or other materials provided with the | ||
13 | // distribution. | ||
14 | // * Neither the name of Google Inc. nor the names of its | ||
15 | // contributors may be used to endorse or promote products derived from | ||
16 | // this software without specific prior written permission. | ||
17 | // | ||
18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
29 | |||
30 | // Package gax contains a set of modules which aid the development of APIs | ||
31 | // for clients and servers based on gRPC and Google API conventions. | ||
32 | // | ||
33 | // Application code will rarely need to use this library directly. | ||
34 | // However, code generated automatically from API definition files can use it | ||
35 | // to simplify code generation and to provide more convenient and idiomatic API surfaces. | ||
36 | package gax | ||
37 | |||
38 | // Version specifies the gax-go version being used. | ||
39 | const Version = "2.0.3" | ||
diff --git a/vendor/github.com/googleapis/gax-go/v2/go.mod b/vendor/github.com/googleapis/gax-go/v2/go.mod new file mode 100644 index 0000000..c88c205 --- /dev/null +++ b/vendor/github.com/googleapis/gax-go/v2/go.mod | |||
@@ -0,0 +1,3 @@ | |||
1 | module github.com/googleapis/gax-go/v2 | ||
2 | |||
3 | require google.golang.org/grpc v1.16.0 | ||
diff --git a/vendor/github.com/googleapis/gax-go/v2/go.sum b/vendor/github.com/googleapis/gax-go/v2/go.sum new file mode 100644 index 0000000..bad34ab --- /dev/null +++ b/vendor/github.com/googleapis/gax-go/v2/go.sum | |||
@@ -0,0 +1,26 @@ | |||
1 | cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= | ||
2 | github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= | ||
3 | github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58= | ||
4 | github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= | ||
5 | github.com/golang/lint v0.0.0-20180702182130-06c8688daad7/go.mod h1:tluoj9z5200jBnyusfRPU2LqT6J+DAorxEvtC7LHB+E= | ||
6 | github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= | ||
7 | github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM= | ||
8 | github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= | ||
9 | github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= | ||
10 | golang.org/x/lint v0.0.0-20180702182130-06c8688daad7/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= | ||
11 | golang.org/x/net v0.0.0-20180826012351-8a410e7b638d h1:g9qWBGx4puODJTMVyoPrpoxPFgVGd+z1DZwjfRu4d0I= | ||
12 | golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= | ||
13 | golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= | ||
14 | golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f h1:wMNYb4v58l5UBM7MYRLPG6ZhfOqbKu7X5eyFl8ZhKvA= | ||
15 | golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= | ||
16 | golang.org/x/sys v0.0.0-20180830151530-49385e6e1522 h1:Ve1ORMCxvRmSXBwJK+t3Oy+V2vRW2OetUQBq4rJIkZE= | ||
17 | golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= | ||
18 | golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= | ||
19 | golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= | ||
20 | golang.org/x/tools v0.0.0-20180828015842-6cd1fcedba52/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= | ||
21 | google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= | ||
22 | google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8 h1:Nw54tB0rB7hY/N0NQvRW8DG4Yk3Q6T9cu9RcFQDu1tc= | ||
23 | google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= | ||
24 | google.golang.org/grpc v1.16.0 h1:dz5IJGuC2BB7qXR5AyHNwAUBhZscK2xVez7mznh72sY= | ||
25 | google.golang.org/grpc v1.16.0/go.mod h1:0JHn/cJsOMiMfNA9+DeHDlAU7KAAB5GDlYFpa9MZMio= | ||
26 | honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= | ||
diff --git a/vendor/github.com/googleapis/gax-go/v2/header.go b/vendor/github.com/googleapis/gax-go/v2/header.go new file mode 100644 index 0000000..139371a --- /dev/null +++ b/vendor/github.com/googleapis/gax-go/v2/header.go | |||
@@ -0,0 +1,53 @@ | |||
1 | // Copyright 2018, Google Inc. | ||
2 | // All rights reserved. | ||
3 | // | ||
4 | // Redistribution and use in source and binary forms, with or without | ||
5 | // modification, are permitted provided that the following conditions are | ||
6 | // met: | ||
7 | // | ||
8 | // * Redistributions of source code must retain the above copyright | ||
9 | // notice, this list of conditions and the following disclaimer. | ||
10 | // * Redistributions in binary form must reproduce the above | ||
11 | // copyright notice, this list of conditions and the following disclaimer | ||
12 | // in the documentation and/or other materials provided with the | ||
13 | // distribution. | ||
14 | // * Neither the name of Google Inc. nor the names of its | ||
15 | // contributors may be used to endorse or promote products derived from | ||
16 | // this software without specific prior written permission. | ||
17 | // | ||
18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
29 | |||
30 | package gax | ||
31 | |||
32 | import "bytes" | ||
33 | |||
34 | // XGoogHeader is for use by the Google Cloud Libraries only. | ||
35 | // | ||
36 | // XGoogHeader formats key-value pairs. | ||
37 | // The resulting string is suitable for x-goog-api-client header. | ||
38 | func XGoogHeader(keyval ...string) string { | ||
39 | if len(keyval) == 0 { | ||
40 | return "" | ||
41 | } | ||
42 | if len(keyval)%2 != 0 { | ||
43 | panic("gax.Header: odd argument count") | ||
44 | } | ||
45 | var buf bytes.Buffer | ||
46 | for i := 0; i < len(keyval); i += 2 { | ||
47 | buf.WriteByte(' ') | ||
48 | buf.WriteString(keyval[i]) | ||
49 | buf.WriteByte('/') | ||
50 | buf.WriteString(keyval[i+1]) | ||
51 | } | ||
52 | return buf.String()[1:] | ||
53 | } | ||
diff --git a/vendor/github.com/googleapis/gax-go/v2/invoke.go b/vendor/github.com/googleapis/gax-go/v2/invoke.go new file mode 100644 index 0000000..fe31dd0 --- /dev/null +++ b/vendor/github.com/googleapis/gax-go/v2/invoke.go | |||
@@ -0,0 +1,99 @@ | |||
1 | // Copyright 2016, Google Inc. | ||
2 | // All rights reserved. | ||
3 | // | ||
4 | // Redistribution and use in source and binary forms, with or without | ||
5 | // modification, are permitted provided that the following conditions are | ||
6 | // met: | ||
7 | // | ||
8 | // * Redistributions of source code must retain the above copyright | ||
9 | // notice, this list of conditions and the following disclaimer. | ||
10 | // * Redistributions in binary form must reproduce the above | ||
11 | // copyright notice, this list of conditions and the following disclaimer | ||
12 | // in the documentation and/or other materials provided with the | ||
13 | // distribution. | ||
14 | // * Neither the name of Google Inc. nor the names of its | ||
15 | // contributors may be used to endorse or promote products derived from | ||
16 | // this software without specific prior written permission. | ||
17 | // | ||
18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
29 | |||
30 | package gax | ||
31 | |||
32 | import ( | ||
33 | "context" | ||
34 | "strings" | ||
35 | "time" | ||
36 | ) | ||
37 | |||
38 | // APICall is a user defined call stub. | ||
39 | type APICall func(context.Context, CallSettings) error | ||
40 | |||
41 | // Invoke calls the given APICall, | ||
42 | // performing retries as specified by opts, if any. | ||
43 | func Invoke(ctx context.Context, call APICall, opts ...CallOption) error { | ||
44 | var settings CallSettings | ||
45 | for _, opt := range opts { | ||
46 | opt.Resolve(&settings) | ||
47 | } | ||
48 | return invoke(ctx, call, settings, Sleep) | ||
49 | } | ||
50 | |||
51 | // Sleep is similar to time.Sleep, but it can be interrupted by ctx.Done() closing. | ||
52 | // If interrupted, Sleep returns ctx.Err(). | ||
53 | func Sleep(ctx context.Context, d time.Duration) error { | ||
54 | t := time.NewTimer(d) | ||
55 | select { | ||
56 | case <-ctx.Done(): | ||
57 | t.Stop() | ||
58 | return ctx.Err() | ||
59 | case <-t.C: | ||
60 | return nil | ||
61 | } | ||
62 | } | ||
63 | |||
64 | type sleeper func(ctx context.Context, d time.Duration) error | ||
65 | |||
66 | // invoke implements Invoke, taking an additional sleeper argument for testing. | ||
67 | func invoke(ctx context.Context, call APICall, settings CallSettings, sp sleeper) error { | ||
68 | var retryer Retryer | ||
69 | for { | ||
70 | err := call(ctx, settings) | ||
71 | if err == nil { | ||
72 | return nil | ||
73 | } | ||
74 | if settings.Retry == nil { | ||
75 | return err | ||
76 | } | ||
77 | // Never retry permanent certificate errors. (e.x. if ca-certificates | ||
78 | // are not installed). We should only make very few, targeted | ||
79 | // exceptions: many (other) status=Unavailable should be retried, such | ||
80 | // as if there's a network hiccup, or the internet goes out for a | ||
81 | // minute. This is also why here we are doing string parsing instead of | ||
82 | // simply making Unavailable a non-retried code elsewhere. | ||
83 | if strings.Contains(err.Error(), "x509: certificate signed by unknown authority") { | ||
84 | return err | ||
85 | } | ||
86 | if retryer == nil { | ||
87 | if r := settings.Retry(); r != nil { | ||
88 | retryer = r | ||
89 | } else { | ||
90 | return err | ||
91 | } | ||
92 | } | ||
93 | if d, ok := retryer.Retry(err); !ok { | ||
94 | return err | ||
95 | } else if err = sp(ctx, d); err != nil { | ||
96 | return err | ||
97 | } | ||
98 | } | ||
99 | } | ||