|
| 1 | +// Copyright 2020 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | + |
| 14 | +package pubsublite |
| 15 | + |
| 16 | +import ( |
| 17 | + "time" |
| 18 | + |
| 19 | + "google.golang.org/grpc/codes" |
| 20 | + "google.golang.org/grpc/status" |
| 21 | + |
| 22 | + gax "github.com/googleapis/gax-go/v2" |
| 23 | +) |
| 24 | + |
| 25 | +// streamRetryer implements the retry policy for establishing gRPC stream |
| 26 | +// connections. |
| 27 | +type streamRetryer struct { |
| 28 | + bo gax.Backoff |
| 29 | + deadline time.Time |
| 30 | +} |
| 31 | + |
| 32 | +func newStreamRetryer(timeout time.Duration) *streamRetryer { |
| 33 | + return &streamRetryer{ |
| 34 | + bo: gax.Backoff{ |
| 35 | + Initial: 10 * time.Millisecond, |
| 36 | + Max: 10 * time.Second, |
| 37 | + Multiplier: 2, |
| 38 | + }, |
| 39 | + deadline: time.Now().Add(timeout), |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +func (r *streamRetryer) RetrySend(err error) (time.Duration, bool) { |
| 44 | + if time.Now().After(r.deadline) { |
| 45 | + return 0, false |
| 46 | + } |
| 47 | + if isRetryableSendError(err) { |
| 48 | + return r.bo.Pause(), true |
| 49 | + } |
| 50 | + return 0, false |
| 51 | +} |
| 52 | + |
| 53 | +func (r *streamRetryer) RetryRecv(err error) (time.Duration, bool) { |
| 54 | + if time.Now().After(r.deadline) { |
| 55 | + return 0, false |
| 56 | + } |
| 57 | + if isRetryableRecvError(err) { |
| 58 | + return r.bo.Pause(), true |
| 59 | + } |
| 60 | + return 0, false |
| 61 | +} |
| 62 | + |
| 63 | +func isRetryableSendCode(code codes.Code) bool { |
| 64 | + switch code { |
| 65 | + // Client-side errors that occur during grpc.ClientStream.SendMsg() have a |
| 66 | + // smaller set of retryable codes. |
| 67 | + case codes.DeadlineExceeded, codes.Unavailable: |
| 68 | + return true |
| 69 | + default: |
| 70 | + return false |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +func isRetryableRecvCode(code codes.Code) bool { |
| 75 | + switch code { |
| 76 | + // Consistent with https://github.com/googleapis/java-pubsublite/blob/master/google-cloud-pubsublite/src/main/java/com/google/cloud/pubsublite/ErrorCodes.java |
| 77 | + case codes.Aborted, codes.DeadlineExceeded, codes.Internal, codes.ResourceExhausted, codes.Unavailable, codes.Unknown: |
| 78 | + return true |
| 79 | + default: |
| 80 | + return false |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +func isRetryableSendError(err error) bool { |
| 85 | + return isRetryableStreamError(err, isRetryableSendCode) |
| 86 | +} |
| 87 | + |
| 88 | +func isRetryableRecvError(err error) bool { |
| 89 | + return isRetryableStreamError(err, isRetryableRecvCode) |
| 90 | +} |
| 91 | + |
| 92 | +func isRetryableStreamError(err error, isEligible func(codes.Code) bool) bool { |
| 93 | + s, ok := status.FromError(err) |
| 94 | + if !ok { |
| 95 | + // Includes io.EOF, normal stream close. |
| 96 | + // Consistent with https://github.com/googleapis/google-cloud-go/blob/master/pubsub/service.go |
| 97 | + return true |
| 98 | + } |
| 99 | + return isEligible(s.Code()) |
| 100 | +} |
0 commit comments