Skip to content

Instantly share code, notes, and snippets.

@jpierson
Created December 9, 2016 03:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpierson/87148fa96f9443038123e727415213d8 to your computer and use it in GitHub Desktop.
Save jpierson/87148fa96f9443038123e727415213d8 to your computer and use it in GitHub Desktop.
using Akka.Actor;
using System.Collections.Generic;
namespace Actors
{
public class PreformanceCounterActor : ReceiveActor
{
private Dictionary<string, long> _counters;
public PreformanceCounterActor()
{
_counters = new Dictionary<string, long>();
Receive<string>(counterName => SetCounter(counterName));
Receive<SetCounter>(setCounter => SetCounter(setCounter.Name));
Receive<GetCounter>(getCounter => Sender.Tell(GetCounter(getCounter.Name)));
}
public void SetCounter(string counterName)
{
long counterValue;
if (!_counters.TryGetValue(counterName, out counterValue))
{
counterValue = 0;
}
_counters[counterName] = ++counterValue;
}
public long GetCounter(string counterName)
{
long counterValue;
return _counters.TryGetValue(counterName, out counterValue) ?
counterValue:
0;
}
}
public class SetCounter
{
public string Name { get; private set; }
public SetCounter(string counterName)
{
Name = counterName;
}
}
public class GetCounter
{
public string Name { get; private set; }
public GetCounter(string counterName)
{
Name = counterName;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment