2727 statusCode statusCode
2828 trace * traceSession
2929 closeOnce func (ctx context.Context ) error
30+ checks []func (s * Session ) bool
3031 }
3132 traceSession struct {
3233 onCreate func (ctx context.Context , functionID stack.Caller ) func (* Session , error )
@@ -36,74 +37,15 @@ type (
3637 sessionOption func (session * Session )
3738)
3839
39- func withSessionTrace ( t * traceSession ) sessionOption {
40+ func withSessionCheck ( f func ( * Session ) bool ) sessionOption {
4041 return func (s * Session ) {
41- if t .onCreate != nil {
42- h1 := s .trace .onCreate
43- h2 := t .onCreate
44- s .trace .onCreate = func (ctx context.Context , functionID stack.Caller ) func (* Session , error ) {
45- var r , r1 func (* Session , error )
46- if h1 != nil {
47- r = h1 (ctx , functionID )
48- }
49- if h2 != nil {
50- r1 = h2 (ctx , functionID )
51- }
52-
53- return func (session * Session , err error ) {
54- if r != nil {
55- r (session , err )
56- }
57- if r1 != nil {
58- r1 (session , err )
59- }
60- }
61- }
62- }
63- if t .onAttach != nil {
64- h1 := s .trace .onAttach
65- h2 := t .onAttach
66- s .trace .onAttach = func (ctx context.Context , functionID stack.Caller , session * Session ) func (error ) {
67- var r , r1 func (error )
68- if h1 != nil {
69- r = h1 (ctx , functionID , session )
70- }
71- if h2 != nil {
72- r1 = h2 (ctx , functionID , session )
73- }
74-
75- return func (err error ) {
76- if r != nil {
77- r (err )
78- }
79- if r1 != nil {
80- r1 (err )
81- }
82- }
83- }
84- }
85- if t .onClose != nil {
86- h1 := s .trace .onClose
87- h2 := t .onClose
88- s .trace .onClose = func (ctx context.Context , functionID stack.Caller , session * Session ) func (error ) {
89- var r , r1 func (error )
90- if h1 != nil {
91- r = h1 (ctx , functionID , session )
92- }
93- if h2 != nil {
94- r1 = h2 (ctx , functionID , session )
95- }
42+ s .checks = append (s .checks , f )
43+ }
44+ }
9645
97- return func (err error ) {
98- if r != nil {
99- r (err )
100- }
101- if r1 != nil {
102- r1 (err )
103- }
104- }
105- }
106- }
46+ func withSessionTrace (t * traceSession ) sessionOption {
47+ return func (s * Session ) {
48+ s .trace = t
10749 }
10850}
10951
@@ -114,6 +56,16 @@ func createSession(
11456 grpcClient : client ,
11557 statusCode : statusUnknown ,
11658 trace : defaultSessionTrace ,
59+ checks : []func (* Session ) bool {
60+ func (s * Session ) bool {
61+ switch s .status () {
62+ case statusIdle , statusInUse :
63+ return true
64+ default :
65+ return false
66+ }
67+ },
68+ },
11769 }
11870 defer func () {
11971 if finalErr != nil {
@@ -214,12 +166,10 @@ func (s *Session) attach(ctx context.Context) (finalErr error) {
214166 s .setStatus (statusClosing )
215167 defer s .setStatus (statusClosed )
216168
217- if s .trace .onClose != nil {
218- onClose := s .trace .onClose (ctx , stack .FunctionID ("" ), s )
219- defer func () {
220- onClose (err )
221- }()
222- }
169+ onClose := s .trace .onClose (ctx , stack .FunctionID ("" ), s )
170+ defer func () {
171+ onClose (err )
172+ }()
223173
224174 if err = deleteSession (ctx , s .grpcClient , s .id ); err != nil {
225175 return xerrors .WithStackTrace (err )
@@ -248,12 +198,13 @@ func deleteSession(ctx context.Context, client Ydb_Query_V1.QueryServiceClient,
248198}
249199
250200func (s * Session ) IsAlive () bool {
251- switch s .status () {
252- case statusIdle , statusInUse :
253- return true
254- default :
255- return false
201+ for _ , check := range s .checks {
202+ if ! check (s ) {
203+ return false
204+ }
256205 }
206+
207+ return true
257208}
258209
259210func (s * Session ) Close (ctx context.Context ) error {
0 commit comments