using Microsoft.Extensions.Options; using MongoDB.Driver; namespace StellaOps.Concelier.Storage.Mongo; public interface IMongoSessionProvider { Task StartSessionAsync(CancellationToken cancellationToken = default); } internal sealed class MongoSessionProvider : IMongoSessionProvider { private readonly IMongoClient _client; private readonly MongoStorageOptions _options; public MongoSessionProvider(IMongoClient client, IOptions options) { _client = client ?? throw new ArgumentNullException(nameof(client)); _options = options?.Value ?? throw new ArgumentNullException(nameof(options)); } public Task 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); } }