Skip to content

Commit 41442c4

Browse files
fixes
1 parent 80dbd9b commit 41442c4

File tree

3 files changed

+36
-29
lines changed

3 files changed

+36
-29
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,12 @@ public override async Task OpenAsync(CancellationToken cancellationToken)
100100
{
101101
throw e switch
102102
{
103+
Driver.TransportException { Status.StatusCode: StatusCode.Cancelled } or OperationCanceledException =>
104+
throw new YdbException(
105+
$"The connection pool has been exhausted, either raise 'MaxSessionPool' " +
106+
$"(currently {ConnectionStringBuilder.MaxSessionPool}) or 'CreateSessionTimeout' " +
107+
$"(currently {ConnectionStringBuilder.CreateSessionTimeout} seconds) in your connection string."
108+
),
103109
Driver.TransportException transportException => new YdbException(transportException),
104110
StatusUnsuccessfulException unsuccessfulException => new YdbException(unsuccessfulException.Status),
105111
_ => e

src/Ydb.Sdk/src/Pool/SessionPool.cs

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -42,40 +42,29 @@ internal async Task<TSession> GetSession(CancellationToken cancellationToken = d
4242
throw new YdbException("Session pool is closed");
4343
}
4444

45-
try
46-
{
47-
await _semaphore.WaitAsync(finalCancellationToken);
48-
Interlocked.Decrement(ref _waitingCount);
49-
50-
if (_idleSessions.TryDequeue(out var session) && session.IsActive)
51-
{
52-
return session;
53-
}
45+
await _semaphore.WaitAsync(finalCancellationToken);
46+
Interlocked.Decrement(ref _waitingCount);
5447

55-
if (session != null) // not active
56-
{
57-
Logger.LogDebug("Session[{Id}] isn't active, creating new session", session.SessionId);
58-
}
48+
if (_idleSessions.TryDequeue(out var session) && session.IsActive)
49+
{
50+
return session;
51+
}
5952

60-
try
61-
{
62-
return await CreateSession(finalCancellationToken);
63-
}
64-
catch (Exception e)
65-
{
66-
Release();
53+
if (session != null) // not active
54+
{
55+
Logger.LogDebug("Session[{Id}] isn't active, creating new session", session.SessionId);
56+
}
6757

68-
Logger.LogError(e, "Failed to create a session");
69-
throw;
70-
}
58+
try
59+
{
60+
return await CreateSession(finalCancellationToken);
7161
}
72-
catch (Exception e) when (
73-
e is Driver.TransportException { Status.StatusCode: StatusCode.Cancelled } or OperationCanceledException
74-
)
62+
catch (Exception e)
7563
{
76-
throw new YdbException($"The connection pool has been exhausted, either raise 'MaxSessionPool' " +
77-
$"(currently {_size}) or 'CreateSessionTimeout' " +
78-
$"(currently {_createSessionTimeoutMs / 1000} seconds) in your connection string.");
64+
Release();
65+
66+
Logger.LogError(e, "Failed to create a session");
67+
throw;
7968
}
8069
}
8170

src/Ydb.Sdk/tests/Ado/YdbConnectionTests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,18 @@ public async Task DisableDiscovery_WhenPropertyIsTrue_SimpleWorking()
212212
await YdbConnection.ClearPool(connection);
213213
}
214214

215+
[Fact]
216+
public async Task OpenAsync_WhenCancelTokenIsCanceled_ThrowYdbException()
217+
{
218+
await using var connection = CreateConnection();
219+
connection.ConnectionString = ConnectionString;
220+
using var cts = new CancellationTokenSource();
221+
cts.Cancel();
222+
Assert.Equal("The connection pool has been exhausted, either raise 'MaxSessionPool' (currently 10) " +
223+
"or 'CreateSessionTimeout' (currently 5 seconds) in your connection string.",
224+
(await Assert.ThrowsAsync<YdbException>(async () => await connection.OpenAsync(cts.Token))).Message);
225+
}
226+
215227
private List<Task> GenerateTasks() => Enumerable.Range(0, 100).Select(async i =>
216228
{
217229
await using var connection = await CreateOpenConnectionAsync();

0 commit comments

Comments
 (0)