Skip to content

Commit

Permalink
Invert control flow for readability
Browse files Browse the repository at this point in the history
  • Loading branch information
OoLunar committed Sep 6, 2023
1 parent 0389f95 commit 6a2fc71
Showing 1 changed file with 25 additions and 24 deletions.
49 changes: 25 additions & 24 deletions src/HyperSharp/HyperServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,40 +146,41 @@ private async Task HandleConnectionAsync(TcpClient client)

// Start parsing the HTTP Headers.
Result<HyperContext> context = await HyperHeaderParser.TryParseHeadersAsync(Configuration.MaxHeaderSize, connection, cancellationTokenSource.Token);
if (cancellationTokenSource.IsCancellationRequested)
if (!cancellationTokenSource.IsCancellationRequested)
{
await context.Value!.RespondAsync(HyperStatus.InternalServerError(), Configuration.JsonSerializerOptions);
}
else if (context.IsSuccess)
{
// Execute any registered responders.
HyperLogging.HttpReceivedRequest(_logger, connection.Id, context.Value!.Route, null);
Result<HyperStatus> status = await Configuration.RespondersDelegate(context.Value, cancellationTokenSource.Token);
if (!context.Value.HasResponded)
if (context.IsSuccess)
{
HyperLogging.HttpResponding(_logger, connection.Id, status.Value, null);
HyperStatus response = status.Status switch
// Execute any registered responders.
HyperLogging.HttpReceivedRequest(_logger, connection.Id, context.Value!.Route, null);
Result<HyperStatus> status = await Configuration.RespondersDelegate(context.Value, cancellationTokenSource.Token);
if (!context.Value.HasResponded)
{
_ when cancellationTokenSource.IsCancellationRequested => HyperStatus.InternalServerError(),
ResultStatus.IsSuccess | ResultStatus.HasValue => status.Value,
ResultStatus.IsSuccess => HyperStatus.OK(),
ResultStatus.HasValue => HyperStatus.InternalServerError(status.Value.Headers, status.Value.Body),
ResultStatus.None => HyperStatus.InternalServerError(),
_ => throw new NotImplementedException("Unimplemented result status, please open a GitHub issue as this is a bug.")
};

await context.Value.RespondAsync(response, Configuration.JsonSerializerOptions, cancellationTokenSource.Token);
HyperLogging.HttpResponded(_logger, connection.Id, response, null);
HyperLogging.HttpResponding(_logger, connection.Id, status.Value, null);
HyperStatus response = status.Status switch
{
_ when cancellationTokenSource.IsCancellationRequested => HyperStatus.InternalServerError(),
ResultStatus.IsSuccess | ResultStatus.HasValue => status.Value,
ResultStatus.IsSuccess => HyperStatus.OK(),
ResultStatus.HasValue => HyperStatus.InternalServerError(status.Value.Headers, status.Value.Body),
ResultStatus.None => HyperStatus.InternalServerError(),
_ => throw new NotImplementedException("Unimplemented result status, please open a GitHub issue as this is a bug.")
};

await context.Value.RespondAsync(response, Configuration.JsonSerializerOptions, cancellationTokenSource.Token);
HyperLogging.HttpResponded(_logger, connection.Id, response, null);
}
}
else
{
HyperLogging.HttpInvalidHeaders(_logger, connection.Id, context.Errors, null);
}
}
else
{
HyperLogging.HttpInvalidHeaders(_logger, connection.Id, context.Errors, null);
await context.Value!.RespondAsync(HyperStatus.InternalServerError(), Configuration.JsonSerializerOptions);
}

HyperLogging.ConnectionClosing(_logger, connection.Id, null);
await connection.StreamReader.CompleteAsync();
await connection.StreamWriter.CompleteAsync();
connection.Dispose();

_openConnections.TryPop(out Task? _);
Expand Down

1 comment on commit 6a2fc71

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Machine Information:

BenchmarkDotNet v0.13.7, Void Linux

  • AMD EPYC 7282, 1 CPU, 4 logical and 4 physical cores
  • Hardware Intrinsics: AVX2, AES, BMI1, BMI2, FMA, LZCNT, PCLMUL, POPCNT VectorSize=256
  • .NET 8.0.0 (8.0.23.37506), x64, RyuJIT
  • Total Execution Time: 20m and 18.449s

HttpBenchmarks

Execution Time: 2m and 36.606s

HttpClientTestAsync:

Mean: 483.28μs
Error: 7.06μs
StdDev: 69.54μs
Max per second: 2,069.20 (1,000,000,000ns / 483,278.48ns)

ParseHeadersTestAsync:

Mean: 8.43μs
Error: 48ns
StdDev: 367ns
Max per second: 118,565.28 (1,000,000,000ns / 8,434.17ns)

