Skip to content

Commit 0c53efb

Browse files
committed
Move implicit session creation to PoolManager by flag
1 parent 3addeae commit 0c53efb

File tree

3 files changed

+41
-38
lines changed

3 files changed

+41
-38
lines changed

src/Ydb.Sdk/src/Ado/PoolManager.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,22 @@ CancellationToken cancellationToken
3333
!cacheDriver.IsDisposed
3434
? cacheDriver
3535
: Drivers[settings.GrpcConnectionString] = await settings.BuildDriver();
36-
driver.RegisterOwner();
3736

38-
var factory = new PoolingSessionFactory(driver, settings);
39-
var newSessionPool = new PoolingSessionSource<PoolingSession>(factory, settings);
37+
ISessionSource newSessionPool;
38+
if (settings.EnableImplicitSession)
39+
{
40+
var key = settings.ConnectionString;
41+
newSessionPool = new ImplicitSessionSource(
42+
driver,
43+
onEmpty: () => Pools.TryRemove(key, out _)
44+
);
45+
}
46+
else
47+
{
48+
driver.RegisterOwner();
49+
var factory = new PoolingSessionFactory(driver, settings);
50+
newSessionPool = new PoolingSessionSource<PoolingSession>(factory, settings);
51+
}
4052

4153
Pools[settings.ConnectionString] = newSessionPool;
4254

src/Ydb.Sdk/src/Ado/Session/ImplicitSessionSource.cs

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,49 +3,57 @@ namespace Ydb.Sdk.Ado.Session;
33
internal sealed class ImplicitSessionSource : ISessionSource
44
{
55
private readonly IDriver _driver;
6-
private readonly Action? _onEmpty;
7-
private int _leased;
8-
private int _closed;
6+
private readonly Action? _onBecameEmpty;
7+
private int _isDisposed;
8+
private int _activeLeaseCount;
99

1010
internal ImplicitSessionSource(IDriver driver, Action? onEmpty = null)
1111
{
12-
_driver = driver;
13-
_onEmpty = onEmpty;
12+
_driver = driver ?? throw new ArgumentNullException(nameof(driver));
13+
_onBecameEmpty = onEmpty;
1414
}
1515

1616
public ValueTask<ISession> OpenSession(CancellationToken cancellationToken)
1717
{
1818
cancellationToken.ThrowIfCancellationRequested();
1919

20-
if (Volatile.Read(ref _closed) == 1)
20+
if (!TryAcquireLease())
2121
throw new ObjectDisposedException(nameof(ImplicitSessionSource));
2222

23-
Interlocked.Increment(ref _leased);
23+
return new ValueTask<ISession>(new ImplicitSession(_driver, ReleaseLease));
24+
}
25+
26+
private bool TryAcquireLease()
27+
{
28+
if (Volatile.Read(ref _isDisposed) != 0)
29+
return false;
2430

25-
if (Volatile.Read(ref _closed) == 1)
31+
Interlocked.Increment(ref _activeLeaseCount);
32+
33+
if (Volatile.Read(ref _isDisposed) != 0)
2634
{
27-
Interlocked.Decrement(ref _leased);
28-
throw new ObjectDisposedException(nameof(ImplicitSessionSource));
35+
Interlocked.Decrement(ref _activeLeaseCount);
36+
return false;
2937
}
3038

31-
return new ValueTask<ISession>(new ImplicitSession(_driver, Release));
39+
return true;
3240
}
3341

34-
private void Release()
42+
private void ReleaseLease()
3543
{
36-
if (Interlocked.Decrement(ref _leased) == 0)
44+
if (Interlocked.Decrement(ref _activeLeaseCount) == 0)
3745
{
38-
_onEmpty?.Invoke();
46+
_onBecameEmpty?.Invoke();
3947
}
4048
}
4149

4250
public ValueTask DisposeAsync()
4351
{
44-
Interlocked.Exchange(ref _closed, 1);
52+
Interlocked.Exchange(ref _isDisposed, 1);
4553

46-
if (Volatile.Read(ref _leased) == 0)
54+
if (Volatile.Read(ref _activeLeaseCount) == 0)
4755
{
48-
_onEmpty?.Invoke();
56+
_onBecameEmpty?.Invoke();
4957
}
5058

5159
return ValueTask.CompletedTask;

src/Ydb.Sdk/src/Ado/YdbConnection.cs

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,6 @@ internal ISession Session
3939

4040
private ISession _session = null!;
4141

42-
private ImplicitSessionSource? _implicitSessionSource;
43-
44-
internal bool EnableImplicitSession => ConnectionStringBuilder.EnableImplicitSession;
45-
46-
internal ISession GetExecutionSession(bool useImplicit)
47-
{
48-
ThrowIfConnectionClosed();
49-
50-
if (!useImplicit)
51-
return Session;
52-
53-
_implicitSessionSource ??=
54-
new ImplicitSessionSource(Session.Driver, onEmpty: () => _implicitSessionSource = null);
55-
return _implicitSessionSource.OpenSession(CancellationToken.None).GetAwaiter().GetResult();
56-
}
57-
5842
public YdbConnection()
5943
{
6044
}
@@ -143,7 +127,6 @@ public override async Task CloseAsync()
143127
finally
144128
{
145129
_session.Close();
146-
_implicitSessionSource = null;
147130
}
148131
}
149132

@@ -165,7 +148,7 @@ public override string ConnectionString
165148

166149
public override ConnectionState State => ConnectionState;
167150

168-
private ConnectionState ConnectionState { get; set; } = ConnectionState.Closed; // Invoke AsyncOpen()
151+
private ConnectionState ConnectionState { get; set; } = ConnectionState.Closed;
169152

170153
internal void OnNotSuccessStatusCode(StatusCode code)
171154
{

0 commit comments

Comments
 (0)