35 lines
1.2 KiB
C#
35 lines
1.2 KiB
C#
using Microsoft.Extensions.Options;
|
|
using MongoDB.Driver;
|
|
|
|
namespace StellaOps.Concelier.Storage.Mongo;
|
|
|
|
public interface IMongoSessionProvider
|
|
{
|
|
Task<IClientSessionHandle> StartSessionAsync(CancellationToken cancellationToken = default);
|
|
}
|
|
|
|
internal sealed class MongoSessionProvider : IMongoSessionProvider
|
|
{
|
|
private readonly IMongoClient _client;
|
|
private readonly MongoStorageOptions _options;
|
|
|
|
public MongoSessionProvider(IMongoClient client, IOptions<MongoStorageOptions> options)
|
|
{
|
|
_client = client ?? throw new ArgumentNullException(nameof(client));
|
|
_options = options?.Value ?? throw new ArgumentNullException(nameof(options));
|
|
}
|
|
|
|
public Task<IClientSessionHandle> StartSessionAsync(CancellationToken cancellationToken = default)
|
|
{
|
|
var sessionOptions = new ClientSessionOptions
|
|
{
|
|
DefaultTransactionOptions = new TransactionOptions(
|
|
readPreference: ReadPreference.Primary,
|
|
readConcern: ReadConcern.Majority,
|
|
writeConcern: WriteConcern.WMajority.With(wTimeout: _options.CommandTimeout))
|
|
};
|
|
|
|
return _client.StartSessionAsync(sessionOptions, cancellationToken);
|
|
}
|
|
}
|