internal ThrottleCounter ProcessRequest(TimeSpan timeSpan, string id) { var throttleCounter = new ThrottleCounter() { Timestamp = DateTime.UtcNow, TotalRequests = 1 }; // serial reads and writes lock (ProcessLocker) { var entry = Repository.FirstOrDefault(id); if (entry.HasValue) { // entry has not expired if (entry.Value.Timestamp + timeSpan >= DateTime.UtcNow) { // increment request count var totalRequests = entry.Value.TotalRequests + 1; // deep copy throttleCounter = new ThrottleCounter { Timestamp = entry.Value.Timestamp, TotalRequests = totalRequests }; } } // stores: id (string) - timestamp (datetime) - total (long) Repository.Save(id, throttleCounter, timeSpan); } return(throttleCounter); }
public void Save(string id, ThrottleCounter throttleCounter, TimeSpan expirationTime) { var entry = new ThrottleCounterWrapper { ExpirationTime = expirationTime, Timestamp = throttleCounter.Timestamp, TotalRequests = throttleCounter.TotalRequests }; cache.AddOrUpdate(id, entry, (k, e) => entry); }
internal ThrottleLogEntry ComputeLogEntry(string requestId, RequestIdentity identity, ThrottleCounter throttleCounter, string rateLimitPeriod, long rateLimit, HttpRequestMessage request) { return new ThrottleLogEntry { ClientIp = identity.ClientIp, ClientKey = identity.ClientKey, Endpoint = identity.Endpoint, LogDate = DateTime.UtcNow, RateLimit = rateLimit, RateLimitPeriod = rateLimitPeriod, RequestId = requestId, StartPeriod = throttleCounter.Timestamp, TotalRequests = throttleCounter.TotalRequests, Request = request }; }
/// <summary> /// Insert or update /// </summary> public void Save(string id, ThrottleCounter throttleCounter, TimeSpan expirationTime) { if (memCache[id] != null) { memCache[id] = throttleCounter; } else { memCache.Add( id, throttleCounter, new CacheItemPolicy() { SlidingExpiration = expirationTime }); } }
internal ThrottleCounter ProcessRequest(RequestIdentity requestIdentity, TimeSpan timeSpan, RateLimitPeriod period, long rateLimit, long suspendTime, out string id) { var throttleCounter = new ThrottleCounter() { Timestamp = DateTime.UtcNow, TotalRequests = 1 }; id = ComputeThrottleKey(requestIdentity, period); TimeSpan suspendSpan = TimeSpan.FromSeconds(0); // serial reads and writes lock (ProcessLocker) { var entry = Repository.FirstOrDefault(id); if (entry.HasValue) { var timeStamp = entry.Value.Timestamp; if (entry.Value.TotalRequests >= rateLimit && suspendTime > 0) { timeSpan = GetSuspendSpanFromPeriod(period, timeSpan, suspendTime); } // entry has not expired if (entry.Value.Timestamp + timeSpan >= DateTime.UtcNow) { // increment request count var totalRequests = entry.Value.TotalRequests + 1; // deep copy throttleCounter = new ThrottleCounter { Timestamp = timeStamp, TotalRequests = totalRequests }; } } // stores: id (string) - timestamp (datetime) - total (long) Repository.Save(id, throttleCounter, timeSpan); } return(throttleCounter); }
/// <summary> /// Insert or update /// </summary> public void Save(string id, ThrottleCounter throttleCounter, TimeSpan expirationTime) { if (HttpContext.Current.Cache[id] != null) { HttpContext.Current.Cache[id] = throttleCounter; } else { HttpContext.Current.Cache.Add( id, throttleCounter, null, Cache.NoAbsoluteExpiration, expirationTime, CacheItemPriority.Low, null); } }
/// <summary> /// Insert or update /// </summary> /// <param name="id"> /// The id. /// </param> /// <param name="throttleCounter"> /// The throttle Counter. /// </param> /// <param name="expirationTime"> /// The expiration Time. /// </param> public void Save(string id, ThrottleCounter throttleCounter, TimeSpan expirationTime, string clientKey) { if (HttpContext.Current.Cache[id] != null) { HttpContext.Current.Cache[id] = throttleCounter; } else { HttpContext.Current.Cache.Add( id, throttleCounter, null, Cache.NoAbsoluteExpiration, expirationTime, CacheItemPriority.Low, null); } }
internal ThrottleLogEntry ComputeLogEntry(string requestId, RequestIdentity identity, ThrottleCounter throttleCounter, string rateLimitPeriod, long rateLimit, HttpRequestMessage request) { return(new ThrottleLogEntry { ClientIp = identity.ClientIp, ClientKey = identity.ClientKey, Endpoint = identity.Endpoint, LogDate = DateTime.UtcNow, RateLimit = rateLimit, RateLimitPeriod = rateLimitPeriod, RequestId = requestId, StartPeriod = throttleCounter.Timestamp, TotalRequests = throttleCounter.TotalRequests, Request = request }); }
private ThrottleCounter ProcessRequest(RequestIdentity requestIdentity, TimeSpan timeSpan, RateLimitPeriod period, out string id) { var throttleCounter = new ThrottleCounter() { Timestamp = DateTime.UtcNow, TotalRequests = 1 }; id = ComputeThrottleKey(requestIdentity, period); //serial reads and writes lock (_processLocker) { var entry = Repository.FirstOrDefault(id); if (entry.HasValue) { //entry has not expired if (entry.Value.Timestamp + timeSpan >= DateTime.UtcNow) { //increment request count var totalRequests = entry.Value.TotalRequests + 1; //deep copy throttleCounter = new ThrottleCounter { Timestamp = entry.Value.Timestamp, TotalRequests = totalRequests }; } } //stores: id (string) - timestamp (datetime) - total (long) Repository.Save(id, throttleCounter, timeSpan); } return throttleCounter; }
private ThrottleCounter ProcessRequest(ThrottlePolicy throttlePolicy, RequestIndentity throttleEntry, TimeSpan timeSpan, RateLimitPeriod period, out string id) { var throttleCounter = new ThrottleCounter(); throttleCounter.Timestamp = DateTime.UtcNow; throttleCounter.TotalRequests = 1; //computed request unique id from IP, client key, url and period id = "throttle"; if (throttlePolicy.IpThrottling) { if (throttlePolicy.IpWhitelist != null && throttlePolicy.IpWhitelist.Contains(throttleEntry.ClientIp)) { return throttleCounter; } id += "_" + throttleEntry.ClientIp; } if (throttlePolicy.ClientThrottling) { if (throttlePolicy.ClientWhitelist != null && throttlePolicy.ClientWhitelist.Contains(throttleEntry.ClientKey)) { return throttleCounter; } id += "_" + throttleEntry.ClientKey; } if (throttlePolicy.EndpointThrottling) { if (throttlePolicy.EndpointWhitelist != null && Policy.EndpointWhitelist.Any(x => throttleEntry.Endpoint.Contains(x))) { return throttleCounter; } id += "_" + throttleEntry.Endpoint; } id += "_" + period; //get the hash value of the computed id var hashId = ComputeHash(id); //serial reads and writes lock (_processLocker) { var entry = Repository.FirstOrDefault(hashId); if (entry.HasValue) { //entry has not expired if (entry.Value.Timestamp + timeSpan >= DateTime.UtcNow) { //increment request count var totalRequests = entry.Value.TotalRequests + 1; //deep copy throttleCounter = new ThrottleCounter { Timestamp = entry.Value.Timestamp, TotalRequests = totalRequests }; } } //stores: id (string) - timestamp (datetime) - total (long) Repository.Save(hashId, throttleCounter, timeSpan); } return throttleCounter; }