Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/consul poller option #899

Merged
merged 6 commits into from
May 23, 2019
3 changes: 3 additions & 0 deletions src/Ocelot.Provider.Consul/OcelotBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Configuration.Repository;
using DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Middleware;
using ServiceDiscovery;

Expand All @@ -12,6 +13,8 @@ public static IOcelotBuilder AddConsul(this IOcelotBuilder builder)
{
builder.Services.AddSingleton<ServiceDiscoveryFinderDelegate>(ConsulProviderFactory.Get);
builder.Services.AddSingleton<IConsulClientFactory, ConsulClientFactory>();
builder.Services.RemoveAll(typeof(IFileConfigurationPollerOptions));
builder.Services.AddSingleton<IFileConfigurationPollerOptions, ConsulFileConfigurationPollerOption>();
return builder;
}

Expand Down
12 changes: 12 additions & 0 deletions src/Ocelot/Cache/Middleware/OutputCacheMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,18 @@ private void SetHttpResponseMessageThisRequest(DownstreamContext context,
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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ public void should_do_nothing_if_call_to_provider_fails()
.BDDfy();
}



private void WhenProviderErrors()
{
_repo
Expand Down