Exemple #1
0
        public override async Task <FactorialReply> Factorial(FactorialRequest request, ServerCallContext context)
        {
            // Obtain the number of available threads in ThreadPool
            ThreadPool.GetAvailableThreads(out var availableThreads, out _);
            // The number of available threads is the example of Gauge metric
            // Send gauge metric to StatsD (using JustEat.StatsD nuget)
            _stats.Gauge(availableThreads, "GaugeAvailableThreads");

            // Increment a counter metric for incoming requests
            _stats.Increment("CountRequests");

            // The method _stats.Time() will calculate the time while the _semaphoreSlim.WaitAsync() were waiting
            // and send the metric to StatsD
            await _stats.Time("TimeWait", async f => await _semaphoreSlim.WaitAsync());

            try
            {
                // Again measure time length of calculation and send it to StatsD
                var result = await _stats.Time("TimeCalculation", async t => await CalculateFactorialAsync(request.Factor));

                // Increment a counter of processed requests
                _stats.Increment("CountProcessed");

                return(await Task.FromResult(new FactorialReply
                {
                    Result = result
                }));
            }
            finally
            {
                _semaphoreSlim.Release();
            }
        }
Exemple #2
0
        async Task <FactorialResponse> GetFactorial(FactorialRequest request)
        {
            var validationResult = this.Validate(request);

            if (!validationResult.IsValid)
            {
                _logger.LogInformation($"Failed to generate factorial for {request.Number}");
                return(new FactorialResponse {
                    Success = false, Errors = validationResult.FormattedErrors
                });
            }

            var result = new FactorialResponse {
                Success = true, Factorial = Factorial(request.Number)
            };
            await _logServiceClient.Post(new LogRequest { Message = $"Generated Factorial for {request.Number}: {result.Factorial}" });

            return(result);
        }
Exemple #3
0
        FactorialResponse GetFactorial(FactorialRequest request)
        {
            var validationResult = this.Validate(request);

            if (!validationResult.IsValid)
            {
                _logger.LogInformation($"Failed to generate factorial for {request.Number}");
                return(new FactorialResponse {
                    Success = false, Errors = validationResult.FormattedErrors
                });
            }

            var result = new FactorialResponse {
                Success = true, Factorial = Factorial(request.Number)
            };

            _logger.LogInformation($"Generated Factorial for {request.Number}: {result.Factorial}");

            return(result);
        }