AsyncResponderBenchmarks

Execution Time: 4m and 35.428s

DelegateExecutionTimeAsync:

Mean: 80ns
Error: 0ns
StdDev: 5ns
Max per second: 12,451,584.53 (1,000,000,000ns / 80.31ns)

DelegateExecutionTimeAsync:

Mean: 227ns
Error: 1ns
StdDev: 10ns
Max per second: 4,398,298.56 (1,000,000,000ns / 227.36ns)

DelegateExecutionTimeAsync:

Mean: 166ns
Error: 1ns
StdDev: 6ns
Max per second: 6,025,627.82 (1,000,000,000ns / 165.96ns)

DelegateExecutionTimeAsync:

Mean: 223ns
Error: 1ns
StdDev: 7ns
Max per second: 4,479,925.03 (1,000,000,000ns / 223.22ns)

ErrorBenchmarks

Execution Time: 4m and 54.532s

CreateError, Baseline:

Mean: 14ns
Error: 0ns
StdDev: 1ns
Max per second: 71,498,029.84 (1,000,000,000ns / 13.99ns)

CreateErrorWithMessage:

Mean: 12ns
Error: 0ns
StdDev: 1ns
Max per second: 81,523,919.09 (1,000,000,000ns / 12.27ns)

CreateErrorWithSubError:

Mean: 28ns
Error: 0ns
StdDev: 2ns
Max per second: 35,346,304.77 (1,000,000,000ns / 28.29ns)

CreateErrorWithSubErrors:

Mean: 84ns
Error: 1ns
StdDev: 5ns
Max per second: 11,860,291.74 (1,000,000,000ns / 84.31ns)

GenericResultBenchmarks

Execution Time: 3m and 22.551s

CreateGenericSuccessfulResult, Baseline:

Mean: 6ns
Error: 0ns
StdDev: 0ns
Max per second: 156,077,210.70 (1,000,000,000ns / 6.41ns)

CreateGenericFailedResult:

Mean: 24ns
Error: 0ns
StdDev: 1ns
Max per second: 40,931,407.63 (1,000,000,000ns / 24.43ns)

CreateGenericFailedResultWithMultipleErrors:

Mean: 63ns
Error: 0ns
StdDev: 4ns
Max per second: 15,821,283.04 (1,000,000,000ns / 63.21ns)

CreateGenericFailedResultWithValue:

Mean: 14ns
Error: 0ns
StdDev: 1ns
Max per second: 69,922,647.38 (1,000,000,000ns / 14.30ns)

CreateGenericSuccessfulResultWithValue:

Mean: 6ns
Error: 0ns
StdDev: 0ns
Max per second: 156,289,403.95 (1,000,000,000ns / 6.40ns)

ResponderBenchmarks

Execution Time: 4m and 49.918s

DelegateExecutionTime:

Mean: 33ns
Error: 0ns
StdDev: 1ns
Max per second: 30,213,327.42 (1,000,000,000ns / 33.10ns)

DelegateExecutionTime:

Mean: 50ns
Error: 0ns
StdDev: 2ns
Max per second: 20,097,916.98 (1,000,000,000ns / 49.76ns)

DelegateExecutionTime:

Mean: 62ns
Error: 0ns
StdDev: 3ns
Max per second: 16,248,392.33 (1,000,000,000ns / 61.54ns)

DelegateExecutionTime:

Mean: 80ns
Error: 0ns
StdDev: 3ns
Max per second: 12,555,979.40 (1,000,000,000ns / 79.64ns)

DelegateExecutionTime:

Mean: 61ns
Error: 0ns
StdDev: 3ns
Max per second: 16,440,889.85 (1,000,000,000ns / 60.82ns)

ResultBenchmarks

Execution Time: 4m and 59.414s

CreateSuccessfulResult, Baseline:

Mean: 0ns
Error: 0ns
StdDev: 0ns
Max per second: 16,897,063,350.12 (1,000,000,000ns / 0.06ns)

CreateFailedResult:

Mean: 29ns
Error: 0ns
StdDev: 2ns
Max per second: 34,959,226.12 (1,000,000,000ns / 28.60ns)

CreateFailedResultWithMultipleErrors:

Mean: 66ns
Error: 0ns
StdDev: 3ns
Max per second: 15,044,619.19 (1,000,000,000ns / 66.47ns)

CreateFailedResultWithValue:

Mean: 19ns
Error: 0ns
StdDev: 1ns
Max per second: 52,886,452.77 (1,000,000,000ns / 18.91ns)

CreateSuccessfulResultWithValue:

Mean: 4ns
Error: 0ns
StdDev: 0ns
Max per second: 279,442,127.69 (1,000,000,000ns / 3.58ns)

Please sign in to comment.