@@ -15,7 +15,7 @@ import (
1515
1616 "github.com/ydb-platform/ydb-go-sdk/v3/internal/endpoint"
1717 "github.com/ydb-platform/ydb-go-sdk/v3/internal/meta"
18- "github.com/ydb-platform/ydb-go-sdk/v3/internal/response "
18+ "github.com/ydb-platform/ydb-go-sdk/v3/internal/operation "
1919 "github.com/ydb-platform/ydb-go-sdk/v3/internal/stack"
2020 "github.com/ydb-platform/ydb-go-sdk/v3/internal/xcontext"
2121 "github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
@@ -299,109 +299,151 @@ func (c *conn) Close(ctx context.Context) (err error) {
299299 return c .wrapError (err )
300300}
301301
302+ var (
303+ onTransportErrorStub = func (ctx context.Context , err error ) {}
304+ wrapErrorStub = func (err error ) error { return err }
305+ )
306+
302307//nolint:funlen
303- func ( c * conn ) Invoke (
308+ func invoke (
304309 ctx context.Context ,
305310 method string ,
306- req interface {},
307- res interface {},
311+ req , reply any ,
312+ cc grpc.ClientConnInterface ,
313+ onTransportError func (context.Context , error ),
314+ address string ,
315+ wrapError func (err error ) error ,
308316 opts ... grpc.CallOption ,
309- ) (err error ) {
310- var (
311- opID string
312- issues []trace.Issue
313- useWrapping = UseWrapping (ctx )
314- onDone = trace .DriverOnConnInvoke (
315- c .config .Trace (), & ctx ,
316- stack .FunctionID ("github.com/ydb-platform/ydb-go-sdk/3/internal/conn.(*conn).Invoke" ),
317- c .endpoint , trace .Method (method ),
318- )
319- cc * grpc.ClientConn
320- md = metadata.MD {}
321- )
322- defer func () {
323- meta .CallTrailerCallback (ctx , md )
324- onDone (err , issues , opID , c .GetState (), md )
325- }()
317+ ) (
318+ opID string ,
319+ issues []trace.Issue ,
320+ _ error ,
321+ ) {
322+ useWrapping := UseWrapping (ctx )
326323
327- cc , err = c . realConn (ctx )
324+ ctx , traceID , err := meta . TraceID (ctx )
328325 if err != nil {
329- return c . wrapError (err )
326+ return opID , issues , xerrors . WithStackTrace (err )
330327 }
331328
332- stop := c .lastUsage .Start ()
333- defer stop ()
329+ ctx , sentMark := markContext (meta .WithTraceID (ctx , traceID ))
334330
335- ctx , traceID , err := meta .TraceID (ctx )
336- if err != nil {
337- return xerrors .WithStackTrace (err )
331+ if onTransportError == nil {
332+ onTransportError = onTransportErrorStub
338333 }
339334
340- ctx , sentMark := markContext (meta .WithTraceID (ctx , traceID ))
335+ if wrapError == nil {
336+ wrapError = wrapErrorStub
337+ }
341338
342- err = cc .Invoke (ctx , method , req , res , append ( opts , grpc . Trailer ( & md )) ... )
339+ err = cc .Invoke (ctx , method , req , reply , opts ... )
343340 if err != nil {
344341 if xerrors .IsContextError (err ) {
345- return xerrors .WithStackTrace (err )
342+ return opID , issues , xerrors .WithStackTrace (err )
346343 }
347344
348- defer func () {
349- c .onTransportError (ctx , err )
350- }()
345+ defer onTransportError (ctx , err )
351346
352347 if useWrapping {
353348 err = xerrors .Transport (err ,
354- xerrors .WithAddress (c . Address () ),
349+ xerrors .WithAddress (address ),
355350 xerrors .WithTraceID (traceID ),
356351 )
357352 if sentMark .canRetry () {
358- return c . wrapError (xerrors .Retryable (err , xerrors .WithName ("Invoke" )))
353+ return opID , issues , wrapError (xerrors .Retryable (err , xerrors .WithName ("Invoke" )))
359354 }
360355
361- return c . wrapError (err )
356+ return opID , issues , wrapError (err )
362357 }
363358
364- return err
365- }
366-
367- err = c .handleResponse (res , & opID , & issues , traceID , useWrapping )
368- if err != nil {
369- return err
359+ return opID , issues , err
370360 }
371361
372- return err
373- }
374-
375- func (c * conn ) handleResponse (
376- res interface {},
377- opID * string ,
378- issues * []trace.Issue ,
379- traceID string ,
380- useWrapping bool ,
381- ) error {
382- if o , ok := res .(response.Response ); ok {
383- * opID = o .GetOperation ().GetId ()
384- for _ , issue := range o .GetOperation ().GetIssues () {
385- * issues = append (* issues , issue )
362+ switch t := reply .(type ) {
363+ case operation.Response :
364+ opID = t .GetOperation ().GetId ()
365+ for _ , issue := range t .GetOperation ().GetIssues () {
366+ issues = append (issues , issue )
386367 }
387368 if useWrapping {
388369 switch {
389- case ! o .GetOperation ().GetReady ():
390- return c . wrapError (errOperationNotReady )
370+ case ! t .GetOperation ().GetReady ():
371+ return opID , issues , wrapError (errOperationNotReady )
391372
392- case o .GetOperation ().GetStatus () != Ydb .StatusIds_SUCCESS :
393- return c . wrapError (
373+ case t .GetOperation ().GetStatus () != Ydb .StatusIds_SUCCESS :
374+ return opID , issues , wrapError (
394375 xerrors .Operation (
395- xerrors .FromOperation (o .GetOperation ()),
396- xerrors .WithAddress (c .Address ()),
376+ xerrors .FromOperation (t .GetOperation ()),
377+ xerrors .WithAddress (address ),
378+ xerrors .WithTraceID (traceID ),
379+ ),
380+ )
381+ }
382+ }
383+ case operation.Status :
384+ for _ , issue := range t .GetIssues () {
385+ issues = append (issues , issue )
386+ }
387+ if useWrapping {
388+ if t .GetStatus () != Ydb .StatusIds_SUCCESS {
389+ return opID , issues , wrapError (
390+ xerrors .Operation (
391+ xerrors .FromOperation (t ),
392+ xerrors .WithAddress (address ),
397393 xerrors .WithTraceID (traceID ),
398394 ),
399395 )
400396 }
401397 }
402398 }
403399
404- return nil
400+ return opID , issues , nil
401+ }
402+
403+ func (c * conn ) Invoke (
404+ ctx context.Context ,
405+ method string ,
406+ req interface {},
407+ res interface {},
408+ opts ... grpc.CallOption ,
409+ ) (err error ) {
410+ var (
411+ opID string
412+ issues []trace.Issue
413+ onDone = trace .DriverOnConnInvoke (
414+ c .config .Trace (), & ctx ,
415+ stack .FunctionID ("github.com/ydb-platform/ydb-go-sdk/3/internal/conn.(*conn).Invoke" ),
416+ c .endpoint , trace .Method (method ),
417+ )
418+ cc * grpc.ClientConn
419+ md = metadata.MD {}
420+ )
421+ defer func () {
422+ meta .CallTrailerCallback (ctx , md )
423+ onDone (err , issues , opID , c .GetState (), md )
424+ }()
425+
426+ cc , err = c .realConn (ctx )
427+ if err != nil {
428+ return c .wrapError (err )
429+ }
430+
431+ stop := c .lastUsage .Start ()
432+ defer stop ()
433+
434+ opID , issues , err = invoke (
435+ ctx ,
436+ method ,
437+ req ,
438+ res ,
439+ cc ,
440+ c .onTransportError ,
441+ c .Address (),
442+ c .wrapError ,
443+ append (opts , grpc .Trailer (& md ))... ,
444+ )
445+
446+ return err
405447}
406448
407449//nolint:funlen
0 commit comments