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

Implemented async methods #9

Merged
merged 3 commits into from Apr 14, 2015
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,8 @@
# statsd-csharp-client Changelog

## v1.4.0.0
* Added async support

## v1.3.0.0
* Added support for Calendargrams

Expand Down
42 changes: 42 additions & 0 deletions StatsdClient/AsyncLock.cs
@@ -0,0 +1,42 @@
using System;
using System.Threading;
using System.Threading.Tasks;

namespace StatsdClient
{
internal sealed class AsyncLock
{
private readonly SemaphoreSlim _semaphore = new SemaphoreSlim(1, 1);
private readonly Task<IDisposable> _releaser;

public AsyncLock()
{
_releaser = Task.FromResult((IDisposable)new Releaser(this));
}

public Task<IDisposable> LockAsync()
{
var wait = _semaphore.WaitAsync();
return wait.IsCompleted ?
_releaser :
wait.ContinueWith((_, state) => (IDisposable)state,
_releaser.Result, CancellationToken.None,
TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default);
}

private sealed class Releaser : IDisposable
{
private readonly AsyncLock _toRelease;

internal Releaser(AsyncLock toRelease)
{
_toRelease = toRelease;
}

public void Dispose()
{
_toRelease._semaphore.Release();
}
}
}
}
23 changes: 10 additions & 13 deletions StatsdClient/IOutputChannel.cs
@@ -1,18 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StatsdClient
{
/// <summary>
/// Contract for sending raw statds lines to the server
/// </summary>
public interface IOutputChannel
{
/// <summary>
/// Sends a line of stats data to the server.
/// Contract for sending raw statds lines to the server
/// </summary>
void Send(string line);
}
}
public interface IOutputChannel
{
/// <summary>
/// Sends a line of stats data to the server asynchronously.
/// </summary>
Task SendAsync(string line);
}
}
33 changes: 18 additions & 15 deletions StatsdClient/IStatsd.cs
@@ -1,4 +1,5 @@
using System;
using System.Threading.Tasks;

namespace StatsdClient
{
/// <summary>
Expand All @@ -9,45 +10,47 @@ public interface IStatsd
/// <summary>
/// Log a count for a metric
/// </summary>
void LogCount(string name, int count = 1);
Task LogCountAsync(string name, long count = 1);

/// <summary>
/// Log a gauge value
/// </summary>
void LogGauge(string name, int value);
Task LogGaugeAsync(string name, long value);

/// <summary>
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think this should break anyone.

Copy link
Author

Choose a reason for hiding this comment

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

I agree

/// Log a latency / Timing
/// </summary>
void LogTiming(string name, int milliseconds);
/// <summary>
/// Log a latency / Timing
/// </summary>
void LogTiming(string name, long milliseconds);
Task LogTimingAsync(string name, long milliseconds);

/// <summary>
/// Log the number of unique occurrances of something
/// </summary>
/// <param name="name"></param>
/// <param name="value"></param>
void LogSet(string name, int value);
Task LogSetAsync(string name, long value);

/// <summary>
/// Log a calendargram metric
/// Log a calendargram metric
/// </summary>
/// <param name="name">The metric namespace</param>
/// <param name="value">The unique value to be counted in the time period</param>
/// <param name="period">The time period, can be one of h,d,dow,w,m</param>
void LogCalendargram(string name, string value, string period);
Task LogCalendargramAsync(string name, string value, string period);

/// <summary>
/// Log a calendargram metric
/// Log a calendargram metric
/// </summary>
/// <param name="name">The metric namespace</param>
/// <param name="value">The unique value to be counted in the time period</param>
/// <param name="period">The time period, can be one of h,d,dow,w,m</param>
void LogCalendargram(string name, int value, string period);
Task LogCalendargramAsync(string name, long value, string period);

/// <summary>
/// Log a raw metric that will not get aggregated on the server.
/// </summary>
/// <param name="name">The metric name.</param>
/// <param name="value">The metric value.</param>
/// <param name="epoch">(optional) The epoch timestamp. Leave this blank to have the server assign an epoch for you.</param>
void LogRaw(string name, int value, long? epoch = null);
Task LogRawAsync(string name, long value, long? epoch = null);
}
}
}
16 changes: 6 additions & 10 deletions StatsdClient/NullOutputChannel.cs
@@ -1,15 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StatsdClient
{
internal sealed class NullOutputChannel : IOutputChannel
{
public void Send(string line)
internal sealed class NullOutputChannel : IOutputChannel
{
// noop
public async Task SendAsync(string line)
{
}
}
}
}
}
10 changes: 10 additions & 0 deletions StatsdClient/OutputChannelExtensions.cs
@@ -0,0 +1,10 @@
namespace StatsdClient
{
public static class OutputChannelExtensions
{
public static void Send(this IOutputChannel outputChannel, string line)
{
outputChannel.SendAsync(line).Wait();
}
}
}