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

Handle disconnection / reconnection #125

Merged
merged 4 commits into from
May 29, 2020
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
6 changes: 3 additions & 3 deletions src/StatsdClient/StatsSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ internal class StatsSender : IStatsSender, IDisposable
private static readonly TimeSpan NoBufferSpaceAvailableWait = TimeSpan.FromMilliseconds(10);
private readonly Socket _socket;
private readonly int _noBufferSpaceAvailableRetryCount;
private readonly EndPoint _endPoint;

private StatsSender(
StatsSenderTransportType transport,
Expand All @@ -20,6 +21,7 @@ private StatsSender(
TimeSpan? bufferFullBlockDuration)
{
TransportType = transport;
_endPoint = endPoint;
if (bufferFullBlockDuration.HasValue)
{
_noBufferSpaceAvailableRetryCount = (int)(bufferFullBlockDuration.Value.TotalMilliseconds
Expand Down Expand Up @@ -52,8 +54,6 @@ private StatsSender(
{
// It is not supported on Windows for Dgram with UDP.
}

_socket.Connect(endPoint);
}

public StatsSenderTransportType TransportType { get; }
Expand Down Expand Up @@ -90,7 +90,7 @@ public bool Send(byte[] buffer, int length)
{
try
{
_socket.Send(buffer, 0, length, SocketFlags.None);
_socket.SendTo(buffer, 0, length, SocketFlags.None, _endPoint);
return true;
}
catch (SocketException e) when (e.SocketErrorCode == SocketError.NoBufferSpaceAvailable)
Expand Down
49 changes: 32 additions & 17 deletions src/StatsdClient/Worker/AsynchronousWorker.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading.Tasks;

namespace StatsdClient.Worker
Expand Down Expand Up @@ -63,9 +64,16 @@ public void Dispose()
}

_terminate = true;
foreach (var worker in _workers)
try
{
worker.Wait();
foreach (var worker in _workers)
{
worker.Wait();
}
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
}

_workers.Clear();
Expand All @@ -77,29 +85,36 @@ private void Dequeue()

while (true)
{
if (_queue.TryDequeue(out var v))
{
_handler.OnNewValue(v);
waitDuration = MinWaitDuration;
}
else
try
{
if (_terminate)
if (_queue.TryDequeue(out var v))
{
_handler.OnShutdown();
return;
_handler.OnNewValue(v);
waitDuration = MinWaitDuration;
}

if (_handler.OnIdle())
else
{
_waiter.Wait(waitDuration);
waitDuration = waitDuration + waitDuration;
if (waitDuration > MaxWaitDuration)
if (_terminate)
{
_handler.OnShutdown();
return;
}

if (_handler.OnIdle())
{
waitDuration = MaxWaitDuration;
_waiter.Wait(waitDuration);
waitDuration = waitDuration + waitDuration;
if (waitDuration > MaxWaitDuration)
{
waitDuration = MaxWaitDuration;
}
}
}
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
}
}
}
}
Expand Down
68 changes: 68 additions & 0 deletions tests/StatsdClient.Tests/DogStatsdServiceReconnectionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using NUnit.Framework;
using StatsdClient;
using Tests.Utils;

namespace Tests
{
[TestFixture]
public class DogStatsdServiceReconnectionTests
{
[Test]
public void UDPReconnection()
{
CheckReconnection(new StatsdConfig
{
StatsdServerName = "127.0.0.1",
StatsdPort = 1234,
});
}

#if !OS_WINDOWS
[Test]
public void UDSReconnection()
{
using (var temporaryPath = new TemporaryPath())
{
CheckReconnection(new StatsdConfig
{
StatsdServerName = StatsdBuilder.UnixDomainSocketPrefix + temporaryPath.Path,
});
}
}
#endif

private static void CheckReconnection(StatsdConfig config)
{
SocketServer server = null;

try
{
server = new SocketServer(config);
using (var service = new DogStatsdService())
{
service.Configure(config);
service.Increment("test1");
Assert.AreEqual("test1:1|c", server.Stop().Single());
server.Dispose();

// Send a metric when the server is not running.
service.Increment("test2");
Task.Delay(TimeSpan.FromMilliseconds(500)).Wait();

// Restart the server
server = new SocketServer(config, removeUDSFileBeforeStarting: true);
service.Increment("test3");
service.Dispose();
Assert.AreEqual("test3:1|c", server.Stop().Last());
}
}
finally
{
server?.Dispose();
}
}
}
}
8 changes: 7 additions & 1 deletion tests/StatsdClient.Tests/utils/SocketServer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
Expand All @@ -18,7 +19,7 @@ internal class SocketServer : IDisposable

private volatile bool _shutdown = false;

public SocketServer(StatsdConfig config)
public SocketServer(StatsdConfig config, bool removeUDSFileBeforeStarting = false)
{
EndPoint endPoint;
int bufferSize;
Expand All @@ -27,6 +28,11 @@ public SocketServer(StatsdConfig config)
if (serverName.StartsWith(StatsdBuilder.UnixDomainSocketPrefix))
{
serverName = serverName.Substring(StatsdBuilder.UnixDomainSocketPrefix.Length);
if (removeUDSFileBeforeStarting)
{
File.Delete(serverName);
}

_server = new Socket(AddressFamily.Unix, SocketType.Dgram, ProtocolType.Unspecified);
endPoint = new UnixEndPoint(serverName);
bufferSize = config.StatsdMaxUnixDomainSocketPacketSize;
Expand Down