Skip to content

Commit 0f03b57

Browse files
feat: PoolingSessionSource 2.0 based on Npgsql pooling algorithm (#477)
1 parent f95909b commit 0f03b57

File tree

7 files changed

+343
-16
lines changed

7 files changed

+343
-16
lines changed

src/Ydb.Sdk/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
- ADO.NET: PoolingSessionSource 2.0 based on Npgsql pooling algorithm.
12
- Added new ADO.NET options:
23
- `MinSessionPool`: The minimum connection pool size.
34
- `SessionIdleTimeout`: The time (in seconds) to wait before closing idle session in the pool if the count of all sessions exceeds `MinSessionPool`.

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ namespace Ydb.Sdk.Ado.Session;
66

77
internal interface ISession
88
{
9+
bool IsBroken { get; }
10+
911
ValueTask<IServerStream<ExecuteQueryResponsePart>> ExecuteQuery(
1012
string query,
1113
Dictionary<string, YdbValue> parameters,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ namespace Ydb.Sdk.Ado.Session;
22

33
internal interface ISessionSource<TSession> where TSession : ISession
44
{
5-
ValueTask<TSession> OpenSession();
5+
ValueTask<TSession> OpenSession(CancellationToken cancellationToken = default);
66

77
void Return(TSession session);
88
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ public ImplicitSession(IDriver driver)
1313
_driver = driver;
1414
}
1515

16+
public bool IsBroken => false;
17+
1618
public ValueTask<IServerStream<ExecuteQueryResponsePart>> ExecuteQuery(
1719
string query,
1820
Dictionary<string, YdbValue> parameters,

src/Ydb.Sdk/src/Ado/Session/Session.cs renamed to src/Ydb.Sdk/src/Ado/Session/PoolingSession.cs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
namespace Ydb.Sdk.Ado.Session;
1010

11-
internal class Session : IPoolingSession
11+
internal class PoolingSession : IPoolingSession
1212
{
1313
private const string SessionBalancer = "session-balancer";
1414

@@ -18,20 +18,20 @@ internal class Session : IPoolingSession
1818
private readonly IDriver _driver;
1919
private readonly PoolingSessionSource _poolingSessionSource;
2020
private readonly YdbConnectionStringBuilder _settings;
21-
private readonly ILogger<Session> _logger;
21+
private readonly ILogger<PoolingSession> _logger;
2222

23-
private volatile bool _isActive;
23+
private volatile bool _isBroken;
2424

2525
private string SessionId { get; set; } = string.Empty;
2626
private long NodeId { get; set; }
2727

28-
public bool IsActive => _isActive;
28+
public bool IsBroken => _isBroken;
2929

30-
internal Session(
30+
internal PoolingSession(
3131
IDriver driver,
3232
PoolingSessionSource poolingSessionSource,
3333
YdbConnectionStringBuilder settings,
34-
ILogger<Session> logger
34+
ILogger<PoolingSession> logger
3535
)
3636
{
3737
_driver = driver;
@@ -109,7 +109,7 @@ StatusCode.Unavailable or
109109
{
110110
_logger.LogWarning("Session[{SessionId}] is deactivated. Reason StatusCode: {Code}", SessionId, code);
111111

112-
_isActive = false;
112+
_isBroken = true;
113113
}
114114
}
115115

@@ -176,7 +176,7 @@ public async Task Open(CancellationToken cancellationToken)
176176

177177
OnNotSuccessStatusCode(statusCode);
178178

179-
if (!IsActive)
179+
if (IsBroken)
180180
{
181181
return;
182182
}
@@ -204,7 +204,7 @@ public async Task Open(CancellationToken cancellationToken)
204204
}
205205
finally
206206
{
207-
_isActive = false;
207+
_isBroken = true;
208208
}
209209
}, cancellationToken);
210210

@@ -215,7 +215,12 @@ public async Task DeleteSession()
215215
{
216216
try
217217
{
218-
_isActive = false;
218+
if (_isBroken)
219+
{
220+
return;
221+
}
222+
223+
_isBroken = true;
219224

220225
var deleteSessionResponse = await _driver.UnaryCall(
221226
QueryService.DeleteSessionMethod,
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using Microsoft.Extensions.Logging;
2+
3+
namespace Ydb.Sdk.Ado.Session;
4+
5+
internal class PoolingSessionFactory : IPoolingSessionFactory
6+
{
7+
private readonly IDriver _driver;
8+
private readonly YdbConnectionStringBuilder _settings;
9+
private readonly ILogger<PoolingSession> _logger;
10+
11+
public PoolingSessionFactory(IDriver driver, YdbConnectionStringBuilder settings, ILogger<PoolingSession> logger)
12+
{
13+
_driver = driver;
14+
_settings = settings;
15+
_logger = logger;
16+
}
17+
18+
public IPoolingSession NewSession(PoolingSessionSource source) =>
19+
new PoolingSession(_driver, source, _settings, _logger);
20+
}

0 commit comments

Comments
 (0)