now set region in config...or it defaults to something

This commit is contained in:
Tom Gardham-Pallister 2017-06-28 19:02:08 +01:00
parent ab953f28fd
commit 0f60a353ef
19 changed files with 124 additions and 242 deletions

View File

@ -7,7 +7,7 @@ namespace Ocelot.Cache
{
void Add(string key, T value, TimeSpan ttl, string region);
void AddAndDelete(string key, T value, TimeSpan ttl, string region);
T Get(string key);
T Get(string key, string region);
void ClearRegion(string region);
}
}

View File

@ -0,0 +1,9 @@
using Ocelot.Configuration.File;
namespace Ocelot.Cache
{
public interface IRegionCreator
{
string Create(FileReRoute reRoute);
}
}

View File

@ -37,11 +37,11 @@ namespace Ocelot.Cache.Middleware
return;
}
var downstreamUrlKey = DownstreamRequest.RequestUri.OriginalString;
var downstreamUrlKey = $"{DownstreamRequest.Method.Method}-{DownstreamRequest.RequestUri.OriginalString}";
_logger.LogDebug("started checking cache for {downstreamUrlKey}", downstreamUrlKey);
var cached = _outputCache.Get(downstreamUrlKey);
var cached = _outputCache.Get(downstreamUrlKey, DownstreamRoute.ReRoute.CacheOptions.Region);
if (cached != null)
{
@ -67,9 +67,7 @@ namespace Ocelot.Cache.Middleware
var response = HttpResponseMessage;
var region = _regionCreator.Region(DownstreamRoute.ReRoute);
_outputCache.Add(downstreamUrlKey, response, TimeSpan.FromSeconds(DownstreamRoute.ReRoute.FileCacheOptions.TtlSeconds), region);
_outputCache.Add(downstreamUrlKey, response, TimeSpan.FromSeconds(DownstreamRoute.ReRoute.CacheOptions.TtlSeconds), DownstreamRoute.ReRoute.CacheOptions.Region);
_logger.LogDebug("finished response added to cache for {downstreamUrlKey}", downstreamUrlKey);
}

View File

@ -8,18 +8,15 @@ namespace Ocelot.Cache
public class OcelotCacheManagerCache<T> : IOcelotCache<T>
{
private readonly ICacheManager<T> _cacheManager;
private HashSet<string> _keys;
public OcelotCacheManagerCache(ICacheManager<T> cacheManager)
{
_cacheManager = cacheManager;
_keys = new HashSet<string>();
}
public void Add(string key, T value, TimeSpan ttl, string region)
{
_cacheManager.Add(new CacheItem<T>(key, region, value, ExpirationMode.Absolute, ttl));
_keys.Add(key);
}
public void AddAndDelete(string key, T value, TimeSpan ttl, string region)
@ -34,9 +31,9 @@ namespace Ocelot.Cache
Add(key, value, ttl, region);
}
public T Get(string key)
public T Get(string key, string region)
{
return _cacheManager.Get<T>(key);
return _cacheManager.Get<T>(key, region);
}
public void ClearRegion(string region)

View File

