Skip to content

Commit

Permalink
Session detach
Browse files Browse the repository at this point in the history
  • Loading branch information
XmasApple committed Nov 8, 2023
1 parent 77c6d37 commit e133940
Showing 1 changed file with 38 additions and 1 deletion.
39 changes: 38 additions & 1 deletion src/Ydb.Sdk/src/Services/Query/SessionPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ namespace Ydb.Sdk.Services.Query;

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

public SessionPool(Driver driver, SessionPoolConfig config) :
base(
driver: driver,
Expand Down Expand Up @@ -77,6 +79,23 @@ private async Task AttachAndMonitor(string sessionId)

CheckPart(firstPart, sessionId);

cts = new CancellationTokenSource();

var monitorTask = Task.Run(async () => await Monitor(sessionId, stream), cts.Token);
lock (Lock)
{
_attachedSessions.Add(sessionId, cts);
}

await monitorTask;
lock (Lock)
{
_attachedSessions.Remove(sessionId);
}
}

private async Task Monitor(string sessionId, SessionStateStream stream)
{
while (await stream.Next())
{
var part = stream.Response;
Expand Down Expand Up @@ -104,6 +123,22 @@ private void CheckPart(Query.SessionState part, string sessionId)
}
}

internal new void InvalidateSession(string id)
{
DetachSession(id);
base.InvalidateSession(id);
}

private void DetachSession(string id)
{
lock (Lock)
{
_attachedSessions.Remove(id, out var cts);
cts?.Cancel();
Logger.LogInformation($"Session detached: {id}");
}
}

private protected override Session CopySession(Session other)
{
return new Session(
Expand All @@ -116,9 +151,11 @@ private protected override Session CopySession(Session other)

private protected override void DeleteSession(string id)
{
DetachSession(id);

_ = Client.DeleteSession(id, new DeleteSessionSettings
{
TransportTimeout = Shared.Session.DeleteSessionTimeout
});
}
}
}

0 comments on commit e133940

Please sign in to comment.