-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathdriver.go
240 lines (208 loc) · 5.41 KB
/
driver.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package ydb
import (
"context"
"errors"
"path"
"time"
"github.com/yandex-cloud/ydb-go-sdk/v2/internal"
"github.com/yandex-cloud/ydb-go-sdk/v2/timeutil"
"github.com/ydb-platform/ydb-go-genproto/protos/Ydb"
"github.com/ydb-platform/ydb-go-genproto/protos/Ydb_Operations"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
)
type driver struct {
cluster *cluster
meta *meta
trace DriverTrace
requestTimeout time.Duration
streamTimeout time.Duration
operationTimeout time.Duration
operationCancelAfter time.Duration
}
func (d *driver) Close() error {
return d.cluster.Close()
}
func isTransportError(err error) bool {
var te *TransportError
return errors.As(err, &te)
}
func (d *driver) pessimizeConn(ctx context.Context, conn *conn, cause error) {
// remove node from discovery cache on any transport error
driverTracePessimizationDone := driverTraceOnPessimization(ctx, d.trace, ctx, conn.addr.String(), cause)
driverTracePessimizationDone(ctx, conn.addr.String(), d.cluster.Pessimize(conn.addr))
}
func (d *driver) Call(ctx context.Context, op Operation) (info CallInfo, err error) {
// Remember raw context to pass it for the tracing functions.
rawCtx := ctx
if t := d.requestTimeout; t > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, t)
defer cancel()
}
if t := d.operationTimeout; t > 0 {
ctx = WithOperationTimeout(ctx, t)
}
if t := d.operationCancelAfter; t > 0 {
ctx = WithOperationCancelAfter(ctx, t)
}
// Get credentials (token actually) for the request.
var md metadata.MD
md, err = d.meta.md(ctx)
if err != nil {
return
}
if len(md) > 0 {
ctx = metadata.NewOutgoingContext(ctx, md)
}
var cc *conn
cc, err = d.cluster.Get(ctx)
if err != nil {
return
}
defer func() {
if err != nil {
err = mapGRPCError(err)
if isTransportError(err) {
d.pessimizeConn(rawCtx, cc, err)
}
}
}()
var raw *grpc.ClientConn
raw, err = cc.getConn(ctx)
if err != nil {
return
}
info = &callInfo{address: cc.Address()}
method, req, res, resp := internal.Unwrap(op)
if resp == nil {
resp = internal.WrapOpResponse(&Ydb_Operations.GetOperationResponse{})
}
params := operationParams(ctx)
if !params.Empty() {
setOperationParams(req, params)
}
onDone := driverTraceOnOperation(ctx, d.trace, ctx, cc.Address(), Method(method), params)
defer func() {
onDone(rawCtx, cc.Address(), Method(method), params, resp.GetOpID(), resp.GetIssues(), err)
}()
start := timeutil.Now()
cc.runtime.operationStart(start)
err = invoke(ctx, raw, resp, method, req, res)
cc.runtime.operationDone(
start, timeutil.Now(),
errIf(isTimeoutError(err), err),
)
return
}
func (d *driver) StreamRead(ctx context.Context, op StreamOperation) (info CallInfo, err error) {
// Remember raw context to pass it for the tracing functions.
rawCtx := ctx
var cancel context.CancelFunc
if t := d.streamTimeout; t > 0 {
ctx, cancel = context.WithTimeout(ctx, t)
} else {
// we want to force cancel goroutine with stream
ctx, cancel = context.WithCancel(ctx)
}
defer func() {
// if err is nil goroutine not run, and we cancel directly
if err != nil {
cancel()
}
}()
// Get credentials (token actually) for the request.
var md metadata.MD
md, err = d.meta.md(ctx)
if err != nil {
return nil, err
}
if len(md) > 0 {
ctx = metadata.NewOutgoingContext(ctx, md)
}
var cc *conn
cc, err = d.cluster.Get(ctx)
if err != nil {
return
}
defer func() {
if err != nil {
err = mapGRPCError(err)
if isTransportError(err) {
d.pessimizeConn(rawCtx, cc, err)
}
}
}()
var raw *grpc.ClientConn
raw, err = cc.getConn(ctx)
if err != nil {
return
}
info = &callInfo{address: cc.Address()}
if err != nil {
return nil, err
}
method, req, resp, process := internal.UnwrapStreamOperation(op)
desc := grpc.StreamDesc{
StreamName: path.Base(method),
ServerStreams: true,
}
cc.runtime.streamStart(timeutil.Now())
onDone := driverTraceOnStream(ctx, d.trace, ctx, cc.Address(), Method(method))
defer func() {
if err != nil {
cc.runtime.streamDone(timeutil.Now(), err)
onDone(rawCtx, cc.Address(), Method(method), err)
}
}()
var s grpc.ClientStream
s, err = grpc.NewClientStream(ctx, &desc, raw, method,
grpc.MaxCallRecvMsgSize(50*1024*1024), // 50MB
)
if err != nil {
return
}
if err = s.SendMsg(req); err != nil {
return
}
if err = s.CloseSend(); err != nil {
return
}
go func() {
var err error
defer func() {
if err != nil {
if isTransportError(err) {
d.pessimizeConn(rawCtx, cc, err)
}
}
cc.runtime.streamDone(timeutil.Now(), hideEOF(err))
onDone(rawCtx, cc.Address(), Method(method), hideEOF(err))
// cancel directly on exit from goroutine
// this need for break grpc client stream
cancel()
}()
for ; err == nil; err = func() (err error) { // isolate scope for defer effect without for-defer effect
onDone := driverTraceOnStreamRecv(ctx, d.trace, ctx, cc.Address(), Method(method))
defer func() {
if err == nil {
cc.runtime.streamRecv(timeutil.Now())
if s := resp.GetStatus(); s != Ydb.StatusIds_SUCCESS {
err = &OpError{
Reason: statusCode(s),
issues: resp.GetIssues(),
}
}
} else {
err = mapGRPCError(err)
}
// NOTE: do not hide even io.EOF for this call.
process(err)
onDone(rawCtx, cc.Address(), Method(method), resp.GetIssues(), hideEOF(err))
}()
return s.RecvMsg(resp)
}() {
}
}()
return
}