Skip to content

Commit

Permalink
Refactor abstract base classes for sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
XmasApple committed Nov 13, 2023
1 parent 03db110 commit 29123b1
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 33 deletions.
9 changes: 1 addition & 8 deletions src/Ydb.Sdk/src/Services/Query/QueryClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,11 @@
using Ydb.Query;
using Ydb.Query.V1;
using Ydb.Sdk.Client;
using Ydb.Sdk.Services.Shared;
using Ydb.Sdk.Services.Sessions;
using Ydb.Sdk.Value;

namespace Ydb.Sdk.Services.Query;

public enum TxMode
{
SerializableRW,
OnlineRO,
StaleRO
}

public enum ExecMode
{
Unspecified = 0,
Expand Down
12 changes: 6 additions & 6 deletions src/Ydb.Sdk/src/Services/Query/Session.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Microsoft.Extensions.Logging;
using Ydb.Sdk.Services.Shared;
using Ydb.Sdk.Services.Sessions;

namespace Ydb.Sdk.Services.Query;

Expand All @@ -10,14 +10,14 @@ namespace Ydb.Sdk.Services.Query;
/// 2. Distribute load evenly across multiple DB nodes.
/// 3. Store state for volatile stateful operations, such as short-living transactions.
/// </summary>
public class Session : Shared.Session
public class Session : SessionBase
{
internal readonly SessionPool? SessionPool;
private readonly SessionPool? _sessionPool;

internal Session(Driver driver, SessionPool? sessionPool, string id, long nodeId, string? endpoint)
: base(driver, id, endpoint, driver.LoggerFactory.CreateLogger<Session>())
{
SessionPool = sessionPool;
_sessionPool = sessionPool;
NodeId = nodeId;
}

Expand All @@ -33,7 +33,7 @@ protected override void Dispose(bool disposing)

if (disposing)
{
if (SessionPool is null)
if (_sessionPool is null)
{
Logger.LogTrace($"Closing detached session on dispose: {Id}");

Expand All @@ -45,7 +45,7 @@ protected override void Dispose(bool disposing)
}
else
{
SessionPool.ReturnSession(Id);
_sessionPool.ReturnSession(Id);
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/Ydb.Sdk/src/Services/Query/SessionPool.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using Microsoft.Extensions.Logging;
using Ydb.Sdk.Services.Shared;
using Ydb.Sdk.Services.Sessions;

namespace Ydb.Sdk.Services.Query;

using GetSessionResponse = GetSessionResponse<Session>;
using NoPool = NoPool<Session>;

internal class SessionPool : SessionPool<Session, QueryClient>
internal class SessionPool : SessionPoolBase<Session, QueryClient>
{
private readonly Dictionary<string, CancellationTokenSource> _attachedSessions = new();

Expand Down Expand Up @@ -155,7 +155,7 @@ private protected override void DeleteSession(string id)

_ = Client.DeleteSession(id, new DeleteSessionSettings
{
TransportTimeout = Shared.Session.DeleteSessionTimeout
TransportTimeout = SessionBase.DeleteSessionTimeout
});
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using Microsoft.Extensions.Logging;
using Ydb.Sdk.Client;

namespace Ydb.Sdk.Services.Shared;
namespace Ydb.Sdk.Services.Sessions;

public abstract class Session : ClientBase, IDisposable
public abstract class SessionBase : ClientBase, IDisposable
{
internal static readonly TimeSpan DeleteSessionTimeout = TimeSpan.FromSeconds(1);

Expand All @@ -15,7 +15,7 @@ public abstract class Session : ClientBase, IDisposable
protected readonly ILogger Logger;


protected Session(Driver driver, string id, string? endpoint, ILogger logger) : base(driver)
protected SessionBase(Driver driver, string id, string? endpoint, ILogger logger) : base(driver)
{
Id = id;
Endpoint = endpoint;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Microsoft.Extensions.Logging;
using Ydb.Sdk.Client;

namespace Ydb.Sdk.Services.Shared;
namespace Ydb.Sdk.Services.Sessions;

public class SessionPoolConfig
{
Expand All @@ -19,7 +19,7 @@ public SessionPoolConfig(
public TimeSpan CreateSessionTimeout { get; set; } = TimeSpan.FromSeconds(1);
}

public class GetSessionResponse<TSession> : ResponseWithResultBase<TSession>, IDisposable where TSession : Session
public class GetSessionResponse<TSession> : ResponseWithResultBase<TSession>, IDisposable where TSession : SessionBase
{
internal GetSessionResponse(Status status, TSession? session = null)
: base(status, session)
Expand All @@ -40,12 +40,12 @@ protected void Dispose(bool disposing)
}
}

internal interface ISessionPool<TSession> : IDisposable where TSession : Session
internal interface ISessionPool<TSession> : IDisposable where TSession : SessionBase
{
public Task<GetSessionResponse<TSession>> GetSession();
}

internal class NoPool<TSession> : ISessionPool<TSession> where TSession : Session
internal class NoPool<TSession> : ISessionPool<TSession> where TSession : SessionBase
{
public Task<GetSessionResponse<TSession>> GetSession()
{
Expand All @@ -57,8 +57,8 @@ public void Dispose()
}
}

public abstract class SessionPool<TSession, TClient> : ISessionPool<TSession>
where TSession : Session
public abstract class SessionPoolBase<TSession, TClient> : ISessionPool<TSession>
where TSession : SessionBase
where TClient : ClientBase
{
private protected readonly Driver Driver;
Expand All @@ -75,7 +75,7 @@ public abstract class SessionPool<TSession, TClient> : ISessionPool<TSession>
private protected readonly Stack<string> IdleSessions = new();
protected uint PendingSessions;

protected SessionPool(Driver driver, SessionPoolConfig config, TClient client, ILogger logger)
protected SessionPoolBase(Driver driver, SessionPoolConfig config, TClient client, ILogger logger)
{
Driver = driver;
Config = config;
Expand Down
4 changes: 2 additions & 2 deletions src/Ydb.Sdk/src/Services/Table/Session.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using Grpc.Core;
using Microsoft.Extensions.Logging;
using Ydb.Sdk.Services.Shared;
using Ydb.Sdk.Services.Sessions;

namespace Ydb.Sdk.Services.Table;

public partial class Session : Shared.Session
public partial class Session : SessionBase
{
private readonly SessionPool? _sessionPool;

Expand Down
6 changes: 3 additions & 3 deletions src/Ydb.Sdk/src/Services/Table/SessionPool.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using Microsoft.Extensions.Logging;
using Ydb.Sdk.Services.Shared;
using Ydb.Sdk.Services.Sessions;

namespace Ydb.Sdk.Services.Table;

using GetSessionResponse = GetSessionResponse<Session>;
using NoPool = NoPool<Session>;

internal sealed class SessionPool : SessionPool<Session, TableClient>
internal sealed class SessionPool : SessionPoolBase<Session, TableClient>
{
public SessionPool(Driver driver, SessionPoolConfig config) :
base(
Expand Down Expand Up @@ -144,7 +144,7 @@ private protected override void DeleteSession(string id)
{
_ = Client.DeleteSession(id, new DeleteSessionSettings
{
TransportTimeout = Shared.Session.DeleteSessionTimeout
TransportTimeout = SessionBase.DeleteSessionTimeout
});
}
}
2 changes: 1 addition & 1 deletion src/Ydb.Sdk/src/Services/Table/TableClient.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Ydb.Sdk.Client;
using Ydb.Sdk.Services.Shared;
using Ydb.Sdk.Services.Sessions;

namespace Ydb.Sdk.Services.Table;

Expand Down

0 comments on commit 29123b1

Please sign in to comment.