Esempio n. 1
0
        private async Task <T> TryWithFallback <T>(Func <IGitHubActor, GitHubCacheDetails, Task <T> > action, GitHubCacheDetails cacheOptions)
            where T : GitHubResponse
        {
            IGitHubActor actor = null;

            if (cacheOptions?.UserId != null)
            {
                if (!_actorMap.TryGetValue(cacheOptions.UserId, out actor))
                {
                    cacheOptions = null;
                }
            }

            while (true)
            {
                if (actor == null)
                {
                    actor = GetNextActor();
                }

                try {
                    var result = await action(actor, cacheOptions);

                    // Only retry authorization failures and rate limiting
                    switch (result.Status)
                    {
                    case HttpStatusCode.Forbidden:
                    case HttpStatusCode.Unauthorized:
                        // Retry with someone else.
                        Remove(actor.GetPrimaryKeyLong());
                        actor = null;
                        break;

                    default:
                        return(result);
                    }
                } catch (GitHubRateException) {
                    Remove(actor.GetPrimaryKeyLong());
                    actor = null;
                } catch (InvalidOperationException) {
                    // Grain activation failed
                    Remove(actor.GetPrimaryKeyLong());
                    actor = null;
                }
            }
        }