|
| 1 | +/* |
| 2 | +Copyright SecureKey Technologies Inc. All Rights Reserved. |
| 3 | +
|
| 4 | +SPDX-License-Identifier: Apache-2.0 |
| 5 | +*/ |
| 6 | + |
| 7 | +package retry |
| 8 | + |
| 9 | +import ( |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/hyperledger/fabric-sdk-go/pkg/status" |
| 13 | + "github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/common" |
| 14 | + pb "github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/peer" |
| 15 | + grpcCodes "google.golang.org/grpc/codes" |
| 16 | +) |
| 17 | + |
| 18 | +const ( |
| 19 | + // DefaultAttempts number of retry attempts made by default |
| 20 | + DefaultAttempts = 3 |
| 21 | + // DefaultInitialBackoff default initial backoff |
| 22 | + DefaultInitialBackoff = 500 * time.Millisecond |
| 23 | + // DefaultMaxBackoff default maximum backoff |
| 24 | + DefaultMaxBackoff = 60 * time.Second |
| 25 | + // DefaultBackoffFactor default backoff factor |
| 26 | + DefaultBackoffFactor = 2.0 |
| 27 | +) |
| 28 | + |
| 29 | +// DefaultOpts default retry options |
| 30 | +var DefaultOpts = Opts{ |
| 31 | + Attempts: DefaultAttempts, |
| 32 | + InitialBackoff: DefaultInitialBackoff, |
| 33 | + MaxBackoff: DefaultMaxBackoff, |
| 34 | + BackoffFactor: DefaultBackoffFactor, |
| 35 | + RetryableCodes: DefaultRetryableCodes, |
| 36 | +} |
| 37 | + |
| 38 | +// DefaultRetryableCodes these are the error codes, grouped by source of error, |
| 39 | +// that are considered to be transient error conditions by default |
| 40 | +var DefaultRetryableCodes = map[status.Group][]status.Code{ |
| 41 | + status.EndorserClientStatus: []status.Code{ |
| 42 | + status.EndorsementMismatch, |
| 43 | + }, |
| 44 | + status.EndorserServerStatus: []status.Code{ |
| 45 | + status.Code(common.Status_SERVICE_UNAVAILABLE), |
| 46 | + status.Code(common.Status_INTERNAL_SERVER_ERROR), |
| 47 | + }, |
| 48 | + status.OrdererServerStatus: []status.Code{ |
| 49 | + status.Code(common.Status_SERVICE_UNAVAILABLE), |
| 50 | + status.Code(common.Status_INTERNAL_SERVER_ERROR), |
| 51 | + }, |
| 52 | + status.EventServerStatus: []status.Code{ |
| 53 | + status.Code(pb.TxValidationCode_DUPLICATE_TXID), |
| 54 | + status.Code(pb.TxValidationCode_ENDORSEMENT_POLICY_FAILURE), |
| 55 | + status.Code(pb.TxValidationCode_MVCC_READ_CONFLICT), |
| 56 | + status.Code(pb.TxValidationCode_PHANTOM_READ_CONFLICT), |
| 57 | + }, |
| 58 | + // TODO: gRPC introduced retries in v1.8.0. This can be replaced with the |
| 59 | + // gRPC fail fast option, once available |
| 60 | + status.GRPCTransportStatus: []status.Code{ |
| 61 | + status.Code(grpcCodes.Unavailable), |
| 62 | + }, |
| 63 | +} |
| 64 | + |
| 65 | +// ChannelClientRetryableCodes are the suggested codes that should be treated as |
| 66 | +// transient by fabric-sdk-go/api/apitxn.ChannelClient |
| 67 | +var ChannelClientRetryableCodes = map[status.Group][]status.Code{ |
| 68 | + status.EndorserClientStatus: []status.Code{ |
| 69 | + status.ConnectionFailed, status.EndorsementMismatch, |
| 70 | + }, |
| 71 | + status.EndorserServerStatus: []status.Code{ |
| 72 | + status.Code(common.Status_SERVICE_UNAVAILABLE), |
| 73 | + status.Code(common.Status_INTERNAL_SERVER_ERROR), |
| 74 | + }, |
| 75 | + status.OrdererClientStatus: []status.Code{ |
| 76 | + status.ConnectionFailed, |
| 77 | + }, |
| 78 | + status.OrdererServerStatus: []status.Code{ |
| 79 | + status.Code(common.Status_SERVICE_UNAVAILABLE), |
| 80 | + status.Code(common.Status_INTERNAL_SERVER_ERROR), |
| 81 | + }, |
| 82 | + status.EventServerStatus: []status.Code{ |
| 83 | + status.Code(pb.TxValidationCode_DUPLICATE_TXID), |
| 84 | + status.Code(pb.TxValidationCode_ENDORSEMENT_POLICY_FAILURE), |
| 85 | + status.Code(pb.TxValidationCode_MVCC_READ_CONFLICT), |
| 86 | + status.Code(pb.TxValidationCode_PHANTOM_READ_CONFLICT), |
| 87 | + }, |
| 88 | + // TODO: gRPC introduced retries in v1.8.0. This can be replaced with the |
| 89 | + // gRPC fail fast option, once available |
| 90 | + status.GRPCTransportStatus: []status.Code{ |
| 91 | + status.Code(grpcCodes.Unavailable), |
| 92 | + }, |
| 93 | +} |
0 commit comments