Skip to content

Commit

Permalink
Merge pull request #43 Session create fix
Browse files Browse the repository at this point in the history
  • Loading branch information
rekby committed Dec 14, 2023
2 parents 5f82083 + 808410d commit f09a8fe
Showing 1 changed file with 29 additions and 6 deletions.
35 changes: 29 additions & 6 deletions src/Ydb.Sdk/src/Services/Table/SessionPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,25 @@ namespace Ydb.Sdk.Services.Table;
public class SessionPoolConfig
{
public SessionPoolConfig(
uint? sizeLimit = null)
uint? sizeLimit = null,
TimeSpan? keepAliveIdleThreshold = null,
TimeSpan? periodicCheckInterval = null,
TimeSpan? keepAliveTimeout = null,
TimeSpan? createSessionTimeout = null
)
{
SizeLimit = sizeLimit ?? 100;
KeepAliveIdleThreshold = keepAliveIdleThreshold ?? TimeSpan.FromMinutes(5);
PeriodicCheckInterval = periodicCheckInterval ?? TimeSpan.FromSeconds(10);
KeepAliveTimeout = keepAliveTimeout ?? TimeSpan.FromSeconds(1);
CreateSessionTimeout = createSessionTimeout ?? TimeSpan.FromSeconds(1);
}

public uint SizeLimit { get; }

internal TimeSpan KeepAliveIdleThreshold { get; } = TimeSpan.FromMinutes(5);
internal TimeSpan PeriodicCheckInterval { get; } = TimeSpan.FromSeconds(10);
internal TimeSpan KeepAliveTimeout { get; } = TimeSpan.FromSeconds(1);
internal TimeSpan CreateSessionTimeout { get; } = TimeSpan.FromSeconds(1);
public TimeSpan KeepAliveIdleThreshold { get; }
public TimeSpan PeriodicCheckInterval { get; }
public TimeSpan KeepAliveTimeout { get; }
public TimeSpan CreateSessionTimeout { get; }
}

internal class GetSessionResponse : ResponseWithResultBase<Session>, IDisposable
Expand Down Expand Up @@ -83,6 +91,21 @@ public SessionPool(Driver driver, SessionPoolConfig config)
}

public async Task<GetSessionResponse> GetSession()
{
const int maxAttempts = 100;

GetSessionResponse getSessionResponse = null!;
for (var attempt = 0; attempt < maxAttempts; attempt++)
{
getSessionResponse = await GetSessionAttempt();
if (getSessionResponse.Status.IsSuccess) return getSessionResponse;
}

_logger.LogError($"Failed to get session from pool or create it (attempts: {maxAttempts})");
return getSessionResponse;
}

private async Task<GetSessionResponse> GetSessionAttempt()
{
lock (_lock)
{
Expand Down

0 comments on commit f09a8fe

Please sign in to comment.