Ejemplo n.º 1
0
 internal DocumentLibrary()
 {
     this.allDocuments         = new List <Tuple <CdmFolderDefinition, CdmDocumentDefinition> >();
     this.pathLookup           = new Dictionary <string, Tuple <CdmFolderDefinition, CdmDocumentDefinition> >();
     this.docsNotLoaded        = new Dictionary <string, byte>();
     this.docsCurrentlyLoading = new Dictionary <string, byte>();
     this.docsNotFound         = new Dictionary <string, byte>();
     this.docsNotIndexed       = new Dictionary <CdmDocumentDefinition, byte>();
     this.concurrentReadLock   = new ConcurrentSemaphore();
 }
Ejemplo n.º 2
0
 internal DocumentLibrary(CdmCorpusDefinition corpus)
 {
     this.allDocuments          = new List <Tuple <CdmFolderDefinition, CdmDocumentDefinition> >();
     this.concurrentReadLock    = new ConcurrentSemaphore();
     this.corpus                = corpus;
     this.docsCurrentlyIndexing = new HashSet <CdmDocumentDefinition>();
     this.docsCurrentlyLoading  = new HashSet <string>();
     this.docsNotFound          = new HashSet <string>();
     this.pathLookup            = new Dictionary <string, Tuple <CdmFolderDefinition, CdmDocumentDefinition> >();
 }
Ejemplo n.º 3
0
        public override async void OnActionExecuting(ActionExecutingContext context)
        {
            if (Key.IsNullOrEmptyOrWhiteSpace())
            {
                throw new ArgumentNullException(nameof(Key));
            }

            if (MaximumConnectionsCountBeforeThrottling < 1)
            {
                throw new ArgumentOutOfRangeException(nameof(MaximumConnectionsCountBeforeThrottling));
            }

            if (ThrottlingPeriod <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(ThrottlingPeriod));
            }

            var throttlingTypeKeyPart = ThrottlingBy.ToString();

            switch (ThrottlingBy)
            {
            case ThrottlingType.ByIp:
                throttlingTypeKeyPart += $":{context.HttpContext.Connection.RemoteIpAddress}";

                break;

            case ThrottlingType.ByUser:
                if (context.HttpContext.User.Identity.IsAuthenticated)
                {
                    throttlingTypeKeyPart += $":{context.HttpContext.User.Identity.Name}";
                }
                else
                {
                    return;
                }

                break;
            }

            var key = $"{nameof(ThrottleActionFilterAttribute)}:{Key}:{throttlingTypeKeyPart}";

            var memoryCache = context.HttpContext.RequestServices.GetService <IMemoryCache>();

            var semaphore = await ConcurrentSemaphore.WaitAsync(key);

            try
            {
                if (!memoryCache.TryGetValue(key, out Tuple <int, DateTimeOffset> throttleData))
                {
                    var expirationTime = DateTimeOffset.Now.Add(TimeSpan.FromMilliseconds(ThrottlingPeriod));

                    memoryCache.Set(key,
                                    new Tuple <int, DateTimeOffset>(MaximumConnectionsCountBeforeThrottling, expirationTime),
                                    expirationTime);
                }
                else
                {
                    if (throttleData.Item1 <= 1)
                    {
                        context.Result = new ConflictResult();
                    }
                    else
                    {
                        memoryCache.Set(key, new Tuple <int, DateTimeOffset>(throttleData.Item1 - 1, throttleData.Item2),
                                        throttleData.Item2);
                    }
                }
            }
            finally
            {
                semaphore.Dispose();
            }

            base.OnActionExecuting(context);
        }