Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Default Keep Alive #107

Merged
merged 5 commits into from
Feb 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions src/EventStore.Client/ChannelFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,27 @@ HttpMessageHandler CreateHandler() {
return settings.CreateHttpMessageHandler.Invoke();
}

var handler = new SocketsHttpHandler();
if (settings.ConnectivitySettings.KeepAlive.HasValue) {
handler.KeepAlivePingDelay = settings.ConnectivitySettings.KeepAlive.Value;
}

return handler;
return new SocketsHttpHandler {
KeepAlivePingDelay = settings.ConnectivitySettings.KeepAliveInterval,
KeepAlivePingTimeout = settings.ConnectivitySettings.KeepAliveTimeout
};
}
#else
return new Channel(address.Host, address.Port, settings.ChannelCredentials ?? ChannelCredentials.Insecure,
GetChannelOptions());

IEnumerable<ChannelOption> GetChannelOptions() {
if (settings.ConnectivitySettings.KeepAlive.HasValue) {
yield return new ChannelOption("grpc.keepalive_time_ms",
(int)settings.ConnectivitySettings.KeepAlive.Value.TotalMilliseconds);
}
yield return new ChannelOption("grpc.keepalive_time_ms",
GetValue((int)settings.ConnectivitySettings.KeepAliveInterval.TotalMilliseconds));

yield return new ChannelOption("grpc.keepalive_timeout_ms",
GetValue((int)settings.ConnectivitySettings.KeepAliveTimeout.TotalMilliseconds));
}

