mirror of
				https://github.com/nsnail/Ocelot.git
				synced 2025-11-04 23:50:49 +08:00 
			
		
		
		
	qos options creator in own class
This commit is contained in:
		@@ -32,6 +32,7 @@ namespace Ocelot.Configuration.Creator
 | 
				
			|||||||
        private IUpstreamTemplatePatternCreator _upstreamTemplatePatternCreator;
 | 
					        private IUpstreamTemplatePatternCreator _upstreamTemplatePatternCreator;
 | 
				
			||||||
        private IRequestIdKeyCreator _requestIdKeyCreator;
 | 
					        private IRequestIdKeyCreator _requestIdKeyCreator;
 | 
				
			||||||
        private IServiceProviderConfigurationCreator _serviceProviderConfigCreator;
 | 
					        private IServiceProviderConfigurationCreator _serviceProviderConfigCreator;
 | 
				
			||||||
 | 
					        private IQoSOptionsCreator _qosOptionsCreator;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        public FileOcelotConfigurationCreator(
 | 
					        public FileOcelotConfigurationCreator(
 | 
				
			||||||
            IOptions<FileConfiguration> options, 
 | 
					            IOptions<FileConfiguration> options, 
 | 
				
			||||||
@@ -45,7 +46,9 @@ namespace Ocelot.Configuration.Creator
 | 
				
			|||||||
            IAuthenticationOptionsCreator authOptionsCreator,
 | 
					            IAuthenticationOptionsCreator authOptionsCreator,
 | 
				
			||||||
            IUpstreamTemplatePatternCreator upstreamTemplatePatternCreator,
 | 
					            IUpstreamTemplatePatternCreator upstreamTemplatePatternCreator,
 | 
				
			||||||
            IRequestIdKeyCreator requestIdKeyCreator,
 | 
					            IRequestIdKeyCreator requestIdKeyCreator,
 | 
				
			||||||
            IServiceProviderConfigurationCreator serviceProviderConfigCreator)
 | 
					            IServiceProviderConfigurationCreator serviceProviderConfigCreator,
 | 
				
			||||||
 | 
					            IQoSOptionsCreator qosOptionsCreator
 | 
				
			||||||
 | 
					            )
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            _requestIdKeyCreator = requestIdKeyCreator;
 | 
					            _requestIdKeyCreator = requestIdKeyCreator;
 | 
				
			||||||
            _upstreamTemplatePatternCreator = upstreamTemplatePatternCreator;
 | 
					            _upstreamTemplatePatternCreator = upstreamTemplatePatternCreator;
 | 
				
			||||||
@@ -59,6 +62,7 @@ namespace Ocelot.Configuration.Creator
 | 
				
			|||||||
            _logger = logger;
 | 
					            _logger = logger;
 | 
				
			||||||
            _claimsToThingCreator = claimsToThingCreator;
 | 
					            _claimsToThingCreator = claimsToThingCreator;
 | 
				
			||||||
            _serviceProviderConfigCreator = serviceProviderConfigCreator;
 | 
					            _serviceProviderConfigCreator = serviceProviderConfigCreator;
 | 
				
			||||||
 | 
					            _qosOptionsCreator = qosOptionsCreator;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        public async Task<Response<IOcelotConfiguration>> Create()
 | 
					        public async Task<Response<IOcelotConfiguration>> Create()
 | 
				
			||||||
