Exemplo n.º 1
0
        public Response <DownstreamRoute> Get(string upstreamUrlPath, string upstreamQueryString, string upstreamHttpMethod, IInternalConfiguration configuration, string upstreamHost)
        {
            var serviceName = GetServiceName(upstreamUrlPath);

            var downstreamPath = GetDownstreamPath(upstreamUrlPath);

            if (HasQueryString(downstreamPath))
            {
                downstreamPath = RemoveQueryString(downstreamPath);
            }

            var downstreamPathForKeys = $"/{serviceName}{downstreamPath}";

            var loadBalancerKey = CreateLoadBalancerKey(downstreamPathForKeys, upstreamHttpMethod, configuration.LoadBalancerOptions);

            if (_cache.TryGetValue(loadBalancerKey, out var downstreamRoute))
            {
                return(downstreamRoute);
            }

            var qosOptions = _qoSOptionsCreator.Create(configuration.QoSOptions, downstreamPathForKeys, new List <string> {
                upstreamHttpMethod
            });

            var upstreamPathTemplate = new UpstreamPathTemplateBuilder().WithOriginalValue(upstreamUrlPath).Build();

            var downstreamReRouteBuilder = new DownstreamReRouteBuilder()
                                           .WithServiceName(serviceName)
                                           .WithLoadBalancerKey(loadBalancerKey)
                                           .WithDownstreamPathTemplate(downstreamPath)
                                           .WithUseServiceDiscovery(true)
                                           .WithHttpHandlerOptions(configuration.HttpHandlerOptions)
                                           .WithQosOptions(qosOptions)
                                           .WithDownstreamScheme(configuration.DownstreamScheme)
                                           .WithLoadBalancerOptions(configuration.LoadBalancerOptions)
                                           .WithUpstreamPathTemplate(upstreamPathTemplate);

            var rateLimitOptions = configuration.ReRoutes != null
                ? configuration.ReRoutes
                                   .SelectMany(x => x.DownstreamReRoute)
                                   .FirstOrDefault(x => x.ServiceName == serviceName)
                : null;

            if (rateLimitOptions != null)
            {
                downstreamReRouteBuilder
                .WithRateLimitOptions(rateLimitOptions.RateLimitOptions)
                .WithEnableRateLimiting(true);
            }

            var downstreamReRoute = downstreamReRouteBuilder.Build();

            var reRoute = new ReRouteBuilder()
                          .WithDownstreamReRoute(downstreamReRoute)
                          .WithUpstreamHttpMethod(new List <string>()
            {
                upstreamHttpMethod
            })
                          .WithUpstreamPathTemplate(upstreamPathTemplate)
                          .Build();

            downstreamRoute = new OkResponse <DownstreamRoute>(new DownstreamRoute(new List <PlaceholderNameAndValue>(), reRoute));

            _cache.AddOrUpdate(loadBalancerKey, downstreamRoute, (x, y) => downstreamRoute);

            return(downstreamRoute);
        }
Exemplo n.º 2
0
 private void GivenSetDownstreamReRoute()
 {
     _downstreamContext.DownstreamReRoute = _downstreamReRouteBuilder.Build();
 }