/// <inheritdoc /> /// <summary> /// </summary> /// <param name="actionContext"></param> public override void OnActionExecuting(HttpActionContext actionContext) { if (actionContext.Request.Method == HttpMethod.Get) { //5min降级时间 var b = ThrottlingHelper.ThrottlingDegrade(ConfigHelper.GetIntValue("Throttling"), 300); //当开启动态降级 if (b) { var controllerName = actionContext.ControllerContext.ControllerDescriptor.ControllerName; var actionName = actionContext.ActionDescriptor.ActionName; var item = GetKey(controllerName, actionName, actionContext.ActionArguments); var bRes = MemoryCacheHelper.Get(item); if (bRes != null) { var bResult = (ResponseResult)bRes; if (DateTimeOffset.Now.DateTime <= bResult.Expires.DateTime) { var res = actionContext.Request.CreateResponse(); res.Headers.CacheControl = new CacheControlHeaderValue { MaxAge = TimeSpan.FromSeconds(180), Public = true, NoCache = false }; res.Content = new StringContent(bResult.ResponseContent, Encoding.UTF8, "application/json"); actionContext.Response = res; } } } } base.OnActionExecuting(actionContext); }
internal MainWindow(INavigator navigator, Settings settings, ProcessService processService, bool isClosedAfterStartingProcess) { Ensure.NotNull(navigator, "navigator"); Ensure.NotNull(settings, "settings"); Ensure.NotNull(processService, "processService"); this.navigator = navigator; this.settings = settings; this.processService = processService; this.isClosedAfterStartingProcess = isClosedAfterStartingProcess; InitializeComponent(); AccessKey.AddPressedHandler(this, OnAccessKeyPressed); AccessKey.AddPressingHandler(this, OnAccessKeyPressing); EventManager.FilePinned += OnFilePinned; DispatcherHelper = new DispatcherHelper(Dispatcher); fileThrottler = new ThrottlingHelper(DispatcherHelper, () => lvwFiles.SelectedIndex = 0, 0); applicationThrottler = new ThrottlingHelper(DispatcherHelper, () => lvwApplications.SelectedIndex = 0, 0); }
public ZendeskRestApi(ZendeskConfig zendeskConfig) { this.Config = zendeskConfig; ContractResolver = new DefaultContractResolver { NamingStrategy = new SnakeCaseNamingStrategy() }; JsonSerializerSettings = new JsonSerializerSettings() { ContractResolver = ContractResolver }; var handler = new HttpClientHandler() { AllowAutoRedirect = false, }; ThrottlingHelper = new ThrottlingHelper(699, TimeSpan.FromMinutes(1)); var byteArray = Encoding.ASCII.GetBytes(Config.Login + ':' + Config.Password); HttpClient = new HttpClient(handler); HttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray)); }
/// <summary> /// To apply policy, check limit & restrict user. /// </summary> /// <param name="actionContext">Request context</param> /// <param name="policyList">List of throttle policies</param> /// <param name="IdentityKey">Key like emailId, IP address etc.</param> private void ApplyAndCheckPolicy(HttpActionContext actionContext, List <Policy> policyList, string IdentityKey) { foreach (Policy item in policyList) { var key = ThrottlingHelper.ComputeCounterKey(IdentityKey, item); var allowExecute = false; item.PeriodTimespan = ThrottlingHelper.ConvertToTimeSpan(item.period); var throttleCounter = new ThrottleCounter() { Timestamp = DateTime.UtcNow, TotalRequests = 1 }; lock (ProcessLocker) { var entry = (ThrottleCounter?)HttpRuntime.Cache[key]; if (entry.HasValue) { // entry has not expired if (entry.Value.Timestamp + item.PeriodTimespan >= DateTime.UtcNow) { // increment request count var totalRequests = entry.Value.TotalRequests + 1; // deep copy throttleCounter = new ThrottleCounter { Timestamp = entry.Value.Timestamp, TotalRequests = totalRequests }; } } if (HttpRuntime.Cache[key] != null) { HttpRuntime.Cache[key] = throttleCounter; } else { HttpRuntime.Cache.Add( key, throttleCounter, null, Cache.NoAbsoluteExpiration, item.PeriodTimespan, CacheItemPriority.Low, null); allowExecute = true; } if (throttleCounter.TotalRequests > item.limit) { allowExecute = false; } else { allowExecute = true; } if (!allowExecute) { actionContext.Response = actionContext.Request.CreateResponse( (HttpStatusCode)429, string.Format("API calls quota exceeded!") ); actionContext.Response.Headers.Add("Retry-After", RetryAfterFrom(throttleCounter.Timestamp, item)); Trace.TraceError(string.Format(Environment.NewLine + "Request {0} from IpAddress:{1} clientKey:{2} has been throttled (blocked), quota {3}/{4} exceeded by {5}" , actionContext.Request.RequestUri.AbsoluteUri , requestIdentity.ipAddress , requestIdentity.clientKey , item.limit , item.period , throttleCounter.TotalRequests)); } } } }