diff --git a/src/Ocelot.Provider.Consul/OcelotBuilderExtensions.cs b/src/Ocelot.Provider.Consul/OcelotBuilderExtensions.cs index addbac91..ce2ed95b 100644 --- a/src/Ocelot.Provider.Consul/OcelotBuilderExtensions.cs +++ b/src/Ocelot.Provider.Consul/OcelotBuilderExtensions.cs @@ -3,6 +3,7 @@ using Configuration.Repository; using DependencyInjection; using Microsoft.Extensions.DependencyInjection; + using Microsoft.Extensions.DependencyInjection.Extensions; using Middleware; using ServiceDiscovery; @@ -12,6 +13,8 @@ { builder.Services.AddSingleton(ConsulProviderFactory.Get); builder.Services.AddSingleton(); + builder.Services.RemoveAll(typeof(IFileConfigurationPollerOptions)); + builder.Services.AddSingleton(); return builder; } diff --git a/src/Ocelot/Cache/Middleware/OutputCacheMiddleware.cs b/src/Ocelot/Cache/Middleware/OutputCacheMiddleware.cs index 925e90c6..ae99c52e 100644 --- a/src/Ocelot/Cache/Middleware/OutputCacheMiddleware.cs +++ b/src/Ocelot/Cache/Middleware/OutputCacheMiddleware.cs @@ -77,6 +77,18 @@ context.DownstreamResponse = response; } + private string GenerateRequestCacheKey(DownstreamContext context) { + string hashedContent = null; + StringBuilder downStreamUrlKeyBuilder = new StringBuilder($"{context.DownstreamRequest.Method}-{context.DownstreamRequest.OriginalString}"); + if(context.DownstreamRequest.Content != null) { + string requestContentString = Task.Run(async () => await context.DownstreamRequest.Content?.ReadAsStringAsync()).Result; + downStreamUrlKeyBuilder.Append(requestContentString); + } + + hashedContent = MD5Helper.GenerateMd5(downStreamUrlKeyBuilder.ToString()); + return hashedContent; + } + internal DownstreamResponse CreateHttpResponseMessage(CachedResponse cached) { if (cached == null) diff --git a/src/Ocelot/Configuration/Repository/ConsulFileConfigurationPollerOption.cs b/src/Ocelot/Configuration/Repository/ConsulFileConfigurationPollerOption.cs new file mode 100644 index 00000000..8ea624f6 --- /dev/null +++ b/src/Ocelot/Configuration/Repository/ConsulFileConfigurationPollerOption.cs @@ -0,0 +1,38 @@ +using System.Threading.Tasks; +using Ocelot.Responses; + +namespace Ocelot.Configuration.Repository { + public class ConsulFileConfigurationPollerOption : IFileConfigurationPollerOptions { + private readonly IInternalConfigurationRepository _internalConfigRepo; + private readonly IFileConfigurationRepository _fileConfigurationRepository; + + public ConsulFileConfigurationPollerOption(IInternalConfigurationRepository internalConfigurationRepository, + IFileConfigurationRepository fileConfigurationRepository) { + _internalConfigRepo = internalConfigurationRepository; + _fileConfigurationRepository = fileConfigurationRepository; + } + + public int Delay => GetDelay(); + + private int GetDelay() { + int delay = 1000; + + Response fileConfig = Task.Run(async () => await _fileConfigurationRepository.Get()).Result; + if (fileConfig?.Data?.GlobalConfiguration?.ServiceDiscoveryProvider != null && + !fileConfig.IsError && + fileConfig.Data.GlobalConfiguration.ServiceDiscoveryProvider.PollingInterval > 0) { + delay = fileConfig.Data.GlobalConfiguration.ServiceDiscoveryProvider.PollingInterval; + } + else { + Response internalConfig = _internalConfigRepo.Get(); + if (internalConfig?.Data?.ServiceProviderConfiguration != null && + !internalConfig.IsError && + internalConfig.Data.ServiceProviderConfiguration.PollingInterval > 0) { + delay = internalConfig.Data.ServiceProviderConfiguration.PollingInterval; + } + } + + return delay; + } + } +} diff --git a/test/Ocelot.UnitTests/Configuration/FileConfigurationPollerTests.cs b/test/Ocelot.UnitTests/Configuration/FileConfigurationPollerTests.cs index 4c6a6016..35294889 100644 --- a/test/Ocelot.UnitTests/Configuration/FileConfigurationPollerTests.cs +++ b/test/Ocelot.UnitTests/Configuration/FileConfigurationPollerTests.cs @@ -126,6 +126,8 @@ namespace Ocelot.UnitTests.Configuration .BDDfy(); } + + private void WhenProviderErrors() { _repo