@@ -128,7 +132,7 @@ namespace Ocelot.Configuration.Creator
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
            var claimsToQueries = _claimsToThingCreator.Create(fileReRoute.AddQueriesToRequest);
 | 
					            var claimsToQueries = _claimsToThingCreator.Create(fileReRoute.AddQueriesToRequest);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            var qosOptions = BuildQoSOptions(fileReRoute);
 | 
					            var qosOptions = _qosOptionsCreator.Create(fileReRoute);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            var enableRateLimiting = IsEnableRateLimiting(fileReRoute);
 | 
					            var enableRateLimiting = IsEnableRateLimiting(fileReRoute);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -186,15 +190,6 @@ namespace Ocelot.Configuration.Creator
 | 
				
			|||||||
            return (fileReRoute.RateLimitOptions != null && fileReRoute.RateLimitOptions.EnableRateLimiting) ? true : false;
 | 
					            return (fileReRoute.RateLimitOptions != null && fileReRoute.RateLimitOptions.EnableRateLimiting) ? true : false;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        private QoSOptions BuildQoSOptions(FileReRoute fileReRoute)
 | 
					 | 
				
			||||||
        {
 | 
					 | 
				
			||||||
            return new QoSOptionsBuilder()
 | 
					 | 
				
			||||||
                .WithExceptionsAllowedBeforeBreaking(fileReRoute.QoSOptions.ExceptionsAllowedBeforeBreaking)
 | 
					 | 
				
			||||||
                .WithDurationOfBreak(fileReRoute.QoSOptions.DurationOfBreak)
 | 
					 | 
				
			||||||
                .WithTimeoutValue(fileReRoute.QoSOptions.TimeoutValue)
 | 
					 | 
				
			||||||
                .Build();
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        private bool IsQoS(FileReRoute fileReRoute)
 | 
					        private bool IsQoS(FileReRoute fileReRoute)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            return fileReRoute.QoSOptions?.ExceptionsAllowedBeforeBreaking > 0 && fileReRoute.QoSOptions?.TimeoutValue > 0;
 | 
					            return fileReRoute.QoSOptions?.ExceptionsAllowedBeforeBreaking > 0 && fileReRoute.QoSOptions?.TimeoutValue > 0;
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										9
									
								
								src/Ocelot/Configuration/Creator/IQoSOptionsCreator.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								src/Ocelot/Configuration/Creator/IQoSOptionsCreator.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,9 @@
 | 
				
			|||||||
 | 
					using Ocelot.Configuration.File;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					namespace Ocelot.Configuration.Creator
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    public interface IQoSOptionsCreator
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        QoSOptions Create(FileReRoute fileReRoute);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										17
									
								
								src/Ocelot/Configuration/Creator/QoSOptionsCreator.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								src/Ocelot/Configuration/Creator/QoSOptionsCreator.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,17 @@
 | 
				
			|||||||
 | 
					using Ocelot.Configuration.Builder;
 | 
				
			||||||
 | 
					using Ocelot.Configuration.File;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					namespace Ocelot.Configuration.Creator
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    public class QoSOptionsCreator : IQoSOptionsCreator
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        public QoSOptions Create(FileReRoute fileReRoute)
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            return new QoSOptionsBuilder()
 | 
				
			||||||
 | 
					                .WithExceptionsAllowedBeforeBreaking(fileReRoute.QoSOptions.ExceptionsAllowedBeforeBreaking)
 | 
				
			||||||
 | 
					                .WithDurationOfBreak(fileReRoute.QoSOptions.DurationOfBreak)
 | 
				
			||||||
 | 
					                .WithTimeoutValue(fileReRoute.QoSOptions.TimeoutValue)
 | 
				
			||||||
 | 
					                .Build();
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -65,6 +65,7 @@ namespace Ocelot.DependencyInjection
 | 
				
			|||||||
            services.AddSingleton<IUpstreamTemplatePatternCreator, UpstreamTemplatePatternCreator>();
 | 
					            services.AddSingleton<IUpstreamTemplatePatternCreator, UpstreamTemplatePatternCreator>();
 | 
				
			||||||
            services.AddSingleton<IRequestIdKeyCreator, RequestIdKeyCreator>();
 | 
					            services.AddSingleton<IRequestIdKeyCreator, RequestIdKeyCreator>();
 | 
				
			||||||
            services.AddSingleton<IServiceProviderConfigurationCreator,ServiceProviderConfigurationCreator>();
 | 
					            services.AddSingleton<IServiceProviderConfigurationCreator,ServiceProviderConfigurationCreator>();
 | 
				
			||||||
 | 
					            services.AddSingleton<IQoSOptionsCreator, QoSOptionsCreator>();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            var identityServerConfiguration = IdentityServerConfigurationCreator.GetIdentityServerConfiguration();
 | 
					            var identityServerConfiguration = IdentityServerConfigurationCreator.GetIdentityServerConfiguration();
 | 
				
			||||||
            
 | 
					            
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -35,6 +35,7 @@ namespace Ocelot.UnitTests.Configuration
 | 
				
			|||||||
        private Mock<IUpstreamTemplatePatternCreator> _upstreamTemplatePatternCreator;
 | 
					        private Mock<IUpstreamTemplatePatternCreator> _upstreamTemplatePatternCreator;
 | 
				
			||||||
        private Mock<IRequestIdKeyCreator> _requestIdKeyCreator;
 | 
					        private Mock<IRequestIdKeyCreator> _requestIdKeyCreator;
 | 
				
			||||||
        private Mock<IServiceProviderConfigurationCreator> _serviceProviderConfigCreator;
 | 
					        private Mock<IServiceProviderConfigurationCreator> _serviceProviderConfigCreator;
 | 
				
			||||||
 | 
					        private Mock<IQoSOptionsCreator> _qosOptionsCreator;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        public FileConfigurationCreatorTests()
 | 
					        public FileConfigurationCreatorTests()
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
@@ -52,13 +53,50 @@ namespace Ocelot.UnitTests.Configuration
 | 
				
			|||||||
            _upstreamTemplatePatternCreator = new Mock<IUpstreamTemplatePatternCreator>();
 | 
					            _upstreamTemplatePatternCreator = new Mock<IUpstreamTemplatePatternCreator>();
 | 
				
			||||||
            _requestIdKeyCreator = new Mock<IRequestIdKeyCreator>();
 | 
					            _requestIdKeyCreator = new Mock<IRequestIdKeyCreator>();
 | 
				
			||||||
            _serviceProviderConfigCreator = new Mock<IServiceProviderConfigurationCreator>();
 | 
					            _serviceProviderConfigCreator = new Mock<IServiceProviderConfigurationCreator>();
 | 
				
			||||||
 | 
					            _qosOptionsCreator = new Mock<IQoSOptionsCreator>();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            _ocelotConfigurationCreator = new FileOcelotConfigurationCreator( 
 | 
					            _ocelotConfigurationCreator = new FileOcelotConfigurationCreator( 
 | 
				
			||||||
                _fileConfig.Object, _validator.Object, _logger.Object,
 | 
					                _fileConfig.Object, _validator.Object, _logger.Object,
 | 
				
			||||||
                _loadBalancerFactory.Object, _loadBalancerHouse.Object, 
 | 
					                _loadBalancerFactory.Object, _loadBalancerHouse.Object, 
 | 
				
			||||||
                _qosProviderFactory.Object, _qosProviderHouse.Object, _claimsToThingCreator.Object,
 | 
					                _qosProviderFactory.Object, _qosProviderHouse.Object, _claimsToThingCreator.Object,
 | 
				
			||||||
                _authOptionsCreator.Object, _upstreamTemplatePatternCreator.Object, _requestIdKeyCreator.Object,
 | 
					                _authOptionsCreator.Object, _upstreamTemplatePatternCreator.Object, _requestIdKeyCreator.Object,
 | 
				
			||||||
                _serviceProviderConfigCreator.Object);
 | 
					                _serviceProviderConfigCreator.Object, _qosOptionsCreator.Object);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        [Fact]
 | 
				
			||||||
 | 
					        public void should_call_qos_options_creator()
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            var expected = new QoSOptionsBuilder()
 | 
				
			||||||
 | 
					                .WithDurationOfBreak(1)
 | 
				
			||||||
 | 
					                .WithExceptionsAllowedBeforeBreaking(1)
 | 
				
			||||||
 | 
					                .WithTimeoutValue(1)
 | 
				
			||||||
 | 
					                .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 = "Get",
 | 
				
			||||||
 | 
					                        QoSOptions = new FileQoSOptions
 | 
				
			||||||
 | 
					                        {
 | 
				
			||||||
 | 
					                            TimeoutValue = 1,
 | 
				
			||||||
 | 
					                            DurationOfBreak = 1,
 | 
				
			||||||
 | 
					                            ExceptionsAllowedBeforeBreaking = 1
 | 
				
			||||||
 | 
					                        }
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                },
 | 
				
			||||||
 | 
					            }))
 | 
				
			||||||
 | 
					                .And(x => x.GivenTheConfigIsValid())
 | 
				
			||||||
 | 
					                .And(x => x.GivenTheQosProviderFactoryReturns())
 | 
				
			||||||
 | 
					                .And(x => x.GivenTheQosOptionsCreatorReturns(expected))
 | 
				
			||||||
 | 
					                .When(x => x.WhenICreateTheConfig())
 | 
				
			||||||
 | 
					                .Then(x => x.ThenTheQosOptionsAre(expected))
 | 
				
			||||||
 | 
					                .BDDfy();
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        [Fact]
 | 
					        [Fact]
 | 
				
			||||||
