@@ -8,36 +8,50 @@ import (
88 "google.golang.org/grpc"
99 "google.golang.org/grpc/metadata"
1010
11+ "github.com/ydb-platform/ydb-go-sdk/v3/internal/meta"
1112 "github.com/ydb-platform/ydb-go-sdk/v3/internal/stack"
1213 "github.com/ydb-platform/ydb-go-sdk/v3/internal/wrap"
1314 "github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
1415 "github.com/ydb-platform/ydb-go-sdk/v3/trace"
1516)
1617
1718type grpcClientStream struct {
18- grpc.ClientStream
19- c * conn
20- wrapping bool
21- traceID string
22- sentMark * modificationMark
23- onDone func (ctx context.Context , md metadata.MD )
19+ parentConn * conn
20+ stream grpc.ClientStream
21+ streamCtx context.Context //nolint:containedctx
22+ streamCancel context.CancelFunc
23+ wrapping bool
24+ traceID string
25+ sentMark * modificationMark
26+ }
27+
28+ func (s * grpcClientStream ) Header () (metadata.MD , error ) {
29+ return s .stream .Header ()
30+ }
31+
32+ func (s * grpcClientStream ) Trailer () metadata.MD {
33+ return s .stream .Trailer ()
34+ }
35+
36+ func (s * grpcClientStream ) Context () context.Context {
37+ return s .stream .Context ()
2438}
2539
2640func (s * grpcClientStream ) CloseSend () (err error ) {
2741 var (
28- ctx = s .Context ()
29- onDone = trace .DriverOnConnStreamCloseSend (s .c .config .Trace (), & ctx ,
42+ ctx = s .streamCtx
43+ onDone = trace .DriverOnConnStreamCloseSend (s .parentConn .config .Trace (), & ctx ,
3044 stack .FunctionID ("github.com/ydb-platform/ydb-go-sdk/3/internal/conn.(*grpcClientStream).CloseSend" ),
3145 )
3246 )
3347 defer func () {
3448 onDone (err )
3549 }()
3650
37- stop := s .c .lastUsage .Start ()
51+ stop := s .parentConn .lastUsage .Start ()
3852 defer stop ()
3953
40- err = s .ClientStream .CloseSend ()
54+ err = s .stream .CloseSend ()
4155
4256 if err != nil {
4357 if xerrors .IsContextError (err ) {
@@ -48,7 +62,7 @@ func (s *grpcClientStream) CloseSend() (err error) {
4862 return s .wrapError (
4963 xerrors .Transport (
5064 err ,
51- xerrors .WithAddress (s .c .Address ()),
65+ xerrors .WithAddress (s .parentConn .Address ()),
5266 xerrors .WithTraceID (s .traceID ),
5367 ),
5468 )
@@ -62,32 +76,32 @@ func (s *grpcClientStream) CloseSend() (err error) {
6276
6377func (s * grpcClientStream ) SendMsg (m interface {}) (err error ) {
6478 var (
65- ctx = s .Context ()
66- onDone = trace .DriverOnConnStreamSendMsg (s .c .config .Trace (), & ctx ,
79+ ctx = s .streamCtx
80+ onDone = trace .DriverOnConnStreamSendMsg (s .parentConn .config .Trace (), & ctx ,
6781 stack .FunctionID ("github.com/ydb-platform/ydb-go-sdk/3/internal/conn.(*grpcClientStream).SendMsg" ),
6882 )
6983 )
7084 defer func () {
7185 onDone (err )
7286 }()
7387
74- stop := s .c .lastUsage .Start ()
88+ stop := s .parentConn .lastUsage .Start ()
7589 defer stop ()
7690
77- err = s .ClientStream .SendMsg (m )
91+ err = s .stream .SendMsg (m )
7892
7993 if err != nil {
8094 if xerrors .IsContextError (err ) {
8195 return xerrors .WithStackTrace (err )
8296 }
8397
8498 defer func () {
85- s .c .onTransportError (ctx , err )
99+ s .parentConn .onTransportError (ctx , err )
86100 }()
87101
88102 if s .wrapping {
89103 err = xerrors .Transport (err ,
90- xerrors .WithAddress (s .c .Address ()),
104+ xerrors .WithAddress (s .parentConn .Address ()),
91105 xerrors .WithTraceID (s .traceID ),
92106 )
93107 if s .sentMark .canRetry () {
@@ -105,28 +119,31 @@ func (s *grpcClientStream) SendMsg(m interface{}) (err error) {
105119 return nil
106120}
107121
122+ func (s * grpcClientStream ) finish (err error ) {
123+ s .streamCancel ()
124+ trace .DriverOnConnStreamFinish (s .parentConn .config .Trace (), s .streamCtx ,
125+ stack .FunctionID ("github.com/ydb-platform/ydb-go-sdk/3/internal/conn.(*grpcClientStream).finish" ), err ,
126+ )
127+ }
128+
108129func (s * grpcClientStream ) RecvMsg (m interface {}) (err error ) {
109130 var (
110- ctx = s .Context ()
111- onDone = trace .DriverOnConnStreamRecvMsg (s .c .config .Trace (), & ctx ,
131+ ctx = s .streamCtx
132+ onDone = trace .DriverOnConnStreamRecvMsg (s .parentConn .config .Trace (), & ctx ,
112133 stack .FunctionID ("github.com/ydb-platform/ydb-go-sdk/3/internal/conn.(*grpcClientStream).RecvMsg" ),
113134 )
114135 )
115136 defer func () {
116137 onDone (err )
117- }()
118-
119- stop := s .c .lastUsage .Start ()
120- defer stop ()
121-
122- defer func () {
123138 if err != nil {
124- md := s .ClientStream .Trailer ()
125- s .onDone (ctx , md )
139+ meta .CallTrailerCallback (s .streamCtx , s .stream .Trailer ())
126140 }
127141 }()
128142
129- err = s .ClientStream .RecvMsg (m )
143+ stop := s .parentConn .lastUsage .Start ()
144+ defer stop ()
145+
146+ err = s .stream .RecvMsg (m )
130147
131148 if err != nil { //nolint:nestif
132149 if xerrors .IsContextError (err ) {
@@ -135,13 +152,13 @@ func (s *grpcClientStream) RecvMsg(m interface{}) (err error) {
135152
136153 defer func () {
137154 if ! xerrors .Is (err , io .EOF ) {
138- s .c .onTransportError (ctx , err )
155+ s .parentConn .onTransportError (ctx , err )
139156 }
140157 }()
141158
142159 if s .wrapping {
143160 err = xerrors .Transport (err ,
144- xerrors .WithAddress (s .c .Address ()),
161+ xerrors .WithAddress (s .parentConn .Address ()),
145162 )
146163 if s .sentMark .canRetry () {
147164 return s .wrapError (xerrors .Retryable (err ,
@@ -161,7 +178,7 @@ func (s *grpcClientStream) RecvMsg(m interface{}) (err error) {
161178 return s .wrapError (
162179 xerrors .Operation (
163180 xerrors .FromOperation (operation ),
164- xerrors .WithAddress (s .c .Address ()),
181+ xerrors .WithAddress (s .parentConn .Address ()),
165182 ),
166183 )
167184 }
@@ -177,7 +194,7 @@ func (s *grpcClientStream) wrapError(err error) error {
177194 }
178195
179196 return xerrors .WithStackTrace (
180- newConnError (s .c .endpoint .NodeID (), s .c .endpoint .Address (), err ),
197+ newConnError (s .parentConn .endpoint .NodeID (), s .parentConn .endpoint .Address (), err ),
181198 xerrors .WithSkipDepth (1 ),
182199 )
183200}
0 commit comments