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); }
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; }
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); }); } }
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); }); }