Skip to content

Commit bce5852

Browse files
authored
Code clean up across solution (#1530)
1 parent 7d081d7 commit bce5852

File tree

97 files changed

+250
-191
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

97 files changed

+250
-191
lines changed

examples/Racer/Server/Services/RacerService.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
using System;
2020
using System.Diagnostics;
21+
using System.Globalization;
2122
using System.Linq;
2223
using System.Threading.Tasks;
2324
using Grpc.Core;
@@ -29,7 +30,7 @@ public class RacerService : Racer.RacerBase
2930
{
3031
public override async Task ReadySetGo(IAsyncStreamReader<RaceMessage> requestStream, IServerStreamWriter<RaceMessage> responseStream, ServerCallContext context)
3132
{
32-
var raceDuration = TimeSpan.Parse(context.RequestHeaders.Single(h => h.Key == "race-duration").Value);
33+
var raceDuration = TimeSpan.Parse(context.RequestHeaders.GetValue("race-duration"), CultureInfo.InvariantCulture);
3334

3435
// Read incoming messages in a background task
3536
RaceMessage? lastMessageReceived = null;

examples/Tester/Tests/UnitTests/Helpers/TestAsyncStreamReader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public async Task<bool> MoveNext(CancellationToken cancellationToken)
5454
{
5555
_serverCallContext.CancellationToken.ThrowIfCancellationRequested();
5656

57-
if (await _channel.Reader.WaitToReadAsync() &&
57+
if (await _channel.Reader.WaitToReadAsync(cancellationToken) &&
5858
_channel.Reader.TryRead(out var message))
5959
{
6060
Current = message;

examples/Worker/Client/Worker.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ public Worker(ILogger<Worker> logger, Counter.CounterClient counterClient)
4444
public override async Task StartAsync(CancellationToken cancellationToken)
4545
{
4646
_logger.LogInformation("Starting client streaming call at: {time}", DateTimeOffset.Now);
47-
_clientStreamingCall = _counterClient.AccumulateCount();
47+
// Don't pass cancellation token to the call. The call is completed in StopAsync when service stops.
48+
_clientStreamingCall = _counterClient.AccumulateCount(cancellationToken: CancellationToken.None);
4849

4950
await base.StartAsync(cancellationToken);
5051
}

perf/Grpc.AspNetCore.Microbenchmarks/Client/UnaryClientBenchmarkBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ protected virtual byte[] GetMessageData(HelloReply message)
7777
protected async Task InvokeSayHelloAsync(CallOptions options)
7878
{
7979
var response = await _client!.SayHelloAsync(new HelloRequest { Name = _content }, options).ResponseAsync;
80-
80+
8181
if (response.Message != _content)
8282
{
8383
throw new InvalidOperationException("Unexpected result.");

perf/Grpc.AspNetCore.Microbenchmarks/DefaultCoreConfigAttribute.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
namespace Grpc.AspNetCore.Microbenchmarks
2222
{
23+
[AttributeUsage(AttributeTargets.Assembly)]
2324
public class DefaultCoreConfigAttribute : Attribute, IConfigSource
2425
{
2526
public IConfig Config => new DefaultCoreConfig();

perf/benchmarkapps/GrpcAspNetCoreServer/Controllers/BenchmarkController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ namespace GrpcAspNetCoreServer.Controllers
2525
public class BenchmarkController : Controller
2626
{
2727
[HttpPost("unary")]
28-
public SimpleResponse Post([FromBody]SimpleRequest request)
28+
public SimpleResponse Post([FromBody] SimpleRequest request)
2929
{
3030
return BenchmarkServiceImpl.CreateResponse(request);
3131
}

perf/benchmarkapps/GrpcAspNetCoreServer/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public void Configure(IApplicationBuilder app, IHostApplicationLifetime applicat
100100
ConfigureAuthorization(endpoints.MapGrpcService<BenchmarkServiceImpl>());
101101

102102
ConfigureAuthorization(endpoints.MapControllers());
103-
103+
104104
ConfigureAuthorization(endpoints.MapGet("/", context =>
105105
{
106106
return context.Response.WriteAsync("Benchmark Server");

perf/benchmarkapps/GrpcClient/Program.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
using System.CommandLine;
2020
using System.CommandLine.Invocation;
2121
using System.Diagnostics;
22+
using System.Globalization;
2223
using System.Net.Security;
2324
using System.Net.Sockets;
2425
using System.Reflection;
@@ -43,17 +44,17 @@ class Program
4344
private static List<List<double>> _latencyPerConnection = null!;
4445
private static int _callsStarted;
4546
private static double _maxLatency;
46-
private static Stopwatch _workTimer = new Stopwatch();
47+
private static readonly Stopwatch _workTimer = new Stopwatch();
4748
private static volatile bool _warmingUp;
4849
private static volatile bool _stopped;
49-
private static SemaphoreSlim _lock = new SemaphoreSlim(1);
50+
private static readonly SemaphoreSlim _lock = new SemaphoreSlim(1);
5051
private static List<(double sum, int count)> _latencyAverage = null!;
5152
private static int _totalRequests;
5253
private static ClientOptions _options = null!;
5354
private static ILoggerFactory? _loggerFactory;
5455
private static SslCredentials? _credentials;
55-
private static StringBuilder _errorStringBuilder = new StringBuilder();
56-
private static CancellationTokenSource _cts = new CancellationTokenSource();
56+
private static readonly StringBuilder _errorStringBuilder = new StringBuilder();
57+
private static readonly CancellationTokenSource _cts = new CancellationTokenSource();
5758

5859
public static async Task<int> Main(string[] args)
5960
{
@@ -170,7 +171,7 @@ private static async Task StartScenario()
170171
var text = "Exception from test: " + ex.Message;
171172
Log(text);
172173
_errorStringBuilder.AppendLine();
173-
_errorStringBuilder.Append($"[{DateTime.Now:hh:mm:ss.fff}] {text}");
174+
_errorStringBuilder.Append(CultureInfo.InvariantCulture, $"[{DateTime.Now:hh:mm:ss.fff}] {text}");
174175
}
175176
}
176177

@@ -463,7 +464,7 @@ private static SslCredentials GetSslCredentials()
463464

464465
private static void Log(string message)
465466
{
466-
var time = DateTime.Now.ToString("hh:mm:ss.fff");
467+
var time = DateTime.Now.ToString("hh:mm:ss.fff", CultureInfo.InvariantCulture);
467468
Console.WriteLine($"[{time}] {message}");
468469
}
469470

perf/benchmarkapps/Shared/BenchmarkServiceImpl.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616

1717
#endregion
1818

19-
using Grpc.Testing;
20-
using Grpc.Core;
2119
using Google.Protobuf;
20+
using Grpc.Core;
21+
using Grpc.Testing;
2222

2323
class BenchmarkServiceImpl : BenchmarkService.BenchmarkServiceBase
2424
{

src/Grpc.AspNetCore.Server.ClientFactory/ContextPropagationInterceptor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ private ClientInterceptorContext<TRequest, TResponse> ConfigureContext<TRequest,
172172
return new ClientInterceptorContext<TRequest, TResponse>(context.Method, context.Host, options);
173173
}
174174

175-
private bool TryGetServerCallContext([NotNullWhen(true)]out ServerCallContext? serverCallContext, [NotNullWhen(false)]out string? errorMessage)
175+
private bool TryGetServerCallContext([NotNullWhen(true)] out ServerCallContext? serverCallContext, [NotNullWhen(false)] out string? errorMessage)
176176
{
177177
var httpContext = _httpContextAccessor.HttpContext;
178178
if (httpContext == null)

0 commit comments

Comments
 (0)