Skip to content

Commit

Permalink
Enable keep alives (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
asimmon committed May 27, 2024
1 parent b9f840c commit 9f7650e
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/Workleap.Extensions.Mongo/MongoClientProvider.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System.Collections.Concurrent;
using System.Net.Sockets;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using MongoDB.Driver;
using MongoDB.Driver.Core.Configuration;

namespace Workleap.Extensions.Mongo;

Expand Down Expand Up @@ -91,12 +93,19 @@ private IMongoClient CreateMongoClient(string clientName)
settings.ClusterConfigurator = builder =>
{
userDefinedClusterConfiguration?.Invoke(builder);
builder.ConfigureTcp(EnableKeepAlives);
builder.Subscribe(new OrderedAggregatorEventSubscriber(eventSubscribers));
};

return new MongoClient(settings);
}

private static TcpStreamSettings EnableKeepAlives(TcpStreamSettings tcpStreamSettings)
{
return tcpStreamSettings.With(socketConfigurator: (Action<Socket>)SocketConfigurator);
static void SocketConfigurator(Socket s) => s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
}

public void Dispose()
{
foreach (var disposable in this._disposableDependencies)
Expand Down

0 comments on commit 9f7650e

Please sign in to comment.