@@ -6,10 +6,10 @@ import (
66 "sync/atomic"
77
88 "github.com/ydb-platform/ydb-go-genproto/Ydb_Query_V1"
9- "github.com/ydb-platform/ydb-go-genproto/protos/Ydb"
109 "github.com/ydb-platform/ydb-go-genproto/protos/Ydb_Query"
1110
1211 "github.com/ydb-platform/ydb-go-sdk/v3/internal/allocator"
12+ "github.com/ydb-platform/ydb-go-sdk/v3/internal/query/config"
1313 "github.com/ydb-platform/ydb-go-sdk/v3/internal/query/options"
1414 "github.com/ydb-platform/ydb-go-sdk/v3/internal/stack"
1515 "github.com/ydb-platform/ydb-go-sdk/v3/internal/xcontext"
@@ -23,11 +23,11 @@ var _ query.Session = (*Session)(nil)
2323
2424type (
2525 Session struct {
26+ cfg * config.Config
2627 id string
2728 nodeID int64
2829 grpcClient Ydb_Query_V1.QueryServiceClient
2930 statusCode statusCode
30- trace * trace.Query
3131 closeOnce func (ctx context.Context ) error
3232 checks []func (s * Session ) bool
3333 }
@@ -40,19 +40,13 @@ func withSessionCheck(f func(*Session) bool) sessionOption {
4040 }
4141}
4242
43- func withSessionTrace (t * trace.Query ) sessionOption {
44- return func (s * Session ) {
45- s .trace = s .trace .Compose (t )
46- }
47- }
48-
4943func createSession (
50- ctx context.Context , client Ydb_Query_V1.QueryServiceClient , opts ... sessionOption ,
44+ ctx context.Context , client Ydb_Query_V1.QueryServiceClient , cfg * config. Config , opts ... sessionOption ,
5145) (s * Session , finalErr error ) {
5246 s = & Session {
47+ cfg : cfg ,
5348 grpcClient : client ,
5449 statusCode : statusUnknown ,
55- trace : & trace.Query {},
5650 checks : []func (* Session ) bool {
5751 func (s * Session ) bool {
5852 switch s .status () {
@@ -74,22 +68,14 @@ func createSession(
7468 opt (s )
7569 }
7670
77- onDone := trace .QueryOnSessionCreate (s .trace , & ctx , stack .FunctionID ("github.com/ydb-platform/ydb-go-sdk/3/internal/query.createSession" ))
71+ onDone := trace .QueryOnSessionCreate (s .cfg . Trace () , & ctx , stack .FunctionID ("github.com/ydb-platform/ydb-go-sdk/3/internal/query.createSession" ))
7872 defer func () {
7973 onDone (s , finalErr )
8074 }()
8175
8276 response , err := client .CreateSession (ctx , & Ydb_Query.CreateSessionRequest {})
8377 if err != nil {
84- return nil , xerrors .WithStackTrace (
85- xerrors .Transport (err ),
86- )
87- }
88-
89- if response .GetStatus () != Ydb .StatusIds_SUCCESS {
90- return nil , xerrors .WithStackTrace (
91- xerrors .FromOperation (response ),
92- )
78+ return nil , xerrors .WithStackTrace (err )
9379 }
9480
9581 defer func () {
@@ -103,9 +89,7 @@ func createSession(
10389
10490 err = s .attach (ctx )
10591 if err != nil {
106- return nil , xerrors .WithStackTrace (
107- xerrors .Transport (err ),
108- )
92+ return nil , xerrors .WithStackTrace (err )
10993 }
11094
11195 s .setStatus (statusIdle )
@@ -114,7 +98,7 @@ func createSession(
11498}
11599
116100func (s * Session ) attach (ctx context.Context ) (finalErr error ) {
117- onDone := trace .QueryOnSessionAttach (s .trace , & ctx , stack .FunctionID ("github.com/ydb-platform/ydb-go-sdk/3/internal/query.(*Session).attach" ), s )
101+ onDone := trace .QueryOnSessionAttach (s .cfg . Trace () , & ctx , stack .FunctionID ("github.com/ydb-platform/ydb-go-sdk/3/internal/query.(*Session).attach" ), s )
118102 defer func () {
119103 onDone (finalErr )
120104 }()
@@ -125,30 +109,30 @@ func (s *Session) attach(ctx context.Context) (finalErr error) {
125109 SessionId : s .id ,
126110 })
127111 if err != nil {
128- return xerrors .WithStackTrace (
129- xerrors .Transport (err ),
130- )
112+ return xerrors .WithStackTrace (err )
131113 }
132114
133- state , err : = attach .Recv ()
115+ _ , err = attach .Recv ()
134116 if err != nil {
135117 cancelAttach ()
136118
137- return xerrors .WithStackTrace (xerrors .Transport (err ))
138- }
139-
140- if state .GetStatus () != Ydb .StatusIds_SUCCESS {
141- cancelAttach ()
142-
143- return xerrors .WithStackTrace (xerrors .FromOperation (state ))
119+ return xerrors .WithStackTrace (err )
144120 }
145121
146122 s .closeOnce = xsync .OnceFunc (func (ctx context.Context ) (err error ) {
147- cancelAttach ()
123+ defer cancelAttach ()
148124
149125 s .setStatus (statusClosing )
150126 defer s .setStatus (statusClosed )
151127
128+ var cancel context.CancelFunc
129+ if d := s .cfg .SessionDeleteTimeout (); d > 0 {
130+ ctx , cancel = xcontext .WithTimeout (ctx , d )
131+ } else {
132+ ctx , cancel = xcontext .WithCancel (ctx )
133+ }
134+ defer cancel ()
135+
152136 if err = deleteSession (ctx , s .grpcClient , s .id ); err != nil {
153137 return xerrors .WithStackTrace (err )
154138 }
@@ -158,26 +142,21 @@ func (s *Session) attach(ctx context.Context) (finalErr error) {
158142
159143 go func () {
160144 defer func () {
161- _ = s .closeOnce (ctx )
145+ _ = s .closeOnce (xcontext . ValueOnly ( ctx ) )
162146 }()
163147
164148 for {
165149 if ! s .IsAlive () {
166150 return
167151 }
168- recv , recvErr := attach .Recv ()
152+ _ , recvErr := attach .Recv ()
169153 if recvErr != nil {
170154 if xerrors .Is (recvErr , io .EOF ) {
171155 s .setStatus (statusClosed )
172156 } else {
173157 s .setStatus (statusError )
174158 }
175159
176- return
177- }
178- if recv .GetStatus () != Ydb .StatusIds_SUCCESS {
179- s .setStatus (statusError )
180-
181160 return
182161 }
183162 }
@@ -187,16 +166,13 @@ func (s *Session) attach(ctx context.Context) (finalErr error) {
187166}
188167
189168func deleteSession (ctx context.Context , client Ydb_Query_V1.QueryServiceClient , sessionID string ) error {
190- response , err := client .DeleteSession (ctx ,
169+ _ , err := client .DeleteSession (ctx ,
191170 & Ydb_Query.DeleteSessionRequest {
192171 SessionId : sessionID ,
193172 },
194173 )
195174 if err != nil {
196- return xerrors .WithStackTrace (xerrors .Transport (err ))
197- }
198- if response .GetStatus () != Ydb .StatusIds_SUCCESS {
199- return xerrors .WithStackTrace (xerrors .FromOperation (response ))
175+ return xerrors .WithStackTrace (err )
200176 }
201177
202178 return nil
@@ -213,7 +189,7 @@ func (s *Session) IsAlive() bool {
213189}
214190
215191func (s * Session ) Close (ctx context.Context ) (err error ) {
216- onDone := trace .QueryOnSessionDelete (s .trace , & ctx , stack .FunctionID ("github.com/ydb-platform/ydb-go-sdk/3/internal/query.(*Session).Close" ), s )
192+ onDone := trace .QueryOnSessionDelete (s .cfg . Trace () , & ctx , stack .FunctionID ("github.com/ydb-platform/ydb-go-sdk/3/internal/query.(*Session).Close" ), s )
217193 defer func () {
218194 onDone (err )
219195 }()
@@ -240,13 +216,10 @@ func begin(
240216 },
241217 )
242218 if err != nil {
243- return nil , xerrors .WithStackTrace (xerrors .Transport (err ))
244- }
245- if response .GetStatus () != Ydb .StatusIds_SUCCESS {
246- return nil , xerrors .WithStackTrace (xerrors .FromOperation (response ))
219+ return nil , xerrors .WithStackTrace (err )
247220 }
248221
249- return newTransaction (response .GetTxMeta ().GetId (), s , s . trace ), nil
222+ return newTransaction (response .GetTxMeta ().GetId (), s ), nil
250223}
251224
252225func (s * Session ) Begin (
@@ -257,7 +230,7 @@ func (s *Session) Begin(
257230) {
258231 var tx * transaction
259232
260- onDone := trace .QueryOnSessionBegin (s .trace , & ctx , stack .FunctionID ("github.com/ydb-platform/ydb-go-sdk/3/internal/query.(*Session).Begin" ), s )
233+ onDone := trace .QueryOnSessionBegin (s .cfg . Trace () , & ctx , stack .FunctionID ("github.com/ydb-platform/ydb-go-sdk/3/internal/query.(*Session).Begin" ), s )
261234 defer func () {
262235 onDone (err , tx )
263236 }()
@@ -294,7 +267,7 @@ func (s *Session) Status() string {
294267func (s * Session ) Execute (
295268 ctx context.Context , q string , opts ... options.ExecuteOption ,
296269) (_ query.Transaction , _ query.Result , err error ) {
297- onDone := trace .QueryOnSessionExecute (s .trace , & ctx , stack .FunctionID ("github.com/ydb-platform/ydb-go-sdk/3/internal/query.(*Session).Execute" ), s , q )
270+ onDone := trace .QueryOnSessionExecute (s .cfg . Trace () , & ctx , stack .FunctionID ("github.com/ydb-platform/ydb-go-sdk/3/internal/query.(*Session).Execute" ), s , q )
298271 defer func () {
299272 onDone (err )
300273 }()
0 commit comments