mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-04-20 17:42:50 +08:00
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:
parent
ed2082a4a3
commit
5b02fb7fe7
@ -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<ServiceDiscoveryFinderDelegate>(ConsulProviderFactory.Get);
|
||||
builder.Services.AddSingleton<IConsulClientFactory, ConsulClientFactory>();
|
||||
builder.Services.RemoveAll(typeof(IFileConfigurationPollerOptions));
|
||||
builder.Services.AddSingleton<IFileConfigurationPollerOptions, ConsulFileConfigurationPollerOption>();
|
||||
return builder;
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
@ -126,6 +126,8 @@ namespace Ocelot.UnitTests.Configuration
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
|
||||
|
||||
private void WhenProviderErrors()
|
||||
{
|
||||
_repo
|
||||
|
Loading…
x
Reference in New Issue
Block a user