-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathgrpc_util.go
167 lines (152 loc) · 5.85 KB
/
grpc_util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Copyright 2014 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package grpcutil
import (
"context"
"io"
"strings"
circuit "github.com/cockroachdb/circuitbreaker"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/util/netutil"
"github.com/cockroachdb/errors"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// ErrConnectionInterrupted is returned when a failed connection is
// being reused. We require that new connections be created with
// pkg/rpc.GRPCDial instead.
var ErrConnectionInterrupted = errors.New(errConnectionInterruptedMsg)
const errConnectionInterruptedMsg = "connection interrupted (did the remote node shut down or are there networking issues?)"
type localRequestKey struct{}
// NewLocalRequestContext returns a Context that can be used for local
// (in-process) RPC requests performed by the InternalClientAdapter. The ctx
// carries information about what tenant (if any) is the client of the RPC. The
// auth interceptor uses this information to authorize the tenant.
func NewLocalRequestContext(ctx context.Context, tenantID roachpb.TenantID) context.Context {
return context.WithValue(ctx, localRequestKey{}, tenantID)
}
// IsLocalRequestContext returns true if this context is marked for local (in-process) use.
func IsLocalRequestContext(ctx context.Context) (roachpb.TenantID, bool) {
val := ctx.Value(localRequestKey{})
if val == nil {
return roachpb.TenantID{}, false
}
return val.(roachpb.TenantID), true
}
// IsTimeout returns true if err's Cause is a gRPC timeout, or the request
// was canceled by a context timeout.
func IsTimeout(err error) bool {
if errors.Is(err, context.DeadlineExceeded) {
return true
}
err = errors.Cause(err)
if s, ok := status.FromError(err); ok {
return s.Code() == codes.DeadlineExceeded
}
return false
}
// IsConnectionUnavailable checks if grpc code is either codes.Unavailable which
// is set when we are not able to establish connection to remote node or
// codes.FailedPrecondition when node itself blocked access to remote node
// because it is marked as decommissioned in the local tombstone storage.
func IsConnectionUnavailable(err error) bool {
if s, ok := status.FromError(errors.UnwrapAll(err)); ok {
return s.Code() == codes.Unavailable || s.Code() == codes.FailedPrecondition
}
return false
}
// IsContextCanceled returns true if err's Cause is an error produced by gRPC
// on context cancellation.
func IsContextCanceled(err error) bool {
if s, ok := status.FromError(errors.UnwrapAll(err)); ok {
return s.Code() == codes.Canceled && s.Message() == context.Canceled.Error()
}
return false
}
// IsClosedConnection returns true if err's Cause is an error produced by gRPC
// on closed connections.
func IsClosedConnection(err error) bool {
if errors.Is(err, ErrConnectionInterrupted) {
return true
}
err = errors.Cause(err)
if s, ok := status.FromError(err); ok {
if s.Code() == codes.Canceled ||
s.Code() == codes.Unavailable {
return true
}
}
if errors.Is(err, context.Canceled) ||
strings.Contains(err.Error(), "is closing") ||
strings.Contains(err.Error(), "tls: use of closed connection") ||
strings.Contains(err.Error(), "use of closed network connection") ||
strings.Contains(err.Error(), io.ErrClosedPipe.Error()) ||
strings.Contains(err.Error(), io.EOF.Error()) ||
strings.Contains(err.Error(), "node unavailable") {
return true
}
return netutil.IsClosedConnection(err)
}
// IsConnectionRejected returns true if err's cause is an error produced by
// gRPC due to remote node being unavailable and retrying immediately would
// not fix the problem. It happens when either remote node is decommissioned
// or caller is not authorized to talk to the node.
// This check is helpful if caller doesn't want to distinguish between
// authentication and decommissioning errors in specific ways and just want
// to abort operations.
func IsConnectionRejected(err error) bool {
if s, ok := status.FromError(errors.UnwrapAll(err)); ok {
switch s.Code() {
case codes.Unauthenticated, codes.PermissionDenied, codes.FailedPrecondition:
return true
}
}
return false
}
// IsAuthError returns true if err's Cause is an error produced by
// gRPC due to an authentication or authorization error for the operation.
// AuthErrors should generally be considered non-retriable. They indicate
// that the operation would not succeed even if directed at another node
// in the cluster.
//
// As a special case, an AuthError (PermissionDenied) is returned on outbound
// dialing when the source node is in the process of terminating (see
// rpc.errDialRejected).
func IsAuthError(err error) bool {
if s, ok := status.FromError(errors.UnwrapAll(err)); ok {
switch s.Code() {
case codes.Unauthenticated, codes.PermissionDenied:
return true
}
}
return false
}
// RequestDidNotStart returns true if the given error from gRPC
// means that the request definitely could not have started on the
// remote server.
func RequestDidNotStart(err error) bool {
if errors.HasType(err, (*netutil.InitialHeartbeatFailedError)(nil)) ||
errors.Is(err, circuit.ErrBreakerOpen) ||
IsConnectionRejected(err) {
return true
}
_, ok := status.FromError(errors.Cause(err))
if !ok {
// This is a non-gRPC error; assume nothing.
return false
}
// This is where you'd hope to treat some gRPC errors as unambiguous.
// Unfortunately, gRPC provides no good way to distinguish ambiguous from
// unambiguous failures.
//
// https://github.com/grpc/grpc-go/issues/1443
// https://github.com/cockroachave hdb/cockroach/issues/19708#issuecomment-343891640
return false
}