--- lang: ja-jp breaks: true --- # gRPC `ASP.NET Core` で キープアライブを使用する 2021-10-07 ## クライアント > キープ アライブ ping > https://docs.microsoft.com/ja-jp/aspnet/core/grpc/performance#keep-alive-pings ```csharp= // ping により、サーバーと使用中のすべてのプロキシが、非アクティブが理由で接続が閉じられることがないように保証されます。 var socketsHttpHandler = new SocketsHttpHandler { PooledConnectionIdleTimeout = Timeout.InfiniteTimeSpan, KeepAlivePingDelay = TimeSpan.FromSeconds(3), KeepAlivePingTimeout = TimeSpan.FromSeconds(3), KeepAlivePingPolicy = HttpKeepAlivePingPolicy.Always, EnableMultipleHttp2Connections = true }; var canelOptions = new GrpcChannelOptions { HttpHandler = socketsHttpHandler, }; GrpcChannel channel = GrpcChannel.ForAddress(url, canelOptions); ``` :::info クライアント側からのキープアライブは、通信経路のとあるネットワークが切断された場合、キープアライブによってエラーが発生するという事は特にない。 ネットワーク復旧後、`gRPC API`を実行すると何事もなかったように通信可能となる。 ※復旧後のキープアライブは、`gRPC API` 実行後に実行される。 ::: ## サーバ > How to set keepalive on the server Version 4.3.x #458 > https://github.com/Cysharp/MagicOnion/issues/458 > Keep-Alive タイムアウト > https://docs.microsoft.com/ja-jp/aspnet/core/fundamentals/servers/kestrel/options#keep-alive-timeout > HTTP/2 keep alive ping configuration > https://docs.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel/options#http2-keep-alive-ping-configuration > HTTP/2 キープ アライブ ping 構成 > https://docs.microsoft.com/ja-jp/aspnet/core/fundamentals/servers/kestrel/options#http2-keep-alive-ping-configuration > aspnetcore/src/Servers/Kestrel/Core/src/Http2Limits.cs https://github.com/dotnet/aspnetcore/blob/main/src/Servers/Kestrel/Core/src/Http2Limits.cs ```csharp= public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder .ConfigureKestrel(serverOptions => { serverOptions.Limits.Http2.KeepAlivePingDelay = TimeSpan.FromSeconds(3); serverOptions.Limits.Http2.KeepAlivePingTimeout = TimeSpan.FromSeconds(3); }) .UseStartup<Startup>(); }); ``` :::info Kestrelに実装されたキープアライブは、タイムアウトが発生すると例外が送出される。 例外以外でタイムアウトを検知する方法は用意されていない。 https://github.com/dotnet/aspnetcore/blob/a0b950fc51c43289ab3c6dbea15926d32f3556cc/src/Servers/Kestrel/Core/src/Internal/Http2/Http2Connection.cs#L282 `Http2ConnectionErrorException(CoreStrings.Http2ErrorKeepAliveTimeout, Http2ErrorCode.INTERNAL_ERROR);` この内部例外は、`System.IO.IOException` に変換されて `gRPC Service` 側で捕捉することが可能。 ```shell= ?ex.Message "The request stream was aborted." ``` ::: ###### tags: `gRPC` `ASP.NET Core` `キープアライブ` `keepalive` `Kestrel`
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up