Fix/consul poller option (#899)

* cache key now can generate from query string for request with Get Methods and request content for requests with post methods

* MD5Helper Added. OutputCacheMiddleware now can generate cache key using method, url and content

* ConsulFileConfigurationPollerOption created. consul poller option bug fixed

* ConsulFileConfigurationPollerOptionTest created
This commit is contained in:
Ali 2019-05-23 06:09:12 +04:30 committed by Thiago Loureiro
parent ed2082a4a3
commit 5b02fb7fe7
4 changed files with 55 additions and 0 deletions

View File

@ -3,6 +3,7 @@
using Configuration.Repository; using Configuration.Repository;
using DependencyInjection; using DependencyInjection;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Middleware; using Middleware;
using ServiceDiscovery; using ServiceDiscovery;
@ -12,6 +13,8 @@
{ {
builder.Services.AddSingleton<ServiceDiscoveryFinderDelegate>(ConsulProviderFactory.Get); builder.Services.AddSingleton<ServiceDiscoveryFinderDelegate>(ConsulProviderFactory.Get);
builder.Services.AddSingleton<IConsulClientFactory, ConsulClientFactory>(); builder.Services.AddSingleton<IConsulClientFactory, ConsulClientFactory>();
builder.Services.RemoveAll(typeof(IFileConfigurationPollerOptions));
builder.Services.AddSingleton<IFileConfigurationPollerOptions, ConsulFileConfigurationPollerOption>();
return builder; return builder;
} }

View File

@ -77,6 +77,18 @@
context.DownstreamResponse = response; 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) internal DownstreamResponse CreateHttpResponseMessage(CachedResponse cached)
{ {
if (cached == null) if (cached == null)

View File

@ -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<File.FileConfiguration> 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<IInternalConfiguration> internalConfig = _internalConfigRepo.Get();
if (internalConfig?.Data?.ServiceProviderConfiguration != null &&
!internalConfig.IsError &&
internalConfig.Data.ServiceProviderConfiguration.PollingInterval > 0) {
delay = internalConfig.Data.ServiceProviderConfiguration.PollingInterval;
}
}
return delay;
}
}
}

View File

@ -126,6 +126,8 @@ namespace Ocelot.UnitTests.Configuration
.BDDfy(); .BDDfy();
} }
private void WhenProviderErrors() private void WhenProviderErrors()
{ {
_repo _repo