Created
December 1, 2023 13:56
-
-
Save yanxurui/d257e0240c72fb4f34b9ee880cef2696 to your computer and use it in GitHub Desktop.
GrpcGreeter server code that compares kestrel vs http.sys and TCP/IP vs unix domain socket vs named pipe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
syntax = "proto3"; | |
option csharp_namespace = "GrpcGreeter"; | |
package greet; | |
// The greeting service definition. | |
service Greeter { | |
// Sends a greeting | |
rpc SayHello (HelloRequest) returns (HelloReply); | |
rpc Download(DownloadRequest) returns (DownloadReply); | |
} | |
// The request message containing the user's name. | |
message HelloRequest { | |
string name = 1; | |
} | |
// The response message containing the greetings. | |
message HelloReply { | |
string message = 1; | |
} | |
message DownloadRequest { | |
uint32 requestSize = 1; | |
} | |
message DownloadReply { | |
bytes body = 1; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Google.Protobuf; | |
using Grpc.Core; | |
using GrpcGreeter; | |
namespace GrpcGreeter.Services; | |
public class GreeterService : Greeter.GreeterBase | |
{ | |
private static Dictionary<uint, ByteString> responseMap; | |
static GreeterService() | |
{ | |
responseMap = new Dictionary<uint, ByteString>(); | |
for (uint size = 10; size <= 1000000; size *= 10) | |
{ | |
responseMap.Add(size, ByteString.CopyFrom(new byte[size])); | |
} | |
} | |
private readonly ILogger<GreeterService> _logger; | |
public GreeterService(ILogger<GreeterService> logger) | |
{ | |
_logger = logger; | |
} | |
public override Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context) | |
{ | |
return Task.FromResult(new HelloReply | |
{ | |
Message = "Hello " + request.Name | |
}); | |
} | |
public override Task<DownloadReply> Download(DownloadRequest request, ServerCallContext context) | |
{ | |
if (!responseMap.TryGetValue(request.RequestSize, out ByteString? byteString)) | |
{ | |
// 1 KB by default | |
byteString = responseMap[1000]; | |
} | |
return Task.FromResult(new DownloadReply | |
{ | |
Body = byteString | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using GrpcGreeter.Services; | |
using Microsoft.AspNetCore.Server.Kestrel.Core; | |
var builder = WebApplication.CreateBuilder(args); | |
bool useHttpSys = false; | |
if (args.Length > 0) | |
{ | |
useHttpSys = args[0] == "httpsys"; | |
} | |
if (useHttpSys) | |
{ | |
#pragma warning disable CA1416 // Validate platform compatibility | |
builder.WebHost.UseHttpSys(options => | |
{ | |
options.MaxRequestBodySize = 30_000_000; | |
options.UrlPrefixes.Add("https://*:5002"); | |
}); | |
#pragma warning restore CA1416 // Validate platform compatibility | |
} | |
else | |
{ | |
var socketPath = Path.Combine(Path.GetTempPath(), "greeter.tmp"); | |
var pipeName = "PipeGreeter"; | |
builder.WebHost.ConfigureKestrel(serverOptions => | |
{ | |
serverOptions.ListenAnyIP(5001, listenOptions => | |
{ | |
listenOptions.UseHttps(); | |
}); | |
serverOptions.ListenUnixSocket(socketPath, listenOptions => | |
{ | |
listenOptions.Protocols = HttpProtocols.Http2; | |
}); | |
serverOptions.ListenNamedPipe(pipeName, listenOptions => | |
{ | |
listenOptions.Protocols = HttpProtocols.Http2; | |
}); | |
}); | |
} | |
// Add services to the container. | |
builder.Services.AddGrpc(); | |
var app = builder.Build(); | |
// Configure the HTTP request pipeline. | |
app.MapGrpcService<GreeterService>(); | |
app.MapGet("/", () => "Communication with gRPC endpoints must be made through a gRPC client. To learn how to create a client, visit: https://go.microsoft.com/fwlink/?linkid=2086909"); | |
app.Run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment