public Container() { var services = new ServiceCollection(); var policies = services.AddPolicyRegistry(); var innerTimeout = Policy.TimeoutAsync <HttpResponseMessage>(TimeSpan.FromSeconds(10), TimeoutStrategy.Optimistic); var immediateRetry = AsyncRetryTResultSyntax.RetryForeverAsync(Policy.Handle <HttpRequestException>() .Or <TimeoutRejectedException>() .OrResult <HttpResponseMessage>(r => (int)r.StatusCode >= 500)); var rateLimit = Policy.Handle <TooManyRequestsException>() .WaitAndRetryForeverAsync(retryAttempt => TimeSpan.FromSeconds(Math.Min(8, Math.Pow(2, retryAttempt))) + TimeSpan.FromMilliseconds(Jitterer.Next(0, 1000))); policies.Add("Http", rateLimit.WrapAsync(innerTimeout)); policies.Add("HttpIdempotent", rateLimit.WrapAsync(immediateRetry).WrapAsync(innerTimeout)); services.AddTransient <UnauthorizedMessageHandler>(); services.AddTransient <BadMessageHandler>(); services.AddTransient <RateLimitHandler>(); services.AddHttpClient("GW2SDK", http => { http.UseBaseAddress(new Uri("https://api.guildwars2.com", UriKind.Absolute)); http.UseLatestSchemaVersion(); http.UseDataCompression(); }) .ConfigurePrimaryHttpMessageHandler(() => new SocketsHttpHandler { MaxConnectionsPerServer = 8, AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate }) .AddPolicyHandlerFromRegistry((registry, message) => message.Method == HttpMethod.Post || message.Method == HttpMethod.Patch ? registry.Get <IAsyncPolicy <HttpResponseMessage> >("Http") : registry.Get <IAsyncPolicy <HttpResponseMessage> >("HttpIdempotent")) .AddHttpMessageHandler <UnauthorizedMessageHandler>() .AddHttpMessageHandler <BadMessageHandler>() .AddHttpMessageHandler <RateLimitHandler>() .AddTypedClient <ContinentService>() .AddTypedClient <JsonAchievementService>() .AddTypedClient <JsonAchievementCategoriesService>() .AddTypedClient <JsonAchievementGroupsService>() .AddTypedClient <JsonBuildService>() .AddTypedClient <JsonColorService>() .AddTypedClient <JsonContinentService>() .AddTypedClient <JsonFloorService>() .AddTypedClient <JsonItemService>() .AddTypedClient <JsonItemPriceService>() .AddTypedClient <JsonRecipeService>() .AddTypedClient <JsonSkinService>() .AddTypedClient <JsonWorldService>() .ConfigureHttpClient(c => { }); _services = services.BuildServiceProvider(); }
/// <inheritdoc /> public async Task WaitUntilSuccess(IDockerClient dockerClient, string containerId) { var retryPolicy = AsyncRetryTResultSyntax.WaitAndRetryForeverAsync(Policy .HandleResult <ContainerState>(s => !IsContainerRunning(s)), retry => TimeSpan.FromSeconds(1)); var outcome = await Policy .TimeoutAsync(TimeSpan.FromMinutes(1)) .WrapAsync(retryPolicy) .ExecuteAndCaptureAsync(async() => await GetCurrentState(dockerClient, containerId)); if (outcome.Outcome == OutcomeType.Failure) { throw new ContainerLaunchException("Container.Abstractions startup failed", outcome.FinalException); } }