예제 #1
0
        private async Task ApplyChangesParallel(OneDriveModel.OneDriveChangeCollection changes, string accessToken, int maxParallelCount, bool countSuccess)
        {
            List <Task> tasks         = new List <Task>();
            string      wwwroot       = _environment.WebRootPath;
            bool        failureExceed = false;

            using (var sem = new SemaphoreSlim(initialCount: maxParallelCount, maxCount: maxParallelCount))
            {
                foreach (OneDriveModel.OneDriveChange change in changes)
                {
                    tasks.Add(Task.Run(async() =>
                    {
                        try
                        {
                            await sem.WaitAsync();
                            if (failureExceed)
                            {
                                return;
                            }

                            bool applied = await ProcessChangeWithRetry(change, accessToken, wwwroot);
                            if (applied)
                            {
                                if (countSuccess)
                                {
                                    Interlocked.Increment(ref _successCount);
                                }
                            }
                            else
                            {
                                if (Interlocked.Increment(ref _failedCount) > MaxFailures)
                                {
                                    failureExceed = true;
                                }
                            }
                        }
                        finally
                        {
                            sem.Release();
                        }
                    }));
                }

                await Task.WhenAll(tasks);
            }
        }
예제 #2
0
        private async Task ApplyChangesParallel(OneDriveModel.OneDriveChangeCollection changes, string accessToken, int maxParallelCount)
        {
            List <Task> tasks   = new List <Task>();
            string      wwwroot = _environment.WebRootPath;

            using (var sem = new SemaphoreSlim(initialCount: maxParallelCount, maxCount: maxParallelCount))
                using (var filesPerSecLimiter = new RateLimiter(MaxFilesPerSeconds, TimeSpan.FromSeconds(1)))
                {
                    foreach (OneDriveModel.OneDriveChange change in changes)
                    {
                        tasks.Add(Task.Run(async() =>
                        {
                            try
                            {
                                await sem.WaitAsync();
                                await filesPerSecLimiter.ThrottleAsync();
                                bool applied = await ProcessChangeWithRetry(change, accessToken, wwwroot);
                                if (applied)
                                {
                                    Interlocked.Increment(ref _successCount);
                                }
                                else
                                {
                                    Interlocked.Increment(ref _failedCount);
                                }
                            }
                            finally
                            {
                                sem.Release();
                            }
                        }));
                    }

                    await Task.WhenAll(tasks);
                }
        }
예제 #3
0
 internal ChangesResult()
 {
     FileChanges      = new OneDriveModel.OneDriveChangeCollection();
     DirectoryChanges = new OneDriveModel.OneDriveChangeCollection();
     DeletionChanges  = new OneDriveModel.OneDriveChangeCollection();
 }