@@ -15,17 +15,19 @@ internal class PoolingSession : IPoolingSession
1515 private static readonly TimeSpan DeleteSessionTimeout = TimeSpan . FromSeconds ( 5 ) ;
1616 private static readonly CreateSessionRequest CreateSessionRequest = new ( ) ;
1717
18- private readonly IDriver _driver ;
1918 private readonly PoolingSessionSource _poolingSessionSource ;
2019 private readonly ILogger < PoolingSession > _logger ;
20+ private readonly CancellationTokenSource _attachStreamLifecycleCts = new ( ) ;
2121
2222 private volatile bool _isBroken = true ;
23+ private volatile bool _isBadSession ;
2324
2425 private readonly bool _disableServerBalancer ;
2526
2627 private string SessionId { get ; set ; } = string . Empty ;
2728 private long NodeId { get ; set ; }
2829
30+ public IDriver Driver { get ; }
2931 public bool IsBroken => _isBroken ;
3032
3133 internal PoolingSession (
@@ -35,10 +37,10 @@ internal PoolingSession(
3537 ILogger < PoolingSession > logger
3638 )
3739 {
38- _driver = driver ;
3940 _poolingSessionSource = poolingSessionSource ;
4041 _disableServerBalancer = disableServerBalancer ;
4142 _logger = logger ;
43+ Driver = driver ;
4244 }
4345
4446 public ValueTask < IServerStream < ExecuteQueryResponsePart > > ExecuteQuery (
@@ -60,15 +62,15 @@ public ValueTask<IServerStream<ExecuteQueryResponsePart>> ExecuteQuery(
6062 } ;
6163 request . Parameters . Add ( parameters . ToDictionary ( p => p . Key , p => p . Value . GetProto ( ) ) ) ;
6264
63- return _driver . ServerStreamCall ( QueryService . ExecuteQueryMethod , request , settings ) ;
65+ return Driver . ServerStreamCall ( QueryService . ExecuteQueryMethod , request , settings ) ;
6466 }
6567
6668 public async Task CommitTransaction (
6769 string txId ,
6870 CancellationToken cancellationToken = default
6971 )
7072 {
71- var response = await _driver . UnaryCall (
73+ var response = await Driver . UnaryCall (
7274 QueryService . CommitTransactionMethod ,
7375 new CommitTransactionRequest { SessionId = SessionId , TxId = txId } ,
7476 new GrpcRequestSettings { CancellationToken = cancellationToken , NodeId = NodeId }
@@ -85,7 +87,7 @@ public async Task RollbackTransaction(
8587 CancellationToken cancellationToken = default
8688 )
8789 {
88- var response = await _driver . UnaryCall (
90+ var response = await Driver . UnaryCall (
8991 QueryService . RollbackTransactionMethod ,
9092 new RollbackTransactionRequest { SessionId = SessionId , TxId = txId } ,
9193 new GrpcRequestSettings { CancellationToken = cancellationToken , NodeId = NodeId }
@@ -99,6 +101,8 @@ public async Task RollbackTransaction(
99101
100102 public void OnNotSuccessStatusCode ( StatusCode statusCode )
101103 {
104+ _isBadSession = _isBadSession || statusCode is StatusCode . BadSession ;
105+
102106 if ( statusCode is
103107 StatusCode . BadSession or
104108 StatusCode . SessionBusy or
@@ -121,7 +125,7 @@ public async Task Open(CancellationToken cancellationToken)
121125 requestSettings . ClientCapabilities . Add ( SessionBalancer ) ;
122126 }
123127
124- var response = await _driver . UnaryCall ( QueryService . CreateSessionMethod , CreateSessionRequest , requestSettings ) ;
128+ var response = await Driver . UnaryCall ( QueryService . CreateSessionMethod , CreateSessionRequest , requestSettings ) ;
125129
126130 if ( response . Status . IsNotSuccess ( ) )
127131 {
@@ -138,7 +142,7 @@ public async Task Open(CancellationToken cancellationToken)
138142 {
139143 try
140144 {
141- using var stream = await _driver . ServerStreamCall (
145+ using var stream = await Driver . ServerStreamCall (
142146 QueryService . AttachSessionMethod ,
143147 new AttachSessionRequest { SessionId = SessionId } ,
144148 new GrpcRequestSettings { NodeId = NodeId }
@@ -161,10 +165,12 @@ public async Task Open(CancellationToken cancellationToken)
161165
162166 completeTask . SetResult ( ) ;
163167
168+ var lifecycleAttachToken = _attachStreamLifecycleCts . Token ;
169+
164170 try
165171 {
166172 // ReSharper disable once MethodSupportsCancellation
167- while ( await stream . MoveNextAsync ( ) )
173+ while ( await stream . MoveNextAsync ( lifecycleAttachToken ) )
168174 {
169175 var sessionState = stream . Current ;
170176
@@ -215,14 +221,16 @@ public async Task DeleteSession()
215221 {
216222 try
217223 {
218- if ( _isBroken )
224+ _isBroken = true ;
225+ _attachStreamLifecycleCts . CancelAfter ( DeleteSessionTimeout ) ;
226+
227+ if ( _isBadSession )
219228 {
220229 return ;
221230 }
222231
223- _isBroken = true ;
224-
225- var deleteSessionResponse = await _driver . UnaryCall (
232+ _isBadSession = true ;
233+ var deleteSessionResponse = await Driver . UnaryCall (
226234 QueryService . DeleteSessionMethod ,
227235 new DeleteSessionRequest { SessionId = SessionId } ,
228236 new GrpcRequestSettings { TransportTimeout = DeleteSessionTimeout , NodeId = NodeId }
0 commit comments