예제 #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);
                   });
        }