BrakePedal.Memcached is an extension to the BrakePedal throttling and rate-limiting library. BrakePedal.Memcached adds support for Memcached backed throttling.
The core library provides the following features:
- Throttling: limit
X attemptsoverY time period. - Locking: after
X attemptsoverY time periodblock future attempts forZ time period.
Current Version: 1.0.0
Target Framework: .NET 4.5 and up.
BrakePedal.Memcachedcontains an implementation of a Memcached throttle repository which uses EnyimMemcached
Dependencies:
-
Begin with a Memcached client and repository:
MemcachedClient client = new MemcachedClient(); var repository = new MemcachedThrottleRepository(client); -
Configure a policy:
var loginPolicy = new ThrottlePolicy(repository) { Name = "LoginAttempts", Prefixes = new[] {"login:attempts"}, Limiters = new Limiter[] { new Limiter().Limit(3).Over(TimeSpan.FromSeconds(10)) } };
Once the policies have been defined they can be used as follows:
-
Create a key that can unique identify the requester.
var key = new SimpleThrottleKey("username"); -
Check the policy:
var check = loginPolicy.Check(key); // NOTE: by default, calling the check method will increment the counter. // If you want to check the status of a policy but not increment the counter // pass in false to the increment parameter as follows. // loginPolicy.Check(key, increment = false); if (check.IsThrottled) { throw new Exception($"Requests throttled. Maximum allowed { check.Limiter.Count } per { check.Limiter.Period }."); }
For more information consult the documentation for BrakePedal and EnyimMemcached.