static int GetValue(int value) => value switch {
{ } v when v < 0 => int.MaxValue,
_ => value
};
#endif
}
}
Expand Down
11 changes: 9 additions & 2 deletions src/EventStore.Client/EventStoreClientConnectivitySettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,12 @@ public class EventStoreClientConnectivitySettings {
/// <summary>
/// The optional amount of time to wait after which a keepalive ping is sent on the transport.
/// </summary>
public TimeSpan? KeepAlive { get; set; }
public TimeSpan KeepAliveInterval { get; set; } = TimeSpan.FromSeconds(10);

/// <summary>
/// The optional amount of time to wait after which a sent keepalive ping is considered timed out.
/// </summary>
public TimeSpan KeepAliveTimeout { get; set; } = TimeSpan.FromSeconds(10);

/// <summary>
/// True if pointing to a single EventStoreDB node.
Expand All @@ -86,7 +91,9 @@ public class EventStoreClientConnectivitySettings {
MaxDiscoverAttempts = 10,
GossipTimeout = TimeSpan.FromSeconds(5),
DiscoveryInterval = TimeSpan.FromMilliseconds(100),
NodePreference = NodePreference.Leader
NodePreference = NodePreference.Leader,
KeepAliveInterval = TimeSpan.FromSeconds(10),
KeepAliveTimeout = TimeSpan.FromSeconds(10),
};
}
}
32 changes: 23 additions & 9 deletions src/EventStore.Client/EventStoreClientSettings.ConnectionString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading;
#if !GRPC_CORE
using System.Net.Http;
#endif
Expand Down Expand Up @@ -35,7 +36,8 @@ private static class ConnectionStringParser {
private const string TlsVerifyCert = nameof(TlsVerifyCert);
private const string OperationTimeout = nameof(OperationTimeout);
private const string ThrowOnAppendFailure = nameof(ThrowOnAppendFailure);
private const string KeepAlive = nameof(KeepAlive);
private const string KeepAliveInterval = nameof(KeepAliveInterval);
private const string KeepAliveTimeout = nameof(KeepAliveTimeout);

private const string UriSchemeDiscover = "esdb+discover";

Expand All @@ -54,7 +56,8 @@ private static class ConnectionStringParser {
{TlsVerifyCert, typeof(bool)},
{OperationTimeout, typeof(int)},
{ThrowOnAppendFailure, typeof(bool)},
{KeepAlive, typeof(int)}
{KeepAliveInterval, typeof(int)},
{KeepAliveTimeout, typeof(int)},
};

public static EventStoreClientSettings Parse(string connectionString) {
Expand Down Expand Up @@ -166,8 +169,20 @@ private static EventStoreClientSettings CreateSettings(string scheme, (string us
if (typedOptions.TryGetValue(ThrowOnAppendFailure, out object throwOnAppendFailure))
settings.OperationOptions.ThrowOnAppendFailure = (bool)throwOnAppendFailure;

if (typedOptions.TryGetValue(KeepAlive, out var keepAliveMs)) {
settings.ConnectivitySettings.KeepAlive = TimeSpan.FromMilliseconds((int)keepAliveMs);
if (typedOptions.TryGetValue(KeepAliveInterval, out var keepAliveIntervalMs)) {
settings.ConnectivitySettings.KeepAliveInterval = keepAliveIntervalMs switch {
int value when value == -1 => Timeout.InfiniteTimeSpan,
int value when value >= 0 => TimeSpan.FromMilliseconds(value),
_ => throw new InvalidSettingException($"Invalid KeepAliveInterval: {keepAliveIntervalMs}")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SUGGESTION: Minor, but in RFC is a bit different format: $"Invalid keepAliveInterval {keepAliveInterval}. Please provide a positive integer, or -1 to disable.".
Same comment for code below https://github.com/EventStore/EventStore-Client-Dotnet/pull/107/files#diff-29e93df819e6a1e3940a362a23ca7857372b6098e23b26c18f4f61282c6319bfR184.

};
}

if (typedOptions.TryGetValue(KeepAliveTimeout, out var keepAliveTimeoutMs)) {
settings.ConnectivitySettings.KeepAliveTimeout = keepAliveTimeoutMs switch {
int value when value == -1 => Timeout.InfiniteTimeSpan,
int value when value >= 0 => TimeSpan.FromMilliseconds(value),
_ => throw new InvalidSettingException($"Invalid KeepAliveTimeout: {keepAliveTimeoutMs}")
};
}

connSettings.Insecure = !useTls;
Expand All @@ -184,16 +199,15 @@ private static EventStoreClientSettings CreateSettings(string scheme, (string us

#if !GRPC_CORE
settings.CreateHttpMessageHandler = () => {
var handler = new SocketsHttpHandler();
var handler = new SocketsHttpHandler {
KeepAlivePingDelay = settings.ConnectivitySettings.KeepAliveInterval,
KeepAlivePingTimeout = settings.ConnectivitySettings.KeepAliveTimeout
};

if (typedOptions.TryGetValue(TlsVerifyCert, out var tlsVerifyCert) && !(bool)tlsVerifyCert) {
handler.SslOptions.RemoteCertificateValidationCallback = delegate { return true; };
}

if (settings.ConnectivitySettings.KeepAlive.HasValue) {
handler.KeepAlivePingDelay = settings.ConnectivitySettings.KeepAlive.Value;
}

return handler;
};
#endif
Expand Down
10 changes: 9 additions & 1 deletion src/EventStore.Client/MultiChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@
using System.Threading;
using System.Threading.Tasks;
using Grpc.Core;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;

#nullable enable
namespace EventStore.Client {
internal class MultiChannel : IDisposable, IAsyncDisposable {
private readonly EventStoreClientSettings _settings;
private readonly IEndpointDiscoverer _endpointDiscoverer;
private readonly ConcurrentDictionary<EndPoint, ChannelBase> _channels;
private readonly ILogger<MultiChannel> _log;

private EndPoint? _current;

private int _disposed;

public MultiChannel(EventStoreClientSettings settings) {
Expand All @@ -22,6 +24,12 @@ public MultiChannel(EventStoreClientSettings settings) {
? (IEndpointDiscoverer)new SingleNodeEndpointDiscoverer(settings.ConnectivitySettings.Address)
: new GossipBasedEndpointDiscoverer(settings.ConnectivitySettings, new GrpcGossipClient(settings));
_channels = new ConcurrentDictionary<EndPoint, ChannelBase>();
_log = settings.LoggerFactory?.CreateLogger<MultiChannel>() ?? new NullLogger<MultiChannel>();

if (settings.ConnectivitySettings.KeepAliveInterval < TimeSpan.FromSeconds(10)) {
_log.LogWarning("Specified KeepAliveInterval of {interval} is less than recommended 10_000 ms.",
settings.ConnectivitySettings.KeepAliveInterval);
}
}

public void SetEndPoint(EndPoint value) => _current = value;
Expand Down
1 change: 1 addition & 0 deletions test/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<Project>
<Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '$(MSBuildThisFileDirectory)../'))" />
<ItemGroup>
<PackageReference Include="AutoFixture.Xunit2" Version="4.15.0" />
<PackageReference Include="Ductus.FluentDocker" Version="2.7.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.3"/>
<PackageReference Include="Polly" Version="7.2.1" />
Expand Down
Loading