private static void ApplicationPreSendRequestHeaders(object sender, EventArgs e) { if (!FailFastUserCache.FailFastEnabled) { return; } Logger.EnterFunction(ExTraceGlobals.FailureThrottlingTracer, "PowerShellFailureThrottlingModule.ApplicationPreSendRequestContent"); HttpApplication httpApplication = (HttpApplication)sender; HttpContext context = httpApplication.Context; if (context == null || context.Request == null || context.Response == null) { Logger.TraceDebug(ExTraceGlobals.FailureThrottlingTracer, "context == null || context.Request == null || context.Response == null", new object[0]); Logger.ExitFunction(ExTraceGlobals.FailureThrottlingTracer, "PowerShellFailureThrottlingModule.ApplicationPreSendRequestContent"); return; } string text = context.Response.Headers[FailFastModule.HeaderKeyToStoreUserToken]; Logger.TraceDebug(ExTraceGlobals.FailureThrottlingTracer, "Current UserToken is {0}.", new object[] { text }); context.Response.Headers.Remove(FailFastModule.HeaderKeyToStoreUserToken); Logger.TraceDebug(ExTraceGlobals.FailureThrottlingTracer, "Remove {0} from header.", new object[] { FailFastModule.HeaderKeyToStoreUserToken }); if (!PowerShellFailureThrottlingModule.failureThrottlingEnabled) { Logger.TraceDebug(ExTraceGlobals.FailureThrottlingTracer, "Failure throttling is disabled.", new object[0]); Logger.ExitFunction(ExTraceGlobals.FailureThrottlingTracer, "PowerShellFailureThrottlingModule.ApplicationPreSendRequestContent"); return; } if (!context.Request.IsAuthenticated) { Logger.TraceDebug(ExTraceGlobals.FailureThrottlingTracer, "Un-authenticated request.", new object[0]); Logger.ExitFunction(ExTraceGlobals.FailureThrottlingTracer, "PowerShellFailureThrottlingModule.ApplicationPreSendRequestContent"); return; } if (!string.IsNullOrEmpty(text) && FailureThrottling.CountBasedOnStatusCode(text, context.Response.StatusCode)) { HttpLogger.SafeAppendColumn(RpsCommonMetadata.ContributeToFailFast, "FailureThrottling", LoggerHelper.GetContributeToFailFastValue("User", text, "NewRequest", -1.0)); Logger.TraceDebug(ExTraceGlobals.FailureThrottlingTracer, "Add user {0} to fail-fast user cache.", new object[] { text }); FailFastUserCache.Instance.AddUserToCache(text, BlockedType.NewRequest, TimeSpan.Zero); } Logger.ExitFunction(ExTraceGlobals.FailureThrottlingTracer, "PowerShellFailureThrottlingModule.ApplicationPreSendRequestContent"); }
internal static bool CountBasedOnStatusCode(string userToken, int statusCode) { int num; if (statusCode != 200) { if (statusCode != 400 && statusCode != 500) { return(false); } num = 1; } else { num = -1; } FailureThrottling.FailureCounter counter = FailureThrottling.GetCounter(userToken); counter.AddDelta(num); Logger.TraceDebug(ExTraceGlobals.FailureThrottlingTracer, "User {0} counter changed to be {1}. Current status code is {2}.", new object[] { userToken, counter.Value, statusCode }); if (num == 1 && counter.Value > FailureThrottling.failureThrottlingLimit) { Logger.TraceDebug(ExTraceGlobals.FailureThrottlingTracer, "User {0} is OverBudget. Counter: {1}. FailureThrottlingLimit: {2}", new object[] { userToken, counter.Value, FailureThrottling.failureThrottlingLimit }); return(true); } return(false); }