1 | ;
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.PullRetry = exports.RETRY_CODES = void 0;
|
4 | /*!
|
5 | * Copyright 2019 Google Inc. All Rights Reserved.
|
6 | *
|
7 | * Licensed under the Apache License, Version 2.0 (the "License");
|
8 | * you may not use this file except in compliance with the License.
|
9 | * You may obtain a copy of the License at
|
10 | *
|
11 | * http://www.apache.org/licenses/LICENSE-2.0
|
12 | *
|
13 | * Unless required by applicable law or agreed to in writing, software
|
14 | * distributed under the License is distributed on an "AS IS" BASIS,
|
15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
16 | * See the License for the specific language governing permissions and
|
17 | * limitations under the License.
|
18 | */
|
19 | const google_gax_1 = require("google-gax");
|
20 | /*!
|
21 | * retryable grpc.status codes
|
22 | */
|
23 | exports.RETRY_CODES = [
|
24 | google_gax_1.grpc.status.DEADLINE_EXCEEDED,
|
25 | google_gax_1.grpc.status.RESOURCE_EXHAUSTED,
|
26 | google_gax_1.grpc.status.ABORTED,
|
27 | google_gax_1.grpc.status.INTERNAL,
|
28 | google_gax_1.grpc.status.UNAVAILABLE,
|
29 | google_gax_1.grpc.status.CANCELLED,
|
30 | ];
|
31 | /**
|
32 | * Used to track pull requests and determine if additional requests should be
|
33 | * made, etc.
|
34 | *
|
35 | * @class
|
36 | * @private
|
37 | */
|
38 | class PullRetry {
|
39 | constructor() {
|
40 | this.failures = 0;
|
41 | }
|
42 | /**
|
43 | * Generates a timeout that can be used for applying a backoff based on the
|
44 | * current number of failed requests.
|
45 | *
|
46 | * @see {@link https://cloud.google.com/iot/docs/how-tos/exponential-backoff}
|
47 | * @private
|
48 | * @returns {number}
|
49 | */
|
50 | createTimeout() {
|
51 | if (this.failures === 0) {
|
52 | return 0;
|
53 | }
|
54 | return Math.pow(2, this.failures) * 1000 + Math.floor(Math.random() * 1000);
|
55 | }
|
56 | /**
|
57 | * Determines if a request grpc.status should be retried.
|
58 | *
|
59 | * Deadlines behave kind of unexpectedly on streams, rather than using it as
|
60 | * an indicator of when to give up trying to connect, it actually dictates
|
61 | * how long the stream should stay open. Because of this, it is virtually
|
62 | * impossible to determine whether or not a deadline error is the result of
|
63 | * the server closing the stream or if we timed out waiting for a connection.
|
64 | *
|
65 | * @private
|
66 | * @param {object} grpc.status The request grpc.status.
|
67 | * @returns {boolean}
|
68 | */
|
69 | retry(err) {
|
70 | if (err.code === google_gax_1.grpc.status.OK ||
|
71 | err.code === google_gax_1.grpc.status.DEADLINE_EXCEEDED) {
|
72 | this.failures = 0;
|
73 | }
|
74 | else {
|
75 | this.failures += 1;
|
76 | }
|
77 | if (err.code === google_gax_1.grpc.status.UNAVAILABLE &&
|
78 | err.details &&
|
79 | err.details.match(/Server shutdownNow invoked/)) {
|
80 | return true;
|
81 | }
|
82 | return exports.RETRY_CODES.includes(err.code);
|
83 | }
|
84 | }
|
85 | exports.PullRetry = PullRetry;
|
86 | //# sourceMappingURL=pull-retry.js.map |
\ | No newline at end of file |