コード例 #1
0
        public async Task <JsonRpcResult> ProcessAsync(string request)
        {
            if (_logger.IsTrace)
            {
                _logger.Trace($"Received JSON RPC request: {request}");
            }

            (JsonRpcRequest Model, IEnumerable <JsonRpcRequest> Collection)rpcRequest;
            try
            {
                rpcRequest = _jsonSerializer.DeserializeObjectOrArray <JsonRpcRequest>(request);
            }
            catch (Exception ex)
            {
                Metrics.JsonRpcRequestDeserializationFailures++;
                if (_logger.IsError)
                {
                    _logger.Error($"Error during parsing/validation, request: {request}", ex);
                }
                var response = _jsonRpcService.GetErrorResponse(ErrorType.ParseError, "Incorrect message");
                TraceResult(response);
                return(JsonRpcResult.Single(response));
            }

            if (rpcRequest.Model != null)
            {
                Metrics.JsonRpcRequests++;
                var response = await _jsonRpcService.SendRequestAsync(rpcRequest.Model);

                if (response.Error != null)
                {
                    if (_logger.IsError)
                    {
                        _logger.Error($"Failed to respond to {rpcRequest.Model.Method} {response.Error.Message}");
                    }
                    Metrics.JsonRpcErrors++;
                }
                else
                {
                    if (_logger.IsDebug)
                    {
                        _logger.Debug($"Responded to {rpcRequest.Model.Method}");
                    }
                    Metrics.JsonRpcSuccesses++;
                }

                TraceResult(response);
                return(JsonRpcResult.Single(response));
            }

            if (rpcRequest.Collection != null)
            {
                var responses = new List <JsonRpcResponse>();
                foreach (var jsonRpcRequest in rpcRequest.Collection)
                {
                    Metrics.JsonRpcRequests++;
                    var response = await _jsonRpcService.SendRequestAsync(jsonRpcRequest);

                    if (response.Error != null)
                    {
                        if (_logger.IsError)
                        {
                            _logger.Error($"Failed to respond to {jsonRpcRequest.Method} {response.Error.Message}");
                        }
                        Metrics.JsonRpcErrors++;
                    }
                    else
                    {
                        if (_logger.IsDebug)
                        {
                            _logger.Debug($"Responded to {jsonRpcRequest.Method}");
                        }
                        Metrics.JsonRpcSuccesses++;
                    }

                    responses.Add(response);
                }

                TraceResult(responses.ToArray());
                return(JsonRpcResult.Collection(responses));
            }

            Metrics.JsonRpcInvalidRequests++;
            var errorResponse = _jsonRpcService.GetErrorResponse(ErrorType.InvalidRequest, "Invalid request");

            TraceResult(errorResponse);
            return(JsonRpcResult.Single(errorResponse));
        }
コード例 #2
0
        public async Task <JsonRpcResult> ProcessAsync(string request)
        {
            Stopwatch stopwatch = Stopwatch.StartNew();

            (JsonRpcRequest Model, List <JsonRpcRequest> Collection)rpcRequest;
            try
            {
                rpcRequest = _jsonSerializer.DeserializeObjectOrArray <JsonRpcRequest>(request);
            }
            catch (Exception ex)
            {
                Metrics.JsonRpcRequestDeserializationFailures++;
                if (_logger.IsError)
                {
                    _logger.Error($"Error during parsing/validation, request: {request}", ex);
                }
                JsonRpcResponse response = _jsonRpcService.GetErrorResponse(ErrorType.ParseError, "Incorrect message");
                TraceResult(response);
                return(JsonRpcResult.Single(response));
            }

            if (rpcRequest.Model != null)
            {
                if (_logger.IsDebug)
                {
                    _logger.Debug($"JSON RPC request {rpcRequest.Model.Method}");
                }

                Metrics.JsonRpcRequests++;
                JsonRpcResponse response = await _jsonRpcService.SendRequestAsync(rpcRequest.Model);

                if (response.Error != null)
                {
                    if (_logger.IsError)
                    {
                        _logger.Error($"Failed to respond to {rpcRequest.Model.Method} {response.Error.Message}");
                    }
                    Metrics.JsonRpcErrors++;
                }
                else
                {
                    if (_logger.IsDebug)
                    {
                        _logger.Debug($"Responded to {rpcRequest.Model.Method}");
                    }
                    Metrics.JsonRpcSuccesses++;
                }

                TraceResult(response);
                stopwatch.Stop();
                if (_logger.IsDebug)
                {
                    _logger.Debug($"  {rpcRequest.Model.Method} handled in {stopwatch.Elapsed.TotalMilliseconds}ms");
                }
                return(JsonRpcResult.Single(response));
            }

            if (rpcRequest.Collection != null)
            {
                if (_logger.IsDebug)
                {
                    _logger.Debug($"{rpcRequest.Collection.Count} JSON RPC requests");
                }

                var       responses          = new List <JsonRpcResponse>();
                int       requestIndex       = 0;
                Stopwatch singleRequestWatch = new Stopwatch();
                foreach (JsonRpcRequest jsonRpcRequest in rpcRequest.Collection)
                {
                    singleRequestWatch.Start();

                    Metrics.JsonRpcRequests++;
                    JsonRpcResponse response = await _jsonRpcService.SendRequestAsync(jsonRpcRequest);

                    if (response.Error != null)
                    {
                        if (_logger.IsError)
                        {
                            _logger.Error($"Failed to respond to {jsonRpcRequest.Method} {response.Error.Message}");
                        }
                        Metrics.JsonRpcErrors++;
                    }
                    else
                    {
                        if (_logger.IsDebug)
                        {
                            _logger.Debug($"Responded to {jsonRpcRequest.Method}");
                        }
                        Metrics.JsonRpcSuccesses++;
                    }

                    singleRequestWatch.Stop();
                    if (_logger.IsDebug)
                    {
                        _logger.Debug($"  {requestIndex++}/{rpcRequest.Collection.Count} JSON RPC request - {jsonRpcRequest.Method} handled after {singleRequestWatch.Elapsed.TotalMilliseconds}");
                    }
                    responses.Add(response);
                }

                TraceResult(responses);
                stopwatch.Stop();
                if (_logger.IsDebug)
                {
                    _logger.Debug($"  {rpcRequest.Collection.Count} requests handled in {stopwatch.Elapsed.TotalMilliseconds}ms");
                }
                return(JsonRpcResult.Collection(responses));
            }

            Metrics.JsonRpcInvalidRequests++;
            JsonRpcResponse errorResponse = _jsonRpcService.GetErrorResponse(ErrorType.InvalidRequest, "Invalid request");

            TraceResult(errorResponse);
            stopwatch.Stop();
            if (_logger.IsDebug)
            {
                _logger.Debug($"  Failed request handled in {stopwatch.Elapsed.TotalMilliseconds}ms");
            }
            return(JsonRpcResult.Single(errorResponse));
        }