@@ -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 {
@@ -159,20 +159,7 @@ func Open(
159
159
core .id = response .GetSessionId ()
160
160
core .nodeID = uint32 (response .GetNodeId ())
161
161
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 {
162
+ if err = core .attach (ctx ); err != nil {
176
163
_ = core .deleteSession (ctx )
177
164
178
165
return nil , xerrors .WithStackTrace (err )
@@ -199,14 +186,14 @@ func (core *sessionCore) attach(ctx context.Context) (finalErr error) {
199
186
}
200
187
}()
201
188
202
- attach , err := core .Client .AttachSession (attachCtx , & Ydb_Query.AttachSessionRequest {
189
+ attachStream , err := core .Client .AttachSession (attachCtx , & Ydb_Query.AttachSessionRequest {
203
190
SessionId : core .id ,
204
191
})
205
192
if err != nil {
206
193
return xerrors .WithStackTrace (err )
207
194
}
208
195
209
- _ , err = attach .Recv ()
196
+ _ , err = attachStream .Recv ()
210
197
if err != nil {
211
198
return xerrors .WithStackTrace (err )
212
199
}
@@ -224,24 +211,34 @@ func (core *sessionCore) attach(ctx context.Context) (finalErr error) {
224
211
return nil
225
212
})
226
213
227
- go func () {
228
- defer func () {
229
- select {
230
- case <- core . done :
231
- return
232
- default :
233
- close ( core . done )
234
- }
235
- }()
214
+ if markGoroutineWithLabelNodeIDForAttachStream {
215
+ pprof . Do ( ctx , pprof . Labels (
216
+ "node_id" , strconv . Itoa ( int ( core . NodeID ())),
217
+ ), func (context. Context ) {
218
+ go core . listenAttachStream ( attachStream )
219
+ })
220
+ } else {
221
+ go core . listenAttachStream ( attachStream )
222
+ }
236
223
237
- for core .IsAlive () {
238
- if _ , recvErr := attach .Recv (); recvErr != nil {
239
- return
240
- }
224
+ return nil
225
+ }
226
+
227
+ func (core * sessionCore ) listenAttachStream (attachStream Ydb_Query_V1.QueryService_AttachSessionClient ) {
228
+ defer func () {
229
+ select {
230
+ case <- core .done :
231
+ return
232
+ default :
233
+ close (core .done )
241
234
}
242
235
}()
243
236
244
- return nil
237
+ for core .IsAlive () {
238
+ if _ , recvErr := attachStream .Recv (); recvErr != nil {
239
+ return
240
+ }
241
+ }
245
242
}
246
243
247
244
func (core * sessionCore ) deleteSession (ctx context.Context ) (finalErr error ) {
0 commit comments