From f88e1f65ef10f959cee004882129e6a1b3d27eba Mon Sep 17 00:00:00 2001 From: Tom Pallister Date: Fri, 13 Apr 2018 17:16:43 +0100 Subject: [PATCH] removed some async we dont need --- .../Provider/IOcelotConfigurationProvider.cs | 6 ++---- .../Provider/OcelotConfigurationProvider.cs | 10 ++++------ .../ConsulFileConfigurationRepository.cs | 19 +++++++++++++------ .../IOcelotConfigurationRepository.cs | 10 ++++------ .../InMemoryOcelotConfigurationRepository.cs | 8 ++++---- .../Setter/FileConfigurationSetter.cs | 8 +++++--- .../DependencyInjection/OcelotBuilder.cs | 9 ++------- .../DownstreamRouteFinderMiddleware.cs | 2 +- .../Middleware/ExceptionHandlerMiddleware.cs | 9 +++------ .../Middleware/OcelotMiddlewareExtensions.cs | 11 +++++------ src/Ocelot/Raft/FilePeersProvider.cs | 3 +-- test/Ocelot.ManualTest/Program.cs | 6 ++++++ .../InMemoryConfigurationRepositoryTests.cs | 4 ++-- .../OcelotConfigurationProviderTests.cs | 4 ++-- .../DownstreamRouteFinderMiddlewareTests.cs | 2 +- .../Errors/ExceptionHandlerMiddlewareTests.cs | 6 +++--- 16 files changed, 58 insertions(+), 59 deletions(-) diff --git a/src/Ocelot/Configuration/Provider/IOcelotConfigurationProvider.cs b/src/Ocelot/Configuration/Provider/IOcelotConfigurationProvider.cs index 80f4583b..e7885c98 100644 --- a/src/Ocelot/Configuration/Provider/IOcelotConfigurationProvider.cs +++ b/src/Ocelot/Configuration/Provider/IOcelotConfigurationProvider.cs @@ -1,11 +1,9 @@ -using System.Threading.Tasks; -using Ocelot.Configuration.File; -using Ocelot.Responses; +using Ocelot.Responses; namespace Ocelot.Configuration.Provider { public interface IOcelotConfigurationProvider { - Task> Get(); + Response Get(); } } diff --git a/src/Ocelot/Configuration/Provider/OcelotConfigurationProvider.cs b/src/Ocelot/Configuration/Provider/OcelotConfigurationProvider.cs index ed72a60e..d4c87992 100644 --- a/src/Ocelot/Configuration/Provider/OcelotConfigurationProvider.cs +++ b/src/Ocelot/Configuration/Provider/OcelotConfigurationProvider.cs @@ -1,6 +1,4 @@ -using System.Threading.Tasks; -using Ocelot.Configuration.File; -using Ocelot.Configuration.Repository; +using Ocelot.Configuration.Repository; using Ocelot.Responses; namespace Ocelot.Configuration.Provider @@ -17,9 +15,9 @@ namespace Ocelot.Configuration.Provider _config = repo; } - public async Task> Get() + public Response Get() { - var repoConfig = await _config.Get(); + var repoConfig = _config.Get(); if (repoConfig.IsError) { @@ -29,4 +27,4 @@ namespace Ocelot.Configuration.Provider return new OkResponse(repoConfig.Data); } } -} \ No newline at end of file +} diff --git a/src/Ocelot/Configuration/Repository/ConsulFileConfigurationRepository.cs b/src/Ocelot/Configuration/Repository/ConsulFileConfigurationRepository.cs index 7db04a09..a30e6ae2 100644 --- a/src/Ocelot/Configuration/Repository/ConsulFileConfigurationRepository.cs +++ b/src/Ocelot/Configuration/Repository/ConsulFileConfigurationRepository.cs @@ -5,6 +5,7 @@ using Consul; using Newtonsoft.Json; using Ocelot.Configuration.File; using Ocelot.Infrastructure.Consul; +using Ocelot.Logging; using Ocelot.Responses; using Ocelot.ServiceDiscovery.Configuration; @@ -15,16 +16,22 @@ namespace Ocelot.Configuration.Repository private readonly ConsulClient _consul; private const string OcelotConfiguration = "OcelotConfiguration"; private readonly Cache.IOcelotCache _cache; + private readonly IOcelotLogger _logger; public ConsulFileConfigurationRepository( - Cache.IOcelotCache cache, - ServiceProviderConfiguration serviceProviderConfig, - IConsulClientFactory factory) + Cache.IOcelotCache cache, + ServiceProviderConfiguration serviceProviderConfiguration, + IConsulClientFactory factory, + IOcelotLoggerFactory loggerFactory) { - var consulHost = string.IsNullOrEmpty(serviceProviderConfig?.Host) ? "localhost" : serviceProviderConfig?.Host; - var consulPort = serviceProviderConfig?.Port ?? 8500; - var config = new ConsulRegistryConfiguration(consulHost, consulPort, OcelotConfiguration, serviceProviderConfig?.Token); + _logger = loggerFactory.CreateLogger(); _cache = cache; + + var consulHost = string.IsNullOrEmpty(serviceProviderConfiguration?.Host) ? "localhost" : serviceProviderConfiguration?.Host; + var consulPort = serviceProviderConfiguration?.Port ?? 8500; + var token = serviceProviderConfiguration?.Token; + var config = new ConsulRegistryConfiguration(consulHost, consulPort, OcelotConfiguration, token); + _consul = factory.Get(config); } diff --git a/src/Ocelot/Configuration/Repository/IOcelotConfigurationRepository.cs b/src/Ocelot/Configuration/Repository/IOcelotConfigurationRepository.cs index 16b386a1..d4a7ab13 100644 --- a/src/Ocelot/Configuration/Repository/IOcelotConfigurationRepository.cs +++ b/src/Ocelot/Configuration/Repository/IOcelotConfigurationRepository.cs @@ -1,12 +1,10 @@ -using System.Threading.Tasks; -using Ocelot.Configuration.File; -using Ocelot.Responses; +using Ocelot.Responses; namespace Ocelot.Configuration.Repository { public interface IOcelotConfigurationRepository { - Task> Get(); - Task AddOrReplace(IOcelotConfiguration ocelotConfiguration); + Response Get(); + Response AddOrReplace(IOcelotConfiguration ocelotConfiguration); } -} \ No newline at end of file +} diff --git a/src/Ocelot/Configuration/Repository/InMemoryOcelotConfigurationRepository.cs b/src/Ocelot/Configuration/Repository/InMemoryOcelotConfigurationRepository.cs index 9ce50ba6..19985ca5 100644 --- a/src/Ocelot/Configuration/Repository/InMemoryOcelotConfigurationRepository.cs +++ b/src/Ocelot/Configuration/Repository/InMemoryOcelotConfigurationRepository.cs @@ -12,19 +12,19 @@ namespace Ocelot.Configuration.Repository private IOcelotConfiguration _ocelotConfiguration; - public Task> Get() + public Response Get() { - return Task.FromResult>(new OkResponse(_ocelotConfiguration)); + return new OkResponse(_ocelotConfiguration); } - public Task AddOrReplace(IOcelotConfiguration ocelotConfiguration) + public Response AddOrReplace(IOcelotConfiguration ocelotConfiguration) { lock (LockObject) { _ocelotConfiguration = ocelotConfiguration; } - return Task.FromResult(new OkResponse()); + return new OkResponse(); } } } diff --git a/src/Ocelot/Configuration/Setter/FileConfigurationSetter.cs b/src/Ocelot/Configuration/Setter/FileConfigurationSetter.cs index 38a7c1cb..27e88e7c 100644 --- a/src/Ocelot/Configuration/Setter/FileConfigurationSetter.cs +++ b/src/Ocelot/Configuration/Setter/FileConfigurationSetter.cs @@ -12,8 +12,10 @@ namespace Ocelot.Configuration.Setter private readonly IOcelotConfigurationCreator _configCreator; private readonly IFileConfigurationRepository _repo; - public FileConfigurationSetter(IOcelotConfigurationRepository configRepo, - IOcelotConfigurationCreator configCreator, IFileConfigurationRepository repo) + public FileConfigurationSetter( + IOcelotConfigurationRepository configRepo, + IOcelotConfigurationCreator configCreator, + IFileConfigurationRepository repo) { _configRepo = configRepo; _configCreator = configCreator; @@ -33,7 +35,7 @@ namespace Ocelot.Configuration.Setter if(!config.IsError) { - await _configRepo.AddOrReplace(config.Data); + _configRepo.AddOrReplace(config.Data); } return new ErrorResponse(config.Errors); diff --git a/src/Ocelot/DependencyInjection/OcelotBuilder.cs b/src/Ocelot/DependencyInjection/OcelotBuilder.cs index 96e084c1..d3fa993c 100644 --- a/src/Ocelot/DependencyInjection/OcelotBuilder.cs +++ b/src/Ocelot/DependencyInjection/OcelotBuilder.cs @@ -1,7 +1,3 @@ -using Butterfly.Client.Tracing; -using Microsoft.Extensions.Options; -using Ocelot.Middleware.Multiplexer; - namespace Ocelot.DependencyInjection { using CacheManager.Core; @@ -22,7 +18,6 @@ namespace Ocelot.DependencyInjection using Ocelot.Configuration.Validator; using Ocelot.DownstreamRouteFinder.Finder; using Ocelot.DownstreamRouteFinder.UrlMatcher; - using Ocelot.DownstreamUrlCreator; using Ocelot.DownstreamUrlCreator.UrlTemplateReplacer; using Ocelot.Headers; using Ocelot.Infrastructure.Claims.Parser; @@ -44,16 +39,16 @@ namespace Ocelot.DependencyInjection using System.Security.Cryptography.X509Certificates; using IdentityServer4.AccessTokenValidation; using Microsoft.AspNetCore.Builder; - using Microsoft.AspNetCore.Hosting; using Ocelot.Configuration; using Ocelot.Configuration.Builder; using FileConfigurationProvider = Ocelot.Configuration.Provider.FileConfigurationProvider; using Microsoft.Extensions.DependencyInjection.Extensions; - using System.Linq; using System.Net.Http; using Butterfly.Client.AspNetCore; using Ocelot.Infrastructure; using Ocelot.Infrastructure.Consul; + using Butterfly.Client.Tracing; + using Ocelot.Middleware.Multiplexer; public class OcelotBuilder : IOcelotBuilder { diff --git a/src/Ocelot/DownstreamRouteFinder/Middleware/DownstreamRouteFinderMiddleware.cs b/src/Ocelot/DownstreamRouteFinder/Middleware/DownstreamRouteFinderMiddleware.cs index 70e4a4e2..ad91a8d2 100644 --- a/src/Ocelot/DownstreamRouteFinder/Middleware/DownstreamRouteFinderMiddleware.cs +++ b/src/Ocelot/DownstreamRouteFinder/Middleware/DownstreamRouteFinderMiddleware.cs @@ -36,7 +36,7 @@ namespace Ocelot.DownstreamRouteFinder.Middleware var upstreamHost = context.HttpContext.Request.Headers["Host"]; - var configuration = await _configProvider.Get(); + var configuration = _configProvider.Get(); if (configuration.IsError) { diff --git a/src/Ocelot/Errors/Middleware/ExceptionHandlerMiddleware.cs b/src/Ocelot/Errors/Middleware/ExceptionHandlerMiddleware.cs index 236e2e8d..46905502 100644 --- a/src/Ocelot/Errors/Middleware/ExceptionHandlerMiddleware.cs +++ b/src/Ocelot/Errors/Middleware/ExceptionHandlerMiddleware.cs @@ -1,10 +1,7 @@ using System; using System.Linq; using System.Threading.Tasks; -using Microsoft.AspNetCore.Http; -using Microsoft.Extensions.Primitives; using Ocelot.Configuration.Provider; -using Ocelot.DownstreamRouteFinder.Middleware; using Ocelot.Infrastructure.Extensions; using Ocelot.Infrastructure.RequestData; using Ocelot.Logging; @@ -36,7 +33,7 @@ namespace Ocelot.Errors.Middleware { try { - await TrySetGlobalRequestId(context); + TrySetGlobalRequestId(context); Logger.LogDebug("ocelot pipeline started"); @@ -56,12 +53,12 @@ namespace Ocelot.Errors.Middleware Logger.LogDebug("ocelot pipeline finished"); } - private async Task TrySetGlobalRequestId(DownstreamContext context) + private void TrySetGlobalRequestId(DownstreamContext context) { //try and get the global request id and set it for logs... //should this basically be immutable per request...i guess it should! //first thing is get config - var configuration = await _provider.Get(); + var configuration = _provider.Get(); if(configuration.IsError) { diff --git a/src/Ocelot/Middleware/OcelotMiddlewareExtensions.cs b/src/Ocelot/Middleware/OcelotMiddlewareExtensions.cs index 9d345dc2..7e061654 100644 --- a/src/Ocelot/Middleware/OcelotMiddlewareExtensions.cs +++ b/src/Ocelot/Middleware/OcelotMiddlewareExtensions.cs @@ -4,7 +4,6 @@ using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Hosting; - using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Options; using System.Diagnostics; using Microsoft.AspNetCore.Builder; @@ -89,7 +88,7 @@ { var deps = GetDependencies(builder); - var ocelotConfiguration = await deps.provider.Get(); + var ocelotConfiguration = deps.provider.Get(); if (ConfigurationNotSetUp(ocelotConfiguration)) { @@ -101,7 +100,7 @@ } } - return await GetOcelotConfigAndReturn(deps.provider); + return GetOcelotConfigAndReturn(deps.provider); } private static async Task SetConfig(IApplicationBuilder builder, IOptions fileConfiguration, IFileConfigurationSetter setter, IOcelotConfigurationProvider provider, IFileConfigurationRepository repo) @@ -137,9 +136,9 @@ return (fileConfiguration, setter, provider, repo); } - private static async Task GetOcelotConfigAndReturn(IOcelotConfigurationProvider provider) + private static IOcelotConfiguration GetOcelotConfigAndReturn(IOcelotConfigurationProvider provider) { - var ocelotConfiguration = await provider.Get(); + var ocelotConfiguration = provider.Get(); if(ocelotConfiguration == null || ocelotConfiguration.Data == null || ocelotConfiguration.IsError) { @@ -187,7 +186,7 @@ return new ErrorResponse(ocelotConfig.Errors); } - config = await ocelotConfigurationRepository.AddOrReplace(ocelotConfig.Data); + config = ocelotConfigurationRepository.AddOrReplace(ocelotConfig.Data); if (config.IsError) { diff --git a/src/Ocelot/Raft/FilePeersProvider.cs b/src/Ocelot/Raft/FilePeersProvider.cs index d31dc2ca..20b3620d 100644 --- a/src/Ocelot/Raft/FilePeersProvider.cs +++ b/src/Ocelot/Raft/FilePeersProvider.cs @@ -28,8 +28,7 @@ namespace Ocelot.Raft _options = options; _peers = new List(); - //todo - sort out async nonsense.. - var config = _provider.Get().GetAwaiter().GetResult(); + var config = _provider.Get(); foreach (var item in _options.Value.Peers) { var httpClient = new HttpClient(); diff --git a/test/Ocelot.ManualTest/Program.cs b/test/Ocelot.ManualTest/Program.cs index 966b1097..59a9a45f 100644 --- a/test/Ocelot.ManualTest/Program.cs +++ b/test/Ocelot.ManualTest/Program.cs @@ -22,6 +22,12 @@ namespace Ocelot.ManualTest .AddJsonFile("appsettings.json", true, true) .AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", true, true) .AddJsonFile("ocelot.json") + + //.AddOcelot(); + //load all the ocelot.xxx.json files that are not environments from asp.net core + //merge them into megaconfig + //save megaconfig to disk as ocelot.json + //then add to asp.net config stuff.. .AddEnvironmentVariables(); }) .ConfigureServices(s => { diff --git a/test/Ocelot.UnitTests/Configuration/InMemoryConfigurationRepositoryTests.cs b/test/Ocelot.UnitTests/Configuration/InMemoryConfigurationRepositoryTests.cs index cdc3f94d..6216e24b 100644 --- a/test/Ocelot.UnitTests/Configuration/InMemoryConfigurationRepositoryTests.cs +++ b/test/Ocelot.UnitTests/Configuration/InMemoryConfigurationRepositoryTests.cs @@ -49,7 +49,7 @@ namespace Ocelot.UnitTests.Configuration private void WhenIGetTheConfiguration() { - _getResult = _repo.Get().Result; + _getResult = _repo.Get(); } private void GivenThereIsASavedConfiguration() @@ -65,7 +65,7 @@ namespace Ocelot.UnitTests.Configuration private void WhenIAddOrReplaceTheConfig() { - _result = _repo.AddOrReplace(_config).Result; + _result = _repo.AddOrReplace(_config); } private void ThenNoErrorsAreReturned() diff --git a/test/Ocelot.UnitTests/Configuration/OcelotConfigurationProviderTests.cs b/test/Ocelot.UnitTests/Configuration/OcelotConfigurationProviderTests.cs index 982fe091..24000ea4 100644 --- a/test/Ocelot.UnitTests/Configuration/OcelotConfigurationProviderTests.cs +++ b/test/Ocelot.UnitTests/Configuration/OcelotConfigurationProviderTests.cs @@ -56,12 +56,12 @@ namespace Ocelot.UnitTests.Configuration { _configurationRepository .Setup(x => x.Get()) - .ReturnsAsync(config); + .Returns(config); } private void WhenIGetTheConfig() { - _result = _ocelotConfigurationProvider.Get().Result; + _result = _ocelotConfigurationProvider.Get(); } private void TheFollowingIsReturned(Response expected) diff --git a/test/Ocelot.UnitTests/DownstreamRouteFinder/DownstreamRouteFinderMiddlewareTests.cs b/test/Ocelot.UnitTests/DownstreamRouteFinder/DownstreamRouteFinderMiddlewareTests.cs index d3363b00..7361c862 100644 --- a/test/Ocelot.UnitTests/DownstreamRouteFinder/DownstreamRouteFinderMiddlewareTests.cs +++ b/test/Ocelot.UnitTests/DownstreamRouteFinder/DownstreamRouteFinderMiddlewareTests.cs @@ -79,7 +79,7 @@ namespace Ocelot.UnitTests.DownstreamRouteFinder _config = config; _provider .Setup(x => x.Get()) - .ReturnsAsync(new OkResponse(_config)); + .Returns(new OkResponse(_config)); } private void GivenTheDownStreamRouteFinderReturns(DownstreamRoute downstreamRoute) diff --git a/test/Ocelot.UnitTests/Errors/ExceptionHandlerMiddlewareTests.cs b/test/Ocelot.UnitTests/Errors/ExceptionHandlerMiddlewareTests.cs index 6b52a1c5..2d905035 100644 --- a/test/Ocelot.UnitTests/Errors/ExceptionHandlerMiddlewareTests.cs +++ b/test/Ocelot.UnitTests/Errors/ExceptionHandlerMiddlewareTests.cs @@ -134,7 +134,7 @@ namespace Ocelot.UnitTests.Errors { var ex = new Exception("outer", new Exception("inner")); _provider - .Setup(x => x.Get()).ThrowsAsync(ex); + .Setup(x => x.Get()).Throws(ex); } private void ThenAnExceptionIsThrown() @@ -146,7 +146,7 @@ namespace Ocelot.UnitTests.Errors { var response = new Responses.ErrorResponse(new FakeError()); _provider - .Setup(x => x.Get()).ReturnsAsync(response); + .Setup(x => x.Get()).Returns(response); } private void TheRequestIdIsSet(string key, string value) @@ -158,7 +158,7 @@ namespace Ocelot.UnitTests.Errors { var response = new Responses.OkResponse(config); _provider - .Setup(x => x.Get()).ReturnsAsync(response); + .Setup(x => x.Get()).Returns(response); } private void GivenAnExceptionWillNotBeThrownDownstream()