From ab9e859850eb6f02d6d7a45f5712ccdf7d459411 Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Wed, 31 Mar 2021 18:21:47 +1300 Subject: [PATCH 1/2] Move static Log classes out of generic types --- .../Internal/HttpContextStreamReader.cs | 8 +- .../Internal/ServerCallHandlerFactory.cs | 29 ++-- .../Internal/BinderServiceModelProvider.cs | 17 +-- .../Model/Internal/ServiceRouteBuilder.cs | 41 +++--- .../Internal/ClientStreamWriterBase.cs | 41 +++--- .../Internal/HttpContentClientStreamReader.cs | 21 ++- .../Internal/HttpContentClientStreamWriter.cs | 1 + .../Internal/Retry/HedgingCall.cs | 1 + .../Internal/Retry/RetryCall.cs | 1 + .../Internal/Retry/RetryCallBase.Log.cs | 128 ------------------ .../Internal/Retry/RetryCallBase.cs | 1 + .../Retry/RetryCallBaseClientStreamWriter.cs | 1 + .../Internal/Retry/RetryCallBaseLog.cs | 123 +++++++++++++++++ src/Shared/CommonGrpcProtocolHelpers.cs | 4 + 14 files changed, 212 insertions(+), 205 deletions(-) delete mode 100644 src/Grpc.Net.Client/Internal/Retry/RetryCallBase.Log.cs create mode 100644 src/Grpc.Net.Client/Internal/Retry/RetryCallBaseLog.cs diff --git a/src/Grpc.AspNetCore.Server/Internal/HttpContextStreamReader.cs b/src/Grpc.AspNetCore.Server/Internal/HttpContextStreamReader.cs index ce256cf9f..8153adce1 100644 --- a/src/Grpc.AspNetCore.Server/Internal/HttpContextStreamReader.cs +++ b/src/Grpc.AspNetCore.Server/Internal/HttpContextStreamReader.cs @@ -20,14 +20,12 @@ using System.Threading; using System.Threading.Tasks; using Grpc.Core; +using Grpc.Shared; namespace Grpc.AspNetCore.Server.Internal { internal class HttpContextStreamReader : IAsyncStreamReader where TRequest : class { - private static readonly Task True = Task.FromResult(true); - private static readonly Task False = Task.FromResult(false); - private readonly HttpContextServerCallContext _serverCallContext; private readonly Func _deserializer; private bool _completed; @@ -69,7 +67,9 @@ async Task MoveNextAsync(ValueTask readStreamTask) return MoveNextAsync(request); } - return ProcessPayload(request.Result) ? True : False; + return ProcessPayload(request.Result) + ? CommonGrpcProtocolHelpers.TrueTask + : CommonGrpcProtocolHelpers.FalseTask; } private bool ProcessPayload(TRequest? request) diff --git a/src/Grpc.AspNetCore.Server/Internal/ServerCallHandlerFactory.cs b/src/Grpc.AspNetCore.Server/Internal/ServerCallHandlerFactory.cs index 5801078ef..5f46895dd 100644 --- a/src/Grpc.AspNetCore.Server/Internal/ServerCallHandlerFactory.cs +++ b/src/Grpc.AspNetCore.Server/Internal/ServerCallHandlerFactory.cs @@ -25,6 +25,7 @@ using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; +using Log = Grpc.AspNetCore.Server.Internal.ServerCallHandlerFactoryLog; namespace Grpc.AspNetCore.Server.Internal { @@ -148,24 +149,24 @@ public RequestDelegate CreateUnimplementedService() return Task.CompletedTask; }; } + } - private static class Log - { - private static readonly Action _serviceUnimplemented = - LoggerMessage.Define(LogLevel.Information, new EventId(1, "ServiceUnimplemented"), "Service '{ServiceName}' is unimplemented."); + internal static class ServerCallHandlerFactoryLog + { + private static readonly Action _serviceUnimplemented = + LoggerMessage.Define(LogLevel.Information, new EventId(1, "ServiceUnimplemented"), "Service '{ServiceName}' is unimplemented."); - private static readonly Action _methodUnimplemented = - LoggerMessage.Define(LogLevel.Information, new EventId(2, "MethodUnimplemented"), "Method '{MethodName}' is unimplemented."); + private static readonly Action _methodUnimplemented = + LoggerMessage.Define(LogLevel.Information, new EventId(2, "MethodUnimplemented"), "Method '{MethodName}' is unimplemented."); - public static void ServiceUnimplemented(ILogger logger, string serviceName) - { - _serviceUnimplemented(logger, serviceName, null); - } + public static void ServiceUnimplemented(ILogger logger, string serviceName) + { + _serviceUnimplemented(logger, serviceName, null); + } - public static void MethodUnimplemented(ILogger logger, string methodName) - { - _methodUnimplemented(logger, methodName, null); - } + public static void MethodUnimplemented(ILogger logger, string methodName) + { + _methodUnimplemented(logger, methodName, null); } } } diff --git a/src/Grpc.AspNetCore.Server/Model/Internal/BinderServiceModelProvider.cs b/src/Grpc.AspNetCore.Server/Model/Internal/BinderServiceModelProvider.cs index de3b49945..169e67d85 100644 --- a/src/Grpc.AspNetCore.Server/Model/Internal/BinderServiceModelProvider.cs +++ b/src/Grpc.AspNetCore.Server/Model/Internal/BinderServiceModelProvider.cs @@ -20,6 +20,7 @@ using System.Diagnostics.CodeAnalysis; using Grpc.Shared.Server; using Microsoft.Extensions.Logging; +using Log = Grpc.AspNetCore.Server.Model.Internal.BinderServiceMethodProviderLog; namespace Grpc.AspNetCore.Server.Model.Internal { @@ -62,16 +63,16 @@ public void OnServiceMethodDiscovery(ServiceMethodProviderContext cont Log.BindMethodNotFound(_logger, typeof(TService)); } } + } - private static class Log - { - private static readonly Action _bindMethodNotFound = - LoggerMessage.Define(LogLevel.Debug, new EventId(1, "BindMethodNotFound"), "Could not find bind method for {ServiceType}."); + internal static class BinderServiceMethodProviderLog + { + private static readonly Action _bindMethodNotFound = + LoggerMessage.Define(LogLevel.Debug, new EventId(1, "BindMethodNotFound"), "Could not find bind method for {ServiceType}."); - public static void BindMethodNotFound(ILogger logger, Type serviceType) - { - _bindMethodNotFound(logger, serviceType, null); - } + public static void BindMethodNotFound(ILogger logger, Type serviceType) + { + _bindMethodNotFound(logger, serviceType, null); } } } diff --git a/src/Grpc.AspNetCore.Server/Model/Internal/ServiceRouteBuilder.cs b/src/Grpc.AspNetCore.Server/Model/Internal/ServiceRouteBuilder.cs index c13813ebe..2db7f63d1 100644 --- a/src/Grpc.AspNetCore.Server/Model/Internal/ServiceRouteBuilder.cs +++ b/src/Grpc.AspNetCore.Server/Model/Internal/ServiceRouteBuilder.cs @@ -27,6 +27,7 @@ using Microsoft.AspNetCore.Routing; using Microsoft.AspNetCore.Routing.Patterns; using Microsoft.Extensions.Logging; +using Log = Grpc.AspNetCore.Server.Model.Internal.ServiceRouteBuilderLog; namespace Grpc.AspNetCore.Server.Model.Internal { @@ -184,32 +185,32 @@ private GrpcUnimplementedConstraint() { } } + } - private static class Log - { - private static readonly Action _addedServiceMethod = - LoggerMessage.Define(LogLevel.Trace, new EventId(1, "AddedServiceMethod"), "Added gRPC method '{MethodName}' to service '{ServiceName}'. Method type: '{MethodType}', route pattern: '{RoutePattern}'."); + internal static class ServiceRouteBuilderLog + { + private static readonly Action _addedServiceMethod = + LoggerMessage.Define(LogLevel.Trace, new EventId(1, "AddedServiceMethod"), "Added gRPC method '{MethodName}' to service '{ServiceName}'. Method type: '{MethodType}', route pattern: '{RoutePattern}'."); - private static readonly Action _discoveringServiceMethods = - LoggerMessage.Define(LogLevel.Trace, new EventId(2, "DiscoveringServiceMethods"), "Discovering gRPC methods for {ServiceType}."); + private static readonly Action _discoveringServiceMethods = + LoggerMessage.Define(LogLevel.Trace, new EventId(2, "DiscoveringServiceMethods"), "Discovering gRPC methods for {ServiceType}."); - private static readonly Action _noServiceMethodsDiscovered = - LoggerMessage.Define(LogLevel.Debug, new EventId(3, "NoServiceMethodsDiscovered"), "No gRPC methods discovered for {ServiceType}."); + private static readonly Action _noServiceMethodsDiscovered = + LoggerMessage.Define(LogLevel.Debug, new EventId(3, "NoServiceMethodsDiscovered"), "No gRPC methods discovered for {ServiceType}."); - public static void AddedServiceMethod(ILogger logger, string methodName, string serviceName, MethodType methodType, string routePattern) - { - _addedServiceMethod(logger, methodName, serviceName, methodType, routePattern, null); - } + public static void AddedServiceMethod(ILogger logger, string methodName, string serviceName, MethodType methodType, string routePattern) + { + _addedServiceMethod(logger, methodName, serviceName, methodType, routePattern, null); + } - public static void DiscoveringServiceMethods(ILogger logger, Type serviceType) - { - _discoveringServiceMethods(logger, serviceType, null); - } + public static void DiscoveringServiceMethods(ILogger logger, Type serviceType) + { + _discoveringServiceMethods(logger, serviceType, null); + } - public static void NoServiceMethodsDiscovered(ILogger logger, Type serviceType) - { - _noServiceMethodsDiscovered(logger, serviceType, null); - } + public static void NoServiceMethodsDiscovered(ILogger logger, Type serviceType) + { + _noServiceMethodsDiscovered(logger, serviceType, null); } } } diff --git a/src/Grpc.Net.Client/Internal/ClientStreamWriterBase.cs b/src/Grpc.Net.Client/Internal/ClientStreamWriterBase.cs index dd48d2765..143c85754 100644 --- a/src/Grpc.Net.Client/Internal/ClientStreamWriterBase.cs +++ b/src/Grpc.Net.Client/Internal/ClientStreamWriterBase.cs @@ -22,6 +22,7 @@ using System.Threading.Tasks; using Grpc.Core; using Microsoft.Extensions.Logging; +using Log = Grpc.Net.Client.Internal.ClientStreamWriterBaseLog; namespace Grpc.Net.Client.Internal { @@ -69,32 +70,32 @@ protected bool IsWriteInProgressUnsynchronized return writeTask != null && !writeTask.IsCompleted; } } + } - protected static class Log - { - private static readonly Action _completingClientStream = - LoggerMessage.Define(LogLevel.Debug, new EventId(1, "CompletingClientStream"), "Completing client stream."); + internal static class ClientStreamWriterBaseLog + { + private static readonly Action _completingClientStream = + LoggerMessage.Define(LogLevel.Debug, new EventId(1, "CompletingClientStream"), "Completing client stream."); - private static readonly Action _writeMessageError = - LoggerMessage.Define(LogLevel.Error, new EventId(2, "WriteMessageError"), "Error writing message."); + private static readonly Action _writeMessageError = + LoggerMessage.Define(LogLevel.Error, new EventId(2, "WriteMessageError"), "Error writing message."); - private static readonly Action _completeClientStreamError = - LoggerMessage.Define(LogLevel.Error, new EventId(3, "CompleteClientStreamError"), "Error completing client stream."); + private static readonly Action _completeClientStreamError = + LoggerMessage.Define(LogLevel.Error, new EventId(3, "CompleteClientStreamError"), "Error completing client stream."); - public static void CompletingClientStream(ILogger logger) - { - _completingClientStream(logger, null); - } + public static void CompletingClientStream(ILogger logger) + { + _completingClientStream(logger, null); + } - public static void WriteMessageError(ILogger logger, Exception ex) - { - _writeMessageError(logger, ex); - } + public static void WriteMessageError(ILogger logger, Exception ex) + { + _writeMessageError(logger, ex); + } - public static void CompleteClientStreamError(ILogger logger, Exception ex) - { - _completeClientStreamError(logger, ex); - } + public static void CompleteClientStreamError(ILogger logger, Exception ex) + { + _completeClientStreamError(logger, ex); } } } diff --git a/src/Grpc.Net.Client/Internal/HttpContentClientStreamReader.cs b/src/Grpc.Net.Client/Internal/HttpContentClientStreamReader.cs index 88315d8f6..27ac5bd3e 100644 --- a/src/Grpc.Net.Client/Internal/HttpContentClientStreamReader.cs +++ b/src/Grpc.Net.Client/Internal/HttpContentClientStreamReader.cs @@ -25,6 +25,7 @@ using Grpc.Core; using Grpc.Shared; using Microsoft.Extensions.Logging; +using Log = Grpc.Net.Client.Internal.HttpContentClientStreamReaderLog; namespace Grpc.Net.Client.Internal { @@ -35,8 +36,6 @@ internal class HttpContentClientStreamReader : IAsyncStream // Getting logger name from generic type is slow. Cached copy. private const string LoggerName = "Grpc.Net.Client.Internal.HttpContentClientStreamReader"; - private static readonly Task FinishedTask = Task.FromResult(false); - private readonly GrpcCall _call; private readonly ILogger _logger; private readonly object _moveNextLock; @@ -90,7 +89,7 @@ public Task MoveNext(CancellationToken cancellationToken) if (status.StatusCode == StatusCode.OK) { // Response is finished and it was successful so just return false - return FinishedTask; + return CommonGrpcProtocolHelpers.FalseTask; } else { @@ -247,16 +246,16 @@ private bool IsMoveNextInProgressUnsynchronized return moveNextTask != null && !moveNextTask.IsCompleted; } } + } - private static class Log - { - private static readonly Action _readMessageError = - LoggerMessage.Define(LogLevel.Error, new EventId(1, "ReadMessageError"), "Error reading message."); + internal static class HttpContentClientStreamReaderLog + { + private static readonly Action _readMessageError = + LoggerMessage.Define(LogLevel.Error, new EventId(1, "ReadMessageError"), "Error reading message."); - public static void ReadMessageError(ILogger logger, Exception ex) - { - _readMessageError(logger, ex); - } + public static void ReadMessageError(ILogger logger, Exception ex) + { + _readMessageError(logger, ex); } } } diff --git a/src/Grpc.Net.Client/Internal/HttpContentClientStreamWriter.cs b/src/Grpc.Net.Client/Internal/HttpContentClientStreamWriter.cs index 62587ca67..ebb57e25c 100644 --- a/src/Grpc.Net.Client/Internal/HttpContentClientStreamWriter.cs +++ b/src/Grpc.Net.Client/Internal/HttpContentClientStreamWriter.cs @@ -21,6 +21,7 @@ using System.Threading.Tasks; using Grpc.Core; using Grpc.Shared; +using Log = Grpc.Net.Client.Internal.ClientStreamWriterBaseLog; #if NETSTANDARD2_0 using ValueTask = System.Threading.Tasks.Task; diff --git a/src/Grpc.Net.Client/Internal/Retry/HedgingCall.cs b/src/Grpc.Net.Client/Internal/Retry/HedgingCall.cs index 737ef3a76..8b3026c2b 100644 --- a/src/Grpc.Net.Client/Internal/Retry/HedgingCall.cs +++ b/src/Grpc.Net.Client/Internal/Retry/HedgingCall.cs @@ -24,6 +24,7 @@ using System.Threading.Tasks; using Grpc.Core; using Grpc.Shared; +using Log = Grpc.Net.Client.Internal.Retry.RetryCallBaseLog; namespace Grpc.Net.Client.Internal.Retry { diff --git a/src/Grpc.Net.Client/Internal/Retry/RetryCall.cs b/src/Grpc.Net.Client/Internal/Retry/RetryCall.cs index 4b843a08c..0acad90c0 100644 --- a/src/Grpc.Net.Client/Internal/Retry/RetryCall.cs +++ b/src/Grpc.Net.Client/Internal/Retry/RetryCall.cs @@ -23,6 +23,7 @@ using System.Threading.Tasks; using Grpc.Core; using Grpc.Shared; +using Log = Grpc.Net.Client.Internal.Retry.RetryCallBaseLog; namespace Grpc.Net.Client.Internal.Retry { diff --git a/src/Grpc.Net.Client/Internal/Retry/RetryCallBase.Log.cs b/src/Grpc.Net.Client/Internal/Retry/RetryCallBase.Log.cs deleted file mode 100644 index 1b5c6922e..000000000 --- a/src/Grpc.Net.Client/Internal/Retry/RetryCallBase.Log.cs +++ /dev/null @@ -1,128 +0,0 @@ -#region Copyright notice and license - -// Copyright 2019 The gRPC Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#endregion - -using System; -using Grpc.Core; -using Microsoft.Extensions.Logging; - -namespace Grpc.Net.Client.Internal.Retry -{ - internal partial class RetryCallBase : IGrpcCall - where TRequest : class - where TResponse : class - { - protected static class Log - { - private static readonly Action _retryEvaluated = - LoggerMessage.Define(LogLevel.Debug, new EventId(1, "RetryEvaluated"), "Evaluated retry for failed gRPC call. Status code: '{StatusCode}', Attempt: {AttemptCount}, Retry: {WillRetry}"); - - private static readonly Action _retryPushbackReceived = - LoggerMessage.Define(LogLevel.Debug, new EventId(2, "RetryPushbackReceived"), "Retry pushback of '{RetryPushback}' received from the failed gRPC call."); - - private static readonly Action _startingRetryDelay = - LoggerMessage.Define(LogLevel.Trace, new EventId(3, "StartingRetryDelay"), "Starting retry delay of {DelayDuration}."); - - private static readonly Action _errorRetryingCall = - LoggerMessage.Define(LogLevel.Error, new EventId(4, "ErrorRetryingCall"), "Error retrying gRPC call."); - - private static readonly Action _sendingBufferedMessages = - LoggerMessage.Define(LogLevel.Trace, new EventId(5, "SendingBufferedMessages"), "Sending {MessageCount} buffered messages from previous failed gRPC calls."); - - private static readonly Action _messageAddedToBuffer = - LoggerMessage.Define(LogLevel.Trace, new EventId(6, "MessageAddedToBuffer"), "Message with {MessageSize} bytes added to the buffer. There are {CallBufferSize} bytes buffered for this call."); - - private static readonly Action _callCommited = - LoggerMessage.Define(LogLevel.Debug, new EventId(7, "CallCommited"), "Call commited. Reason: {CommitReason}"); - - private static readonly Action _startingRetryWorker = - LoggerMessage.Define(LogLevel.Trace, new EventId(8, "StartingRetryWorker"), "Starting retry worker."); - - private static readonly Action _stoppingRetryWorker = - LoggerMessage.Define(LogLevel.Trace, new EventId(9, "StoppingRetryWorker"), "Stopping retry worker."); - - private static readonly Action _maxAttemptsLimited = - LoggerMessage.Define(LogLevel.Debug, new EventId(10, "MaxAttemptsLimited"), "The method has {ServiceConfigMaxAttempts} attempts specified in the service config. The number of attempts has been limited by channel configuration to {ChannelMaxAttempts}."); - - private static readonly Action _additionalCallsBlockedByRetryThrottling = - LoggerMessage.Define(LogLevel.Debug, new EventId(11, "AdditionalCallsBlockedByRetryThrottling"), "Additional calls blocked by retry throttling."); - - private static readonly Action _startingAttempt = - LoggerMessage.Define(LogLevel.Debug, new EventId(12, "StartingAttempt"), "Starting attempt {AttemptCount}."); - - internal static void RetryEvaluated(ILogger logger, StatusCode statusCode, int attemptCount, bool willRetry) - { - _retryEvaluated(logger, statusCode, attemptCount, willRetry, null); - } - - internal static void RetryPushbackReceived(ILogger logger, string retryPushback) - { - _retryPushbackReceived(logger, retryPushback, null); - } - - internal static void StartingRetryDelay(ILogger logger, TimeSpan delayDuration) - { - _startingRetryDelay(logger, delayDuration, null); - } - - internal static void ErrorRetryingCall(ILogger logger, Exception ex) - { - _errorRetryingCall(logger, ex); - } - - internal static void SendingBufferedMessages(ILogger logger, int messageCount) - { - _sendingBufferedMessages(logger, messageCount, null); - } - - internal static void MessageAddedToBuffer(ILogger logger, int messageSize, long callBufferSize) - { - _messageAddedToBuffer(logger, messageSize, callBufferSize, null); - } - - internal static void CallCommited(ILogger logger, CommitReason commitReason) - { - _callCommited(logger, commitReason, null); - } - - internal static void StartingRetryWorker(ILogger logger) - { - _startingRetryWorker(logger, null); - } - - internal static void StoppingRetryWorker(ILogger logger) - { - _stoppingRetryWorker(logger, null); - } - - internal static void MaxAttemptsLimited(ILogger logger, int serviceConfigMaxAttempts, int channelMaxAttempts) - { - _maxAttemptsLimited(logger, serviceConfigMaxAttempts, channelMaxAttempts, null); - } - - internal static void AdditionalCallsBlockedByRetryThrottling(ILogger logger) - { - _additionalCallsBlockedByRetryThrottling(logger, null); - } - - internal static void StartingAttempt(ILogger logger, int attempts) - { - _startingAttempt(logger, attempts, null); - } - } - } -} diff --git a/src/Grpc.Net.Client/Internal/Retry/RetryCallBase.cs b/src/Grpc.Net.Client/Internal/Retry/RetryCallBase.cs index 80013a143..3e98177bc 100644 --- a/src/Grpc.Net.Client/Internal/Retry/RetryCallBase.cs +++ b/src/Grpc.Net.Client/Internal/Retry/RetryCallBase.cs @@ -28,6 +28,7 @@ using Grpc.Net.Client.Internal.Http; using Grpc.Shared; using Microsoft.Extensions.Logging; +using Log = Grpc.Net.Client.Internal.Retry.RetryCallBaseLog; #if NETSTANDARD2_0 using ValueTask = System.Threading.Tasks.Task; diff --git a/src/Grpc.Net.Client/Internal/Retry/RetryCallBaseClientStreamWriter.cs b/src/Grpc.Net.Client/Internal/Retry/RetryCallBaseClientStreamWriter.cs index 1f412c58f..1ae943303 100644 --- a/src/Grpc.Net.Client/Internal/Retry/RetryCallBaseClientStreamWriter.cs +++ b/src/Grpc.Net.Client/Internal/Retry/RetryCallBaseClientStreamWriter.cs @@ -21,6 +21,7 @@ using System.Threading; using System.Threading.Tasks; using Grpc.Core; +using Log = Grpc.Net.Client.Internal.ClientStreamWriterBaseLog; namespace Grpc.Net.Client.Internal.Retry { diff --git a/src/Grpc.Net.Client/Internal/Retry/RetryCallBaseLog.cs b/src/Grpc.Net.Client/Internal/Retry/RetryCallBaseLog.cs new file mode 100644 index 000000000..582abf091 --- /dev/null +++ b/src/Grpc.Net.Client/Internal/Retry/RetryCallBaseLog.cs @@ -0,0 +1,123 @@ +#region Copyright notice and license + +// Copyright 2019 The gRPC Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#endregion + +using System; +using Grpc.Core; +using Microsoft.Extensions.Logging; + +namespace Grpc.Net.Client.Internal.Retry +{ + internal static class RetryCallBaseLog + { + private static readonly Action _retryEvaluated = + LoggerMessage.Define(LogLevel.Debug, new EventId(1, "RetryEvaluated"), "Evaluated retry for failed gRPC call. Status code: '{StatusCode}', Attempt: {AttemptCount}, Retry: {WillRetry}"); + + private static readonly Action _retryPushbackReceived = + LoggerMessage.Define(LogLevel.Debug, new EventId(2, "RetryPushbackReceived"), "Retry pushback of '{RetryPushback}' received from the failed gRPC call."); + + private static readonly Action _startingRetryDelay = + LoggerMessage.Define(LogLevel.Trace, new EventId(3, "StartingRetryDelay"), "Starting retry delay of {DelayDuration}."); + + private static readonly Action _errorRetryingCall = + LoggerMessage.Define(LogLevel.Error, new EventId(4, "ErrorRetryingCall"), "Error retrying gRPC call."); + + private static readonly Action _sendingBufferedMessages = + LoggerMessage.Define(LogLevel.Trace, new EventId(5, "SendingBufferedMessages"), "Sending {MessageCount} buffered messages from previous failed gRPC calls."); + + private static readonly Action _messageAddedToBuffer = + LoggerMessage.Define(LogLevel.Trace, new EventId(6, "MessageAddedToBuffer"), "Message with {MessageSize} bytes added to the buffer. There are {CallBufferSize} bytes buffered for this call."); + + private static readonly Action _callCommited = + LoggerMessage.Define(LogLevel.Debug, new EventId(7, "CallCommited"), "Call commited. Reason: {CommitReason}"); + + private static readonly Action _startingRetryWorker = + LoggerMessage.Define(LogLevel.Trace, new EventId(8, "StartingRetryWorker"), "Starting retry worker."); + + private static readonly Action _stoppingRetryWorker = + LoggerMessage.Define(LogLevel.Trace, new EventId(9, "StoppingRetryWorker"), "Stopping retry worker."); + + private static readonly Action _maxAttemptsLimited = + LoggerMessage.Define(LogLevel.Debug, new EventId(10, "MaxAttemptsLimited"), "The method has {ServiceConfigMaxAttempts} attempts specified in the service config. The number of attempts has been limited by channel configuration to {ChannelMaxAttempts}."); + + private static readonly Action _additionalCallsBlockedByRetryThrottling = + LoggerMessage.Define(LogLevel.Debug, new EventId(11, "AdditionalCallsBlockedByRetryThrottling"), "Additional calls blocked by retry throttling."); + + private static readonly Action _startingAttempt = + LoggerMessage.Define(LogLevel.Debug, new EventId(12, "StartingAttempt"), "Starting attempt {AttemptCount}."); + + internal static void RetryEvaluated(ILogger logger, StatusCode statusCode, int attemptCount, bool willRetry) + { + _retryEvaluated(logger, statusCode, attemptCount, willRetry, null); + } + + internal static void RetryPushbackReceived(ILogger logger, string retryPushback) + { + _retryPushbackReceived(logger, retryPushback, null); + } + + internal static void StartingRetryDelay(ILogger logger, TimeSpan delayDuration) + { + _startingRetryDelay(logger, delayDuration, null); + } + + internal static void ErrorRetryingCall(ILogger logger, Exception ex) + { + _errorRetryingCall(logger, ex); + } + + internal static void SendingBufferedMessages(ILogger logger, int messageCount) + { + _sendingBufferedMessages(logger, messageCount, null); + } + + internal static void MessageAddedToBuffer(ILogger logger, int messageSize, long callBufferSize) + { + _messageAddedToBuffer(logger, messageSize, callBufferSize, null); + } + + internal static void CallCommited(ILogger logger, CommitReason commitReason) + { + _callCommited(logger, commitReason, null); + } + + internal static void StartingRetryWorker(ILogger logger) + { + _startingRetryWorker(logger, null); + } + + internal static void StoppingRetryWorker(ILogger logger) + { + _stoppingRetryWorker(logger, null); + } + + internal static void MaxAttemptsLimited(ILogger logger, int serviceConfigMaxAttempts, int channelMaxAttempts) + { + _maxAttemptsLimited(logger, serviceConfigMaxAttempts, channelMaxAttempts, null); + } + + internal static void AdditionalCallsBlockedByRetryThrottling(ILogger logger) + { + _additionalCallsBlockedByRetryThrottling(logger, null); + } + + internal static void StartingAttempt(ILogger logger, int attempts) + { + _startingAttempt(logger, attempts, null); + } + } +} diff --git a/src/Shared/CommonGrpcProtocolHelpers.cs b/src/Shared/CommonGrpcProtocolHelpers.cs index 25eccd23b..a254a6765 100644 --- a/src/Shared/CommonGrpcProtocolHelpers.cs +++ b/src/Shared/CommonGrpcProtocolHelpers.cs @@ -18,6 +18,7 @@ using System; using System.Text; +using System.Threading.Tasks; namespace Grpc.Shared { @@ -115,5 +116,8 @@ private static string BuildErrorMessage(Exception ex) return sb.ToString(); } + + public static readonly Task TrueTask = Task.FromResult(false); + public static readonly Task FalseTask = Task.FromResult(false); } } From 81425392bf0bc8c4ab71a0494f12bb80f522094c Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Wed, 31 Mar 2021 21:41:24 +1300 Subject: [PATCH 2/2] Fix --- src/Shared/CommonGrpcProtocolHelpers.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Shared/CommonGrpcProtocolHelpers.cs b/src/Shared/CommonGrpcProtocolHelpers.cs index a254a6765..6d85c1154 100644 --- a/src/Shared/CommonGrpcProtocolHelpers.cs +++ b/src/Shared/CommonGrpcProtocolHelpers.cs @@ -24,6 +24,9 @@ namespace Grpc.Shared { internal static class CommonGrpcProtocolHelpers { + public static readonly Task TrueTask = Task.FromResult(true); + public static readonly Task FalseTask = Task.FromResult(false); + // Timer and DateTime.UtcNow have a 14ms precision. Add a small delay when scheduling deadline // timer that tests if exceeded or not. This avoids rescheduling the deadline callback multiple // times when timer is triggered before DateTime.UtcNow reports the deadline has been exceeded. @@ -116,8 +119,5 @@ private static string BuildErrorMessage(Exception ex) return sb.ToString(); } - - public static readonly Task TrueTask = Task.FromResult(false); - public static readonly Task FalseTask = Task.FromResult(false); } }