public void AddAndCleanup(string key, ThrottlingCacheEntry entry, ICoreLogger logger)
        {
            // in a high concurrency scenario, pick the most fresh entry
            _cache.AddOrUpdate(
                key,
                entry,
                (_, oldEntry) => entry.CreationTime > oldEntry.CreationTime ? entry : oldEntry);

            CleanCache(logger);
        }
        public void RecordException(AuthenticationRequestParameters requestParams, IReadOnlyDictionary <string, string> bodyParams, MsalServiceException ex)
        {
            if (ex is MsalUiRequiredException && IsRequestSupported(requestParams))
            {
                var logger = requestParams.RequestContext.Logger;

                logger.Info($"[Throttling] MsalUiRequiredException encountered - " +
                            $"throttling for {s_uiRequiredExpiration.TotalSeconds} seconds");

                var thumbprint = GetRequestStrictThumbprint(bodyParams,
                                                            requestParams.AuthorityInfo.CanonicalAuthority,
                                                            requestParams.RequestContext.ServiceBundle.PlatformProxy.CryptographyManager);
                var entry = new ThrottlingCacheEntry(ex, s_uiRequiredExpiration);
                ThrottlingCache.AddAndCleanup(thumbprint, entry, logger);
            }
        }
        public void RecordException(
            AuthenticationRequestParameters requestParams,
            IReadOnlyDictionary <string, string> bodyParams,
            MsalServiceException ex)
        {
            if (TryGetRetryAfterValue(ex.Headers, out TimeSpan retryAfterTimespan))
            {
                retryAfterTimespan = GetSafeValue(retryAfterTimespan);

                var logger = requestParams.RequestContext.Logger;
                logger.Info($"[Throttling] Retry-After header detected, " +
                            $"value: {retryAfterTimespan.TotalSeconds} seconds");

                string thumbprint = ThrottleCommon.GetRequestStrictThumbprint(
                    bodyParams,
                    requestParams.AuthorityInfo.CanonicalAuthority,
                    requestParams.Account?.HomeAccountId?.Identifier);
                var entry = new ThrottlingCacheEntry(ex, retryAfterTimespan);

                ThrottlingCache.AddAndCleanup(thumbprint, entry, logger);
            }
        }
Ejemplo n.º 4
0
        public void RecordException(
            AuthenticationRequestParameters requestParams,
            IReadOnlyDictionary <string, string> bodyParams,
            MsalServiceException ex)
        {
            var logger = requestParams.RequestContext.Logger;

            if (ThrottleCommon.IsRetryAfterAndHttpStatusThrottlingSupported(requestParams) &&
                (ex.StatusCode == 429 || (ex.StatusCode >= 500 && ex.StatusCode < 600)) &&
                // if a retry-after header is present, another provider will take care of this
                !RetryAfterProvider.TryGetRetryAfterValue(ex.Headers, out _))
            {
                logger.Info($"[Throttling] Http status code {ex.StatusCode} encountered - " +
                            $"throttling for {s_throttleDuration.TotalSeconds} seconds");

                var thumbprint = ThrottleCommon.GetRequestStrictThumbprint(bodyParams,
                                                                           requestParams.AuthorityInfo.CanonicalAuthority,
                                                                           requestParams.Account?.HomeAccountId?.Identifier);
                var entry = new ThrottlingCacheEntry(ex, s_throttleDuration);
                ThrottlingCache.AddAndCleanup(thumbprint, entry, logger);
            }
        }