@ -1,20 +1,22 @@
using System.Linq;
using Ocelot.Configuration;
using Ocelot.Configuration.File;
namespace Ocelot.Cache
{
public interface IRegionCreator
{
string Region(ReRoute reRoute);
}
public class RegionCreator : IRegionCreator
{
public string Region(ReRoute reRoute)
public string Create(FileReRoute reRoute)
{
var methods = string.Join("", reRoute.UpstreamHttpMethod.Select(m => m.Method));
if(!string.IsNullOrEmpty(reRoute?.FileCacheOptions?.Region))
{
return reRoute?.FileCacheOptions?.Region;
}
var region = $"{methods}{reRoute.UpstreamPathTemplate.Value.Replace("/", "")}";
var methods = string.Join("", reRoute.UpstreamHttpMethod.Select(m => m));
var region = $"{methods}{reRoute.UpstreamPathTemplate.Replace("/", "")}";
return region;
}

View File

@ -1,50 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Ocelot.Configuration.Provider;
using Ocelot.Logging;
namespace Ocelot.Cache
{
public interface IRegionsGetter
{
Task<List<string>> Regions();
}
public class RegionsGetter : IRegionsGetter
{
private readonly IOcelotConfigurationProvider _provider;
private readonly IRegionCreator _creator;
private readonly IOcelotLogger _logger;
public RegionsGetter(IOcelotConfigurationProvider provider, IRegionCreator creator, IOcelotLoggerFactory loggerFactory)
{
_logger = loggerFactory.CreateLogger<RegionsGetter>();
_provider = provider;
_creator = creator;
}
public async Task<List<string>> Regions()
{
var config = await _provider.Get();
if(config.IsError)
{
_logger.LogError("unable to find regions", new Exception(string.Join(",", config.Errors)));
return new List<string>();
}
var cachedReRoutes = config.Data.ReRoutes.Where(x => x.IsCached);
var regions = new List<string>();
foreach(var reRoute in cachedReRoutes)
{
var region = _creator.Region(reRoute);
regions.Add(region);
}
return regions;
}
}
}

View File

@ -2,9 +2,10 @@
{
public class CacheOptions
{
public CacheOptions(int ttlSeconds)
public CacheOptions(int ttlSeconds, string region)
{
TtlSeconds = ttlSeconds;
Region = region;
}
public int TtlSeconds { get; private set; }

View File

@ -4,6 +4,7 @@ using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Ocelot.Cache;
using Ocelot.Configuration.Builder;
using Ocelot.Configuration.File;
using Ocelot.Configuration.Parser;
@ -36,6 +37,7 @@ namespace Ocelot.Configuration.Creator
private IQoSOptionsCreator _qosOptionsCreator;
private IReRouteOptionsCreator _fileReRouteOptionsCreator;
private IRateLimitOptionsCreator _rateLimitOptionsCreator;
private IRegionCreator _regionCreator;
public FileOcelotConfigurationCreator(
IOptions<FileConfiguration> options,
@ -52,9 +54,11 @@ namespace Ocelot.Configuration.Creator
IServiceProviderConfigurationCreator serviceProviderConfigCreator,
IQoSOptionsCreator qosOptionsCreator,
IReRouteOptionsCreator fileReRouteOptionsCreator,
IRateLimitOptionsCreator rateLimitOptionsCreator
IRateLimitOptionsCreator rateLimitOptionsCreator,
IRegionCreator regionCreator
)
{
_regionCreator = regionCreator;
_rateLimitOptionsCreator = rateLimitOptionsCreator;
_requestIdKeyCreator = requestIdKeyCreator;
_upstreamTemplatePatternCreator = upstreamTemplatePatternCreator;
@ -137,6 +141,8 @@ namespace Ocelot.Configuration.Creator
var rateLimitOption = _rateLimitOptionsCreator.Create(fileReRoute, globalConfiguration, fileReRouteOptions.EnableRateLimiting);
var region = _regionCreator.Create(fileReRoute);
var reRoute = new ReRouteBuilder()
.WithDownstreamPathTemplate(fileReRoute.DownstreamPathTemplate)
.WithUpstreamPathTemplate(fileReRoute.UpstreamPathTemplate)
@ -151,7 +157,7 @@ namespace Ocelot.Configuration.Creator
.WithClaimsToQueries(claimsToQueries)
.WithRequestIdKey(requestIdKey)
.WithIsCached(fileReRouteOptions.IsCached)
.WithCacheOptions(new CacheOptions(fileReRoute.FileCacheOptions.TtlSeconds))
.WithCacheOptions(new CacheOptions(fileReRoute.FileCacheOptions.TtlSeconds, region))
.WithDownstreamScheme(fileReRoute.DownstreamScheme)
.WithLoadBalancer(fileReRoute.LoadBalancer)
.WithDownstreamHost(fileReRoute.DownstreamHost)

View File

@ -3,6 +3,6 @@
public class FileCacheOptions
{
public int TtlSeconds { get; set; }
public string Region {get;private set;}
public string Region {get; set;}
}
}

View File

@ -46,7 +46,7 @@ namespace Ocelot.Configuration
IsAuthorised = isAuthorised;
RequestIdKey = requestIdKey;
IsCached = isCached;
FileCacheOptions = fileCacheOptions;
CacheOptions = fileCacheOptions;
ClaimsToQueries = claimsToQueries
?? new List<ClaimToThing>();
ClaimsToClaims = claimsToClaims
@ -74,7 +74,7 @@ namespace Ocelot.Configuration
public Dictionary<string, string> RouteClaimsRequirement { get; private set; }
public string RequestIdKey { get; private set; }
public bool IsCached { get; private set; }
public CacheOptions FileCacheOptions { get; private set; }
public CacheOptions CacheOptions { get; private set; }
public string DownstreamScheme {get;private set;}
public bool IsQos { get; private set; }
public QoSOptions QosOptionsOptions { get; private set; }

View File

@ -30,7 +30,7 @@ namespace Ocelot.Configuration.Repository
public async Task<Response<IOcelotConfiguration>> Get()
{
var config = _cache.Get(_ocelotConfiguration);
var config = _cache.Get(_ocelotConfiguration, _ocelotConfiguration);
if (config != null)
{
@ -68,7 +68,7 @@ namespace Ocelot.Configuration.Repository
if (result.Response)
{
_cache.AddAndDelete(_ocelotConfiguration, ocelotConfiguration, TimeSpan.FromSeconds(3), "OcelotConfiguration");
_cache.AddAndDelete(_ocelotConfiguration, ocelotConfiguration, TimeSpan.FromSeconds(3), _ocelotConfiguration);
return new OkResponse();
}

View File

@ -12,19 +12,10 @@ namespace Ocelot.Controllers
public class OutputCacheController : Controller
{
private IOcelotCache<HttpResponseMessage> _cache;
private IRegionsGetter _regionsGetter;
public OutputCacheController(IOcelotCache<HttpResponseMessage> cache, IRegionsGetter regionsGetter)
public OutputCacheController(IOcelotCache<HttpResponseMessage> cache)
{
_cache = cache;
_regionsGetter = regionsGetter;
}
[HttpGet]
public async Task<IActionResult> Get()
{
var regions = await _regionsGetter.Regions();
return new OkObjectResult(new Regions(regions));
}
[HttpDelete]

View File

@ -145,7 +145,6 @@ namespace Ocelot.DependencyInjection
.AddJsonFormatters();
services.AddLogging();
services.TryAddSingleton<IRegionsGetter, RegionsGetter>();
services.TryAddSingleton<IRegionCreator, RegionCreator>();
services.TryAddSingleton<IFileConfigurationRepository, FileConfigurationRepository>();
services.TryAddSingleton<IFileConfigurationSetter, FileConfigurationSetter>();

View File

@ -18,6 +18,7 @@ namespace Ocelot.UnitTests.Cache
private string _resultGet;
private TimeSpan _ttlSeconds;
private List<string> _resultKeys;
private string _region;
public CacheManagerCacheTests()
{
@ -28,7 +29,7 @@ namespace Ocelot.UnitTests.Cache
[Fact]
public void should_get_from_cache()
{
this.Given(x => x.GivenTheFollowingIsCached("someKey", "someValue"))
this.Given(x => x.GivenTheFollowingIsCached("someKey", "someRegion", "someValue"))
.When(x => x.WhenIGetFromTheCache())
.Then(x => x.ThenTheResultIs("someValue"))
.BDDfy();
@ -88,15 +89,16 @@ namespace Ocelot.UnitTests.Cache
private void WhenIGetFromTheCache()
{
_resultGet = _ocelotOcelotCacheManager.Get(_key);
_resultGet = _ocelotOcelotCacheManager.Get(_key, _region);
}
private void GivenTheFollowingIsCached(string key, string value)
private void GivenTheFollowingIsCached(string key, string region, string value)
{
_key = key;
_value = value;
_region = region;
_mockCacheManager
.Setup(x => x.Get<string>(It.IsAny<string>()))
.Setup(x => x.Get<string>(It.IsAny<string>(), It.IsAny<string>()))
.Returns(value);
}
}

View File

@ -92,7 +92,7 @@ namespace Ocelot.UnitTests.Cache
{
var reRoute = new ReRouteBuilder()
.WithIsCached(true)
.WithCacheOptions(new CacheOptions(100))
.WithCacheOptions(new CacheOptions(100, "kanken"))
.WithUpstreamHttpMethod(new List<string> { "Get" })
.Build();
@ -120,7 +120,7 @@ namespace Ocelot.UnitTests.Cache
private void ThenTheCacheGetIsCalledCorrectly()
{
_cacheManager
.Verify(x => x.Get(It.IsAny<string>()), Times.Once);
.Verify(x => x.Get(It.IsAny<string>(), It.IsAny<string>()), Times.Once);
}
private void ThenTheCacheAddIsCalledCorrectly()
@ -140,7 +140,7 @@ namespace Ocelot.UnitTests.Cache
{
_response = response;
_cacheManager
.Setup(x => x.Get(It.IsAny<string>()))
.Setup(x => x.Get(It.IsAny<string>(), It.IsAny<string>()))
.Returns(_response);
}

View File

@ -2,6 +2,7 @@ using System.Collections.Generic;
using Ocelot.Cache;
using Ocelot.Configuration;
using Ocelot.Configuration.Builder;
using Ocelot.Configuration.File;
using Shouldly;
using TestStack.BDDfy;
using Xunit;
@ -11,15 +12,16 @@ namespace Ocelot.UnitTests.Cache
public class RegionCreatorTests
{
private string _result;
private ReRoute _reRoute;
private FileReRoute _reRoute;
[Fact]
public void should_create_region()
{
var reRoute = new ReRouteBuilder()
.WithUpstreamHttpMethod(new List<string>{"Get"})
.WithUpstreamPathTemplate("/test/dummy")
.Build();
var reRoute = new FileReRoute
{
UpstreamHttpMethod = new List<string> { "Get" },
UpstreamPathTemplate = "/testdummy"
};
this.Given(_ => GivenTheReRoute(reRoute))
.When(_ => WhenICreateTheRegion())
@ -27,7 +29,24 @@ namespace Ocelot.UnitTests.Cache
.BDDfy();
}
private void GivenTheReRoute(ReRoute reRoute)
[Fact]
public void should_use_region()
{
var reRoute = new FileReRoute
{
FileCacheOptions = new FileCacheOptions
{
Region = "region"
}
};
this.Given(_ => GivenTheReRoute(reRoute))
.When(_ => WhenICreateTheRegion())
.Then(_ => ThenTheRegionIs("region"))
.BDDfy();
}
private void GivenTheReRoute(FileReRoute reRoute)
{
_reRoute = reRoute;
}
@ -35,7 +54,7 @@ namespace Ocelot.UnitTests.Cache
private void WhenICreateTheRegion()
{
RegionCreator regionCreator = new RegionCreator();
_result = regionCreator.Region(_reRoute);
_result = regionCreator.Create(_reRoute);
}
private void ThenTheRegionIs(string expected)

View File

@ -1,113 +0,0 @@
using Xunit;
using TestStack.BDDfy;
using Shouldly;
using Ocelot.Cache;
using Moq;
using Ocelot.Configuration.Provider;
using System.Collections.Generic;
using Ocelot.Responses;
using Ocelot.Configuration;
using System.Threading.Tasks;
using Ocelot.Configuration.Builder;
using System;
using Ocelot.Errors;
using Ocelot.Logging;
namespace Ocelot.UnitTests.Cache
{
public class RegionsGetterTests
{
private RegionsGetter _regionsGetter;
private readonly Mock<IOcelotConfigurationProvider> _provider;
private readonly Mock<IRegionCreator> _creator;
private readonly Mock<IOcelotLoggerFactory> _factory;
private List<string> _result;
public RegionsGetterTests()
{
_provider = new Mock<IOcelotConfigurationProvider>();
_creator = new Mock<IRegionCreator>();
_factory = new Mock<IOcelotLoggerFactory>();
var logger = new Mock<IOcelotLogger>();
_factory
.Setup(x => x.CreateLogger<RegionsGetter>())
.Returns(logger.Object);
_regionsGetter = new RegionsGetter(_provider.Object, _creator.Object, _factory.Object);
}
[Fact]
public void should_get_regions()
{
var cacheOptions = new CacheOptions(12);
var reRoute = new ReRouteBuilder()
.WithUpstreamHttpMethod(new List<string>{"Get"})
.WithUpstreamPathTemplate("/")
.WithCacheOptions(cacheOptions)
.WithIsCached(true)
.Build();
var reRoutes = new List<ReRoute>
{
reRoute
};
var config = new OcelotConfiguration(reRoutes, "whocares!");
var expected = new List<string>
{
"balls"
};
this.Given(_ => GivenTheFollowingConfig(config))
.And(_ => GivenTheProviderReturns("balls"))
.When(_ => WhenIGetTheRegions())
.Then(_ => ThenTheFollowingIsReturned(expected))
.BDDfy();
}
[Fact]
public void should_return_empty_regions()
{
var expected = new List<string>();
this.Given(_ => GivenAnErrorGettingTheConfig())
.When(_ => WhenIGetTheRegions())
.Then(_ => ThenTheFollowingIsReturned(expected))
.BDDfy();
}
private void GivenAnErrorGettingTheConfig()
{
var config = new OcelotConfiguration(new List<ReRoute>(), "whocares!");
_provider
.Setup(x => x.Get())
.ReturnsAsync(new ErrorResponse<IOcelotConfiguration>(It.IsAny<Error>()));
}
private void GivenTheProviderReturns(string expected)
{
_creator
.Setup(x => x.Region(It.IsAny<ReRoute>()))
.Returns(expected);
}
private void GivenTheFollowingConfig(IOcelotConfiguration config)
{
_provider
.Setup(x => x.Get())
.ReturnsAsync(new OkResponse<IOcelotConfiguration>(config));
}
private void WhenIGetTheRegions()
{
_result = _regionsGetter.Regions().Result;
}
private void ThenTheFollowingIsReturned(List<string> expected)
{
_result.ShouldBe(expected);
}
}
}

View File

@ -2,6 +2,7 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Moq;
using Ocelot.Cache;
using Ocelot.Configuration;
using Ocelot.Configuration.Builder;
using Ocelot.Configuration.Creator;
@ -39,6 +40,7 @@ namespace Ocelot.UnitTests.Configuration
private Mock<IQoSOptionsCreator> _qosOptionsCreator;
private Mock<IReRouteOptionsCreator> _fileReRouteOptionsCreator;
private Mock<IRateLimitOptionsCreator> _rateLimitOptions;
private Mock<IRegionCreator> _regionCreator;
public FileConfigurationCreatorTests()
{
@ -59,6 +61,7 @@ namespace Ocelot.UnitTests.Configuration
_qosOptionsCreator = new Mock<IQoSOptionsCreator>();
_fileReRouteOptionsCreator = new Mock<IReRouteOptionsCreator>();
_rateLimitOptions = new Mock<IRateLimitOptionsCreator>();
_regionCreator = new Mock<IRegionCreator>();
_ocelotConfigurationCreator = new FileOcelotConfigurationCreator(
_fileConfig.Object, _validator.Object, _logger.Object,
@ -66,7 +69,51 @@ namespace Ocelot.UnitTests.Configuration
_qosProviderFactory.Object, _qosProviderHouse.Object, _claimsToThingCreator.Object,
_authOptionsCreator.Object, _upstreamTemplatePatternCreator.Object, _requestIdKeyCreator.Object,
_serviceProviderConfigCreator.Object, _qosOptionsCreator.Object, _fileReRouteOptionsCreator.Object,
_rateLimitOptions.Object);
_rateLimitOptions.Object, _regionCreator.Object);
}
[Fact]
public void should_call_region_creator()
{
var reRouteOptions = new ReRouteOptionsBuilder()
.Build();
this.Given(x => x.GivenTheConfigIs(new FileConfiguration
{
ReRoutes = new List<FileReRoute>
{
new FileReRoute
{
DownstreamHost = "127.0.0.1",
UpstreamPathTemplate = "/api/products/{productId}",
DownstreamPathTemplate = "/products/{productId}",
UpstreamHttpMethod = new List<string> { "Get" },
FileCacheOptions = new FileCacheOptions
{
Region = "region"
}
}
},
}))
.And(x => x.GivenTheFollowingOptionsAreReturned(reRouteOptions))
.And(x => x.GivenTheConfigIsValid())
.And(x => x.GivenTheFollowingRegionIsReturned("region"))
.When(x => x.WhenICreateTheConfig())
.Then(x => x.ThenTheRegionCreatorIsCalledCorrectly("region"))
.BDDfy();
}
private void GivenTheFollowingRegionIsReturned(string region)
{
_regionCreator
.Setup(x => x.Create(It.IsAny<FileReRoute>()))
.Returns(region);
}
private void ThenTheRegionCreatorIsCalledCorrectly(string expected)
{
_regionCreator
.Verify(x => x.Create(_fileConfiguration.ReRoutes[0]), Times.Once);
}
[Fact]

View File

@ -15,23 +15,12 @@ namespace Ocelot.UnitTests.Controllers
{
private OutputCacheController _controller;
private Mock<IOcelotCache<HttpResponseMessage>> _cache;
private Mock<IRegionsGetter> _getter;
private IActionResult _result;
public OutputCacheControllerTests()
{
_cache = new Mock<IOcelotCache<HttpResponseMessage>>();
_getter = new Mock<IRegionsGetter>();
_controller = new OutputCacheController(_cache.Object, _getter.Object);
}
[Fact]
public void should_get_all_keys_from_server()
{
this.Given(_ => GivenTheFollowingKeys(new List<string>{"b", "a"}))
.When(_ => WhenIGetTheKeys())
.Then(_ => ThenTheKeysAreReturned())
.BDDfy();
_controller = new OutputCacheController(_cache.Object);
}
[Fact]
@ -53,20 +42,5 @@ namespace Ocelot.UnitTests.Controllers
{
_result = _controller.Delete(key);
}
private void GivenTheFollowingKeys(List<string> keys)
{
}
private void WhenIGetTheKeys()
{
_result = _controller.Get().Result;
}
private void ThenTheKeysAreReturned()
{
_result.ShouldBeOfType<OkObjectResult>();
}
}
}