@@ -585,5 +623,19 @@ namespace Ocelot.UnitTests.Configuration
 | 
				
			|||||||
                .Returns(requestId);
 | 
					                .Returns(requestId);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        private void GivenTheQosOptionsCreatorReturns(QoSOptions qosOptions)
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            _qosOptionsCreator
 | 
				
			||||||
 | 
					                .Setup(x => x.Create(_fileConfiguration.ReRoutes[0]))
 | 
				
			||||||
 | 
					                .Returns(qosOptions);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        private void ThenTheQosOptionsAre(QoSOptions qosOptions)
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            _config.Data.ReRoutes[0].QosOptions.DurationOfBreak.ShouldBe(qosOptions.DurationOfBreak);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            _config.Data.ReRoutes[0].QosOptions.ExceptionsAllowedBeforeBreaking.ShouldBe(qosOptions.ExceptionsAllowedBeforeBreaking);
 | 
				
			||||||
 | 
					            _config.Data.ReRoutes[0].QosOptions.TimeoutValue.ShouldBe(qosOptions.TimeoutValue);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,5 +1,3 @@
 | 
				
			|||||||
using System;
 | 
					 | 
				
			||||||
using System.Net;
 | 
					 | 
				
			||||||
using Microsoft.AspNetCore.Mvc;
 | 
					using Microsoft.AspNetCore.Mvc;
 | 
				
			||||||
using Moq;
 | 
					using Moq;
 | 
				
			||||||
using Ocelot.Configuration.File;
 | 
					using Ocelot.Configuration.File;
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user