示例#1
0
        public Task <ResponseData> Request(RequestData request, CancellationToken token)
        {
            this.EnsureIsReceiving();

            var requestId = this.NextId();

            var redisRequest      = new RedisRequest(this.receiveQueue, requestId, request);
            var redisRequestBytes = redisRequest.ToBinary();

            var redis     = this.connection.GetClient();
            var lpushTask = redis.ListLeftPushAsync(sendQueue, redisRequestBytes);

            var callback = new TaskCompletionSource <ResponseData>();

            this.requestCallbacks[requestId] = callback;

            if (token != CancellationToken.None)
            {
                token.Register(() =>
                {
                    this.OnRequestCancelled(requestId);
                });
            }

            lpushTask.ContinueWith(t =>
            {
                if (lpushTask.Exception != null)
                {
                    this.OnRequestError(requestId, lpushTask.Exception.InnerException);
                }
            });

            return(callback.Task);
        }
示例#2
0
        public Task<ResponseData> Request(RequestData request, CancellationToken token)
        {
            this.EnsureIsReceiving();

            var requestId = this.NextId();

            var redisRequest = new RedisRequest(this.receiveQueue, requestId, request);
            var redisRequestBytes = redisRequest.ToBinary();

            var redis = this.connection.GetClient();
            var lpushTask = redis.ListLeftPushAsync(sendQueue, redisRequestBytes);

            var callback = new TaskCompletionSource<ResponseData>();
            this.requestCallbacks[requestId] = callback;

            if (token != CancellationToken.None)
            {
                token.Register(() =>
                {
                    this.OnRequestCancelled(requestId);
                });
            }

            lpushTask.ContinueWith(t =>
            {
                if (lpushTask.Exception != null)
                {
                    this.OnRequestError(requestId, lpushTask.Exception.InnerException);
                }
            });

            return callback.Task;
        }
示例#3
0
        private async Task ReceiveRequests()
        {
            int delayTimeout = 1;

            byte[] rawRequest;

            var redis = this.connection.GetClient();

            while (Interlocked.Read(ref this.receiveState) == 1)
            {
                rawRequest = await redis.ListRightPopAsync(this.serviceQueue).IgnoreException(typeof(RedisException));

                if (rawRequest == null)
                {
                    await Task.Delay(delayTimeout);

                    //increase timeout until delayMaxTimeout
                    continue;
                }

                var redisRequestBytes = rawRequest;

                Task.Run(() =>
                {
                    var redisRequest = RedisRequest.FromBinary(redisRequestBytes);
                    this.OnRequest(redis, redisRequest);
                });
            }
        }
示例#4
0
        private void OnRequest(IDatabase redis, RedisRequest redisRequest)
        {
            var service = this.serviceFactory.CreateService(redisRequest.Request.Service);

            service.Process(redisRequest.Request)
            .ContinueWith(t =>
            {
                var response = t.Result;

                var redisResponse      = new RedisResponse(redisRequest.Id, response);
                var redisResponseBytes = redisResponse.ToBinary();

                redis.ListLeftPushAsync(redisRequest.ReceiveQueue, redisResponseBytes);
            });
        }
示例#5
0
        private void OnRequest(IDatabase redis, RedisRequest redisRequest)
        {
            var service = this.serviceFactory.CreateService(redisRequest.Request.Service);

            service.Process(redisRequest.Request)
                   .ContinueWith(t =>
                   {
                       var response = t.Result;

                       var redisResponse = new RedisResponse(redisRequest.Id, response);
                       var redisResponseBytes = redisResponse.ToBinary();

                       redis.ListLeftPushAsync(redisRequest.ReceiveQueue, redisResponseBytes);
                   });
        }