Exemplo n.º 1
0
        public async Task <Response <ServiceHostAndPort> > Lease(DownstreamContext context)
        {
            var value = context.HttpContext.Request.Cookies[_key];

            if (!string.IsNullOrEmpty(value) && _stored.ContainsKey(value))
            {
                var cached = _stored[value];

                var updated = new StickySession(cached.HostAndPort, DateTime.UtcNow.AddMilliseconds(_expiryInMs));

                _stored[value] = updated;

                return(new OkResponse <ServiceHostAndPort>(updated.HostAndPort));
            }

            var next = await _loadBalancer.Lease(context);

            if (next.IsError)
            {
                return(new ErrorResponse <ServiceHostAndPort>(next.Errors));
            }

            if (!string.IsNullOrEmpty(value) && !_stored.ContainsKey(value))
            {
                _stored[value] = new StickySession(next.Data, DateTime.UtcNow.AddMilliseconds(_expiryInMs));
            }

            return(new OkResponse <ServiceHostAndPort>(next.Data));
        }
Exemplo n.º 2
0
        public async Task <Response <ServiceHostAndPort> > Lease(DownstreamContext context)
        {
            var key = context.HttpContext.Request.Cookies[_key];

            lock (_lock)
            {
                if (!string.IsNullOrEmpty(key) && _stored.ContainsKey(key))
                {
                    var cached = _stored[key];

                    var updated = new StickySession(cached.HostAndPort, DateTime.UtcNow.AddMilliseconds(_keyExpiryInMs), key);

                    _stored[key] = updated;

                    _bus.Publish(updated, _keyExpiryInMs);

                    return(new OkResponse <ServiceHostAndPort>(updated.HostAndPort));
                }
            }

            var next = await _loadBalancer.Lease(context);

            if (next.IsError)
            {
                return(new ErrorResponse <ServiceHostAndPort>(next.Errors));
            }

            lock (_lock)
            {
                if (!string.IsNullOrEmpty(key) && !_stored.ContainsKey(key))
                {
                    var ss = new StickySession(next.Data, DateTime.UtcNow.AddMilliseconds(_keyExpiryInMs), key);
                    _stored[key] = ss;
                    _bus.Publish(ss, _keyExpiryInMs);
                }
            }

            return(new OkResponse <ServiceHostAndPort>(next.Data));
        }