@@ -26,7 +26,7 @@ import (
26
26
)
27
27
28
28
// Internals: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#internals
29
- var setGoroutineLabelForAttachStream = os .Getenv ("YDB_QUERY_SESSION_ATTACH_STREAM_GOROUTINE_LABEL" ) == "1"
29
+ var markGoroutineWithLabelNodeIDForAttachStream = os .Getenv ("YDB_QUERY_SESSION_ATTACH_STREAM_GOROUTINE_LABEL" ) == "1"
30
30
31
31
type (
32
32
Core interface {
@@ -116,7 +116,6 @@ func WithTrace(t *trace.Query) Option {
116
116
}
117
117
}
118
118
119
- //nolint:funlen
120
119
func Open (
121
120
ctx context.Context , client Ydb_Query_V1.QueryServiceClient , opts ... Option ,
122
121
) (_ * sessionCore , finalErr error ) {
@@ -159,20 +158,7 @@ func Open(
159
158
core .id = response .GetSessionId ()
160
159
core .nodeID = uint32 (response .GetNodeId ())
161
160
162
- if setGoroutineLabelForAttachStream {
163
- attachRes := make (chan error )
164
- pprof .Do (ctx , pprof .Labels (
165
- "node_id" , strconv .Itoa (int (core .NodeID ())),
166
- ), func (ctx context.Context ) {
167
- go func () {
168
- attachRes <- core .attach (ctx )
169
- }()
170
- })
171
- err = <- attachRes
172
- } else {
173
- err = core .attach (ctx )
174
- }
175
- if err != nil {
161
+ if err = core .attach (ctx ); err != nil {
176
162
_ = core .deleteSession (ctx )
177
163
178
164
return nil , xerrors .WithStackTrace (err )
@@ -199,14 +185,14 @@ func (core *sessionCore) attach(ctx context.Context) (finalErr error) {
199
185
}
200
186
}()
201
187
202
- attach , err := core .Client .AttachSession (attachCtx , & Ydb_Query.AttachSessionRequest {
188
+ attachStream , err := core .Client .AttachSession (attachCtx , & Ydb_Query.AttachSessionRequest {
203
189
SessionId : core .id ,
204
190
})
205
191
if err != nil {
206
192
return xerrors .WithStackTrace (err )
207
193
}
208
194
209
- _ , err = attach .Recv ()
195
+ _ , err = attachStream .Recv ()
210
196
if err != nil {
211
197
return xerrors .WithStackTrace (err )
212
198
}
@@ -224,24 +210,34 @@ func (core *sessionCore) attach(ctx context.Context) (finalErr error) {
224
210
return nil
225
211
})
226
212
227
- go func () {
228
- defer func () {
229
- select {
230
- case <- core . done :
231
- return
232
- default :
233
- close ( core . done )
234
- }
235
- }()
213
+ if markGoroutineWithLabelNodeIDForAttachStream {
214
+ pprof . Do ( ctx , pprof . Labels (
215
+ "node_id" , strconv . Itoa ( int ( core . NodeID ())),
216
+ ), func (context. Context ) {
217
+ go core . listenAttachStream ( attachStream )
218
+ })
219
+ } else {
220
+ go core . listenAttachStream ( attachStream )
221
+ }
236
222
237
- for core .IsAlive () {
238
- if _ , recvErr := attach .Recv (); recvErr != nil {
239
- return
240
- }
223
+ return nil
224
+ }
225
+
226
+ func (core * sessionCore ) listenAttachStream (attachStream Ydb_Query_V1.QueryService_AttachSessionClient ) {
227
+ defer func () {
228
+ select {
229
+ case <- core .done :
230
+ return
231
+ default :
232
+ close (core .done )
241
233
}
242
234
}()
243
235
244
- return nil
236
+ for core .IsAlive () {
237
+ if _ , recvErr := attachStream .Recv (); recvErr != nil {
238
+ return
239
+ }
240
+ }
245
241
}
246
242
247
243
func (core * sessionCore ) deleteSession (ctx context.Context ) (finalErr error ) {
0 commit comments