Example #1
0
        async Task <List <ArloRegistration> > _getRegistrationLinks(EventRegistrationList links, bool forceRefresh)
        {
            var registrants = await LoadLinkEntities <ArloRegistration>(links, Constants.Cache.AllRegistrationList, Constants.Rel.Registration, forceRefresh);

            var taskList = new List <Task>();

            foreach (var registrant in registrants)
            {
                var r2 = registrant;

                async Task <bool> LocalLoad()
                {
                    var link = r2.FindLink(Constants.Rel.RelatedContact);

                    if (link == null)
                    {
                        return(false);
                    }

                    var c = await LoadEntity <ArloContact>(link, link);

                    r2.Contact = c;
                    return(true);
                }

                taskList.Add(TaskThrottler.Get("LoadRegistrants", 15).Throttle(LocalLoad));
            }

            await Task.WhenAll(taskList);

            return(registrants);
        }
Example #2
0
        public async Task TestAsyncThrottlingReturns()
        {
            List <Task <SomeEntity> > tasks = new List <Task <SomeEntity> >();

            for (var i = 0; i < 500; i++)
            {
                var t = TaskThrottler.Get("Test", 50).Throttle(_getAnEntity);
                tasks.Add(t);
                Debug.WriteLine("Added: {0}", i);
            }

            await Task.WhenAll(tasks);
        }
Example #3
0
        public async Task TestAsyncNoThrottlingNoReturns()
        {
            var         throttler = TaskThrottler.Get("Test", 500);
            List <Task> tasks     = new List <Task>();

            for (var i = 0; i < 5000; i++)
            {
                tasks.Add(_doSomeWork());

                Debug.WriteLine("Added: {0}", i);
            }

            await Task.WhenAll(tasks);
        }
Example #4
0
        public async Task <List <T> > LoadLinkEntities <T>(ListOfLinks links, string key, string rel, bool forceRefresh)
            where T : class, new()
        {
            if (links == null)
            {
                return(null);
            }
            key += _getHashLinks(links);

            var cacheResult = await GetEntity <List <T> >(key);

            if (cacheResult != null && !forceRefresh)
            {
                return(cacheResult);
            }

            var repo = Scope.Resolve <IGenericWebRepo <T> >();

            var resultTasks = new List <Task <T> >();

            if (links.Link == null)
            {
                return(null);
            }

            foreach (var l in links.Link)
            {
                if (l.Rel != rel)
                {
                    continue;
                }

                var lClosure = l;

                lClosure.Href = lClosure.Href.Replace("sessionregistrations.", "sessionregistrations");//hardcode fix a bug in the arlo api

                resultTasks.Add(TaskThrottler.Get("GetLinkEntities", 15).Throttle(() => repo.Get(lClosure.Href)));
            }

            var resultFromTasks = await Task.WhenAll(resultTasks);

            var listResult = resultFromTasks.ToList();

            await SetEntity(key, listResult);

            return(listResult);
        }
Example #5
0
        public async Task TestAsyncThrottlingNoReturnsSmaller()
        {
            var         throttler = TaskThrottler.Get("Test", 10);
            List <Task> tasks     = new List <Task>();



            for (var i = 0; i < 50; i++)
            {
                tasks.Add(throttler.Throttle(_doSomeWork));

                Debug.WriteLine("Added: {0}", i);
            }

            await Task.WhenAll(tasks);

            Assert.IsTrue(thiscount == 50);
        }
Example #6
0
        public async Task <List <T> > LoadLinkEntitiesFromList <T, TLinks>(List <TLinks> links, string key, string rel, bool forceRefresh)
            where T : class, new()
            where TLinks : ListOfLinks
        {
            if (links == null)
            {
                return(null);
            }

            key += _getHashLinksLinks(links);

            var cacheResult = await GetEntity <List <T> >(key);

            if (cacheResult != null && !forceRefresh)
            {
                return(cacheResult);
            }

            var resultTasks = new List <Task <List <T> > >();

            foreach (var lol in links)
            {
                if (lol == null)
                {
                    continue;
                }
                resultTasks.Add(TaskThrottler.Get("LoadLinkEntitiesFromList", 15).Throttle(() => LoadLinkEntities <T>(lol, Guid.NewGuid().ToString(), rel, forceRefresh)));
            }

            var resultFromTasks = await Task.WhenAll(resultTasks);

            var listResult = resultFromTasks.ToList();

            var result = new List <T>();

            foreach (var l in listResult)
            {
                result.AddRange(l);
            }

            await SetEntity(key, result);

            return(result);
        }
Example #7
0
        public async Task TestAsyncThrottlingUsingDisposeMethod()
        {
            //this is a very poor method for loops, good for throttling across many threads.
            for (var i = 0; i < 500; i++)
            {
                var i2 = i;

                Task.Run(async() =>
                {
                    using (var l = await TaskThrottler.Get("Test", 2).LockAsync())
                    {
                        Debug.WriteLine("Added: {0}", i2);
                        await _doSomeWork();
                    }
                });
            }


            await Task.Delay(50000);
        }
Example #8
0
        public async Task <List <ArloSession> > GetEventSessions(List <Link> sessionLinks, bool forceRefresh)
        {
            if (sessionLinks == null)
            {
                return(new List <ArloSession>());
            }

            var resultTasks = new List <Task <ArloSession> >();

            foreach (var item in sessionLinks.Where(_ => _.Rel == Constants.Rel.EventSession))
            {
                var i2 = item;
                var t  = TaskThrottler.Get("GetSessonLinks", 30).Throttle(() => GetEventSession(i2.Href, forceRefresh));
                resultTasks.Add(t);
            }

            var resultFromTasks = await Task.WhenAll(resultTasks);

            var listResult = resultFromTasks.ToList();

            return(listResult);
        }