diff --git a/src/Ydb.Sdk/src/IDriver.cs b/src/Ydb.Sdk/src/IDriver.cs index 97d1a080..8946d3e3 100644 --- a/src/Ydb.Sdk/src/IDriver.cs +++ b/src/Ydb.Sdk/src/IDriver.cs @@ -4,23 +4,23 @@ namespace Ydb.Sdk; -public interface IDriver : IAsyncDisposable, IDisposable +internal interface IDriver : IAsyncDisposable, IDisposable { - internal Task UnaryCall( + Task UnaryCall( Method method, TRequest request, GrpcRequestSettings settings) where TRequest : class where TResponse : class; - internal ServerStream ServerStreamCall( + ServerStream ServerStreamCall( Method method, TRequest request, GrpcRequestSettings settings) where TRequest : class where TResponse : class; - internal BidirectionalStream BidirectionalStreamCall( + BidirectionalStream BidirectionalStreamCall( Method method, GrpcRequestSettings settings) where TRequest : class @@ -33,6 +33,7 @@ public abstract class BaseDriver : IDriver { protected readonly DriverConfig Config; protected readonly ILogger Logger; + protected readonly ILoggerFactory LoggerFactory; protected int Disposed; @@ -43,7 +44,7 @@ protected BaseDriver(DriverConfig config, ILoggerFactory loggerFactory, ILogger LoggerFactory = loggerFactory; } - public async Task UnaryCall( + async Task IDriver.UnaryCall( Method method, TRequest request, GrpcRequestSettings settings) @@ -155,7 +156,7 @@ protected CallOptions GetCallOptions(GrpcRequestSettings settings, bool streamin return options; } - public ILoggerFactory LoggerFactory { get; } + ILoggerFactory IDriver.LoggerFactory => LoggerFactory; public void Dispose() { @@ -166,6 +167,8 @@ public async ValueTask DisposeAsync() { if (Interlocked.CompareExchange(ref Disposed, 1, 0) == 0) { + GC.SuppressFinalize(this); + await InternalDispose(); } } diff --git a/src/Ydb.Sdk/src/Pool/ChannelPool.cs b/src/Ydb.Sdk/src/Pool/ChannelPool.cs index 0e19863f..84b25630 100644 --- a/src/Ydb.Sdk/src/Pool/ChannelPool.cs +++ b/src/Ydb.Sdk/src/Pool/ChannelPool.cs @@ -7,6 +7,10 @@ using Microsoft.Extensions.Logging; using Org.BouncyCastle.Security; +#if TEST +[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")] +#endif + namespace Ydb.Sdk.Pool; internal class ChannelPool : IAsyncDisposable where T : ChannelBase, IDisposable diff --git a/src/Ydb.Sdk/src/Services/Auth/AuthClient.cs b/src/Ydb.Sdk/src/Services/Auth/AuthClient.cs index 36352383..af3d5495 100644 --- a/src/Ydb.Sdk/src/Services/Auth/AuthClient.cs +++ b/src/Ydb.Sdk/src/Services/Auth/AuthClient.cs @@ -37,9 +37,9 @@ public async Task Login(string user, string? password, LoginSetti try { - await using var transport = new AuthGrpcChannelDriver(_config, _grpcChannelFactory, _loggerFactory); + await using var driver = Driver; - var response = await transport.UnaryCall( + var response = await driver.UnaryCall( method: AuthService.LoginMethod, request: request, settings: settings @@ -61,6 +61,8 @@ public async Task Login(string user, string? password, LoginSetti return new LoginResponse(e.Status); } } + + private IDriver Driver => new AuthGrpcChannelDriver(_config, _grpcChannelFactory, _loggerFactory); } public class LoginSettings : OperationSettings diff --git a/src/Ydb.Sdk/src/Services/Operations/OperationsClient.cs b/src/Ydb.Sdk/src/Services/Operations/OperationsClient.cs index 1c623528..3f5bc155 100644 --- a/src/Ydb.Sdk/src/Services/Operations/OperationsClient.cs +++ b/src/Ydb.Sdk/src/Services/Operations/OperationsClient.cs @@ -2,10 +2,15 @@ public partial class OperationsClient { - private readonly Driver _driver; + private readonly IDriver _driver; public OperationsClient(Driver driver) { _driver = driver; } + + internal OperationsClient(IDriver driver) + { + _driver = driver; + } } diff --git a/src/Ydb.Sdk/src/Services/Query/SessionPool.cs b/src/Ydb.Sdk/src/Services/Query/SessionPool.cs index 09e88a31..798219d7 100644 --- a/src/Ydb.Sdk/src/Services/Query/SessionPool.cs +++ b/src/Ydb.Sdk/src/Services/Query/SessionPool.cs @@ -20,10 +20,10 @@ internal sealed class SessionPool : SessionPool, IAsyncDisposable TransportTimeout = TimeSpan.FromMinutes(1) }; - private readonly Driver _driver; + private readonly IDriver _driver; private readonly bool _disposingDriver; - internal SessionPool(Driver driver, int? maxSessionPool = null, bool disposingDriver = false) + internal SessionPool(IDriver driver, int? maxSessionPool = null, bool disposingDriver = false) : base(driver.LoggerFactory.CreateLogger(), maxSessionPool) { _driver = driver; @@ -132,9 +132,9 @@ protected override ValueTask DisposeDriver() internal class Session : SessionBase { - private readonly Driver _driver; + private readonly IDriver _driver; - internal Session(Driver driver, SessionPool sessionPool, string sessionId, long nodeId) + internal Session(IDriver driver, SessionPool sessionPool, string sessionId, long nodeId) : base(sessionPool, sessionId, nodeId) { _driver = driver; diff --git a/src/Ydb.Sdk/src/Services/Scheme/SchemeClient.cs b/src/Ydb.Sdk/src/Services/Scheme/SchemeClient.cs index e8757b61..85cfadaf 100644 --- a/src/Ydb.Sdk/src/Services/Scheme/SchemeClient.cs +++ b/src/Ydb.Sdk/src/Services/Scheme/SchemeClient.cs @@ -42,7 +42,7 @@ internal static Permissions FromProto(Ydb.Scheme.Permissions permissionsProto) public class SchemeEntry { - internal SchemeEntry( + private SchemeEntry( string name, string owner, SchemeEntryType type, @@ -69,11 +69,11 @@ internal static SchemeEntry FromProto(Entry entryProto) : SchemeEntryType.Unspecified; var effectivePermissions = entryProto.EffectivePermissions - .Select(p => Scheme.Permissions.FromProto(p)) + .Select(Scheme.Permissions.FromProto) .ToList(); var permissions = entryProto.Permissions - .Select(p => Scheme.Permissions.FromProto(p)) + .Select(Scheme.Permissions.FromProto) .ToList(); return new SchemeEntry( @@ -120,7 +120,7 @@ internal static ResultData FromProto(ListDirectoryResult resultProto) public class SchemeClient { - private readonly Driver _driver; + private readonly IDriver _driver; public SchemeClient(Driver driver) { diff --git a/src/Ydb.Sdk/src/Services/Sessions/SessionBase.cs b/src/Ydb.Sdk/src/Services/Sessions/SessionBase.cs index fd33e381..1d6f0dd4 100644 --- a/src/Ydb.Sdk/src/Services/Sessions/SessionBase.cs +++ b/src/Ydb.Sdk/src/Services/Sessions/SessionBase.cs @@ -4,7 +4,6 @@ namespace Ydb.Sdk.Services.Sessions; public abstract class SessionBase : IDisposable { - protected readonly Driver Driver; internal static readonly TimeSpan DeleteSessionTimeout = TimeSpan.FromSeconds(1); public string Id { get; } @@ -13,9 +12,8 @@ public abstract class SessionBase : IDisposable private protected bool Disposed; protected readonly ILogger Logger; - protected SessionBase(Driver driver, string id, long nodeId, ILogger logger) + protected SessionBase(string id, long nodeId, ILogger logger) { - Driver = driver; Id = id; NodeId = nodeId; Logger = logger; diff --git a/src/Ydb.Sdk/src/Services/Sessions/SessionPoolBase.cs b/src/Ydb.Sdk/src/Services/Sessions/SessionPoolBase.cs index 6b93fbe1..77fbc6b8 100644 --- a/src/Ydb.Sdk/src/Services/Sessions/SessionPoolBase.cs +++ b/src/Ydb.Sdk/src/Services/Sessions/SessionPoolBase.cs @@ -55,11 +55,11 @@ public void Dispose() } } -public abstract class SessionPoolBase : ISessionPool where TSession : SessionBase +internal abstract class SessionPoolBase : ISessionPool where TSession : SessionBase { private const int MaxAttempts = 100; - private protected readonly Driver Driver; + private protected readonly IDriver Driver; private protected readonly ILogger Logger; private protected readonly SessionPoolConfig Config; @@ -70,7 +70,7 @@ public abstract class SessionPoolBase : ISessionPool where T private protected readonly Stack IdleSessions = new(); protected uint PendingSessions; - protected SessionPoolBase(Driver driver, SessionPoolConfig config, ILogger logger) + protected SessionPoolBase(IDriver driver, SessionPoolConfig config, ILogger logger) { Driver = driver; Config = config; diff --git a/src/Ydb.Sdk/src/Services/Table/CreateSession.cs b/src/Ydb.Sdk/src/Services/Table/CreateSession.cs index 145f1ac8..a86a6d23 100644 --- a/src/Ydb.Sdk/src/Services/Table/CreateSession.cs +++ b/src/Ydb.Sdk/src/Services/Table/CreateSession.cs @@ -26,7 +26,7 @@ private ResultData(Session session) public Session Session { get; } - internal static ResultData FromProto(CreateSessionResult resultProto, Driver driver) + internal static ResultData FromProto(CreateSessionResult resultProto, IDriver driver) { var session = new Session( driver: driver, diff --git a/src/Ydb.Sdk/src/Services/Table/Session.cs b/src/Ydb.Sdk/src/Services/Table/Session.cs index 9525827e..fe53db59 100644 --- a/src/Ydb.Sdk/src/Services/Table/Session.cs +++ b/src/Ydb.Sdk/src/Services/Table/Session.cs @@ -7,10 +7,12 @@ namespace Ydb.Sdk.Services.Table; public partial class Session : SessionBase { private readonly SessionPool? _sessionPool; + private readonly IDriver _driver; - internal Session(Driver driver, SessionPool? sessionPool, string id, long nodeId) - : base(driver, id, nodeId, driver.LoggerFactory.CreateLogger()) + internal Session(IDriver driver, SessionPool? sessionPool, string id, long nodeId) + : base(id, nodeId, driver.LoggerFactory.CreateLogger()) { + _driver = driver; _sessionPool = sessionPool; } @@ -48,7 +50,7 @@ protected override void Dispose(bool disposing) { Logger.LogTrace($"Closing detached session on dispose: {Id}"); - var client = new TableClient(Driver, new NoPool()); + var client = new TableClient(_driver, new NoPool()); var task = client.DeleteSession(Id, new DeleteSessionSettings { TransportTimeout = DeleteSessionTimeout @@ -74,7 +76,7 @@ private async Task UnaryCall( settings.NodeId = NodeId; settings.TrailersHandler = OnResponseTrailers; - return await Driver.UnaryCall( + return await _driver.UnaryCall( method: method, request: request, settings: settings diff --git a/src/Ydb.Sdk/src/Services/Table/SessionPool.cs b/src/Ydb.Sdk/src/Services/Table/SessionPool.cs index 9d1f921b..779d91fb 100644 --- a/src/Ydb.Sdk/src/Services/Table/SessionPool.cs +++ b/src/Ydb.Sdk/src/Services/Table/SessionPool.cs @@ -10,11 +10,8 @@ internal sealed class SessionPool : SessionPoolBase { private readonly TableClient _tableClient; - public SessionPool(Driver driver, SessionPoolConfig config) : - base( - driver: driver, - config: config, - logger: driver.LoggerFactory.CreateLogger()) + public SessionPool(IDriver driver, SessionPoolConfig config) : + base(driver: driver, config: config, logger: driver.LoggerFactory.CreateLogger()) { _tableClient = new TableClient(driver, new NoPool()); diff --git a/src/Ydb.Sdk/src/Services/Table/TableClient.cs b/src/Ydb.Sdk/src/Services/Table/TableClient.cs index a6e6b3b0..cb22aa59 100644 --- a/src/Ydb.Sdk/src/Services/Table/TableClient.cs +++ b/src/Ydb.Sdk/src/Services/Table/TableClient.cs @@ -16,7 +16,8 @@ public TableClientConfig( public partial class TableClient : IDisposable { private readonly ISessionPool _sessionPool; - private readonly Driver _driver; + private readonly IDriver _driver; + private readonly string _database; private bool _disposed; @@ -25,13 +26,15 @@ public TableClient(Driver driver, TableClientConfig? config = null) config ??= new TableClientConfig(); _driver = driver; + _database = driver.Database; _sessionPool = new SessionPool(driver, config.SessionPoolConfig); } - internal TableClient(Driver driver, ISessionPool sessionPool) + internal TableClient(IDriver driver, ISessionPool sessionPool) { _driver = driver; _sessionPool = sessionPool; + _database = ""; // stub legacy client } public void Dispose() @@ -57,6 +60,6 @@ private void Dispose(bool disposing) internal string MakeTablePath(string path) { - return path.StartsWith('/') ? path : $"{_driver.Database}/{path}"; + return path.StartsWith('/') ? path : $"{_database}/{path}"; } } diff --git a/src/Ydb.Sdk/src/Services/Topic/TopicClient.cs b/src/Ydb.Sdk/src/Services/Topic/TopicClient.cs index 265814df..e46a64c1 100644 --- a/src/Ydb.Sdk/src/Services/Topic/TopicClient.cs +++ b/src/Ydb.Sdk/src/Services/Topic/TopicClient.cs @@ -6,7 +6,7 @@ namespace Ydb.Sdk.Services.Topic; public class TopicClient { - private readonly Driver _driver; + private readonly IDriver _driver; public TopicClient(Driver driver) { diff --git a/src/Ydb.Sdk/tests/Tests.csproj b/src/Ydb.Sdk/tests/Tests.csproj index 18e84cda..298c2bf5 100644 --- a/src/Ydb.Sdk/tests/Tests.csproj +++ b/src/Ydb.Sdk/tests/Tests.csproj @@ -15,6 +15,10 @@ + + TEST + + @@ -40,5 +44,4 @@ -