Skip to content

Commit

Permalink
Remove DeleteOnDispose flag
Browse files Browse the repository at this point in the history
  • Loading branch information
XmasApple committed Nov 21, 2023
1 parent 7a9d8f6 commit b257d80
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 48 deletions.
10 changes: 3 additions & 7 deletions src/Ydb.Sdk/src/Services/Table/CreateSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ namespace Ydb.Sdk.Services.Table;

public class CreateSessionSettings : OperationRequestSettings
{
public bool DeleteOnDispose = true;
}

public class CreateSessionResponse : ResponseWithResultBase<CreateSessionResponse.ResultData>
Expand All @@ -25,15 +24,13 @@ internal ResultData(Session session)

public Session Session { get; }

internal static ResultData FromProto(CreateSessionResult resultProto, Driver driver, string endpoint,
bool deleteOnDispose)
internal static ResultData FromProto(CreateSessionResult resultProto, Driver driver, string endpoint)
{
var session = new Session(
driver: driver,
sessionPool: null,
id: resultProto.SessionId,
endpoint: endpoint,
deleteOnDispose: deleteOnDispose);
endpoint: endpoint);

return new ResultData(
session: session
Expand Down Expand Up @@ -65,8 +62,7 @@ public async Task<CreateSessionResponse> CreateSession(CreateSessionSettings? se
CreateSessionResponse.ResultData? result = null;
if (status.IsSuccess && resultProto != null)
{
result = CreateSessionResponse.ResultData.FromProto(resultProto, Driver, response.UsedEndpoint,
settings.DeleteOnDispose);
result = CreateSessionResponse.ResultData.FromProto(resultProto, Driver, response.UsedEndpoint);
}

return new CreateSessionResponse(status, result);
Expand Down
22 changes: 8 additions & 14 deletions src/Ydb.Sdk/src/Services/Table/Session.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,13 @@ public partial class Session : ClientBase, IDisposable
private readonly ILogger _logger;
private bool _disposed;

private readonly bool _deleteOnDispose;

internal Session(Driver driver, SessionPool? sessionPool, string id, string? endpoint, bool deleteOnDispose = true)
internal Session(Driver driver, SessionPool? sessionPool, string id, string? endpoint)
: base(driver)
{
_sessionPool = sessionPool;
_logger = Driver.LoggerFactory.CreateLogger<Session>();
Id = id;
Endpoint = endpoint;
_deleteOnDispose = deleteOnDispose;
}

public string Id { get; }
Expand Down Expand Up @@ -76,17 +73,14 @@ protected virtual void Dispose(bool disposing)
{
if (_sessionPool is null)
{
if (_deleteOnDispose)
_logger.LogTrace($"Closing detached session on dispose: {Id}");

var client = new TableClient(Driver, new NoPool());
var task = client.DeleteSession(Id, new DeleteSessionSettings
{
_logger.LogTrace($"Closing detached session on dispose: {Id}");

var client = new TableClient(Driver, new NoPool());
var task = client.DeleteSession(Id, new DeleteSessionSettings
{
TransportTimeout = DeleteSessionTimeout
});
task.Wait();
}
TransportTimeout = DeleteSessionTimeout
});
task.Wait();
}
else
{
Expand Down
41 changes: 14 additions & 27 deletions src/Ydb.Sdk/src/Services/Table/SessionPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,6 @@ public SessionPoolConfig(
internal TimeSpan PeriodicCheckInterval { get; } = TimeSpan.FromSeconds(10);
internal TimeSpan KeepAliveTimeout { get; } = TimeSpan.FromSeconds(1);
internal TimeSpan CreateSessionTimeout { get; } = TimeSpan.FromSeconds(1);


/// <summary>
/// If true, sending a Delete Session request to the server when deleting and waiting for it
/// If false, the SDK does not send a Delete Session request to the server, this may increase performance, but may lead to an increase in the value of closing idle sessions on monitoring
/// </summary>
internal bool DeleteSessionsOnDispose { get; } = true;
}

internal class GetSessionResponse : ResponseWithResultBase<Session>, IDisposable
Expand Down Expand Up @@ -126,8 +119,7 @@ public async Task<GetSessionResponse> GetSession()
var createSessionResponse = await _client.CreateSession(new CreateSessionSettings
{
TransportTimeout = _config.CreateSessionTimeout,
OperationTimeout = _config.CreateSessionTimeout,
DeleteOnDispose = _config.DeleteSessionsOnDispose
OperationTimeout = _config.CreateSessionTimeout
});

lock (_lock)
Expand All @@ -140,8 +132,7 @@ public async Task<GetSessionResponse> GetSession()
driver: _driver,
sessionPool: this,
id: createSessionResponse.Result.Session.Id,
endpoint: createSessionResponse.Result.Session.Endpoint,
deleteOnDispose: _config.DeleteSessionsOnDispose);
endpoint: createSessionResponse.Result.Session.Endpoint);

_sessions.Add(session.Id, new SessionState(session));

Expand All @@ -166,8 +157,7 @@ internal void ReturnSession(string id)
driver: _driver,
sessionPool: this,
id: id,
endpoint: oldSession.Session.Endpoint,
deleteOnDispose: _config.DeleteSessionsOnDispose);
endpoint: oldSession.Session.Endpoint);

_sessions[id] = new SessionState(session);
_idleSessions.Push(id);
Expand Down Expand Up @@ -283,23 +273,20 @@ private void Dispose(bool disposing)

if (disposing)
{
if (_config.DeleteSessionsOnDispose)
var tasks = new Task[_sessions.Count];
var i = 0;
foreach (var state in _sessions.Values)
{
var tasks = new Task[_sessions.Count];
var i = 0;
foreach (var state in _sessions.Values)
{
_logger.LogTrace($"Closing session on session pool dispose: {state.Session.Id}");
_logger.LogTrace($"Closing session on session pool dispose: {state.Session.Id}");

var task = _client.DeleteSession(state.Session.Id, new DeleteSessionSettings
{
TransportTimeout = Session.DeleteSessionTimeout
});
tasks[i++] = task;
}

Task.WaitAll(tasks);
var task = _client.DeleteSession(state.Session.Id, new DeleteSessionSettings
{
TransportTimeout = Session.DeleteSessionTimeout
});
tasks[i++] = task;
}

Task.WaitAll(tasks);
}

_disposed = true;
Expand Down

0 comments on commit b257d80

Please sign in to comment.