Created
December 9, 2016 03:21
-
-
Save jpierson/87148fa96f9443038123e727415213d8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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