|
| 1 | +using System; |
| 2 | +using System.Net.Http; |
| 3 | +using Grpc.Net.Client; |
| 4 | + |
| 5 | +namespace Dapr.Workflow |
| 6 | +{ |
| 7 | + internal static class GrpcKeepAlive |
| 8 | + { |
| 9 | + public static void Configure(WorkflowRuntimeOptions options, GrpcChannelOptions channelOptions) |
| 10 | + { |
| 11 | + // We check whether the environment variables are set to override |
| 12 | + // the options. The environment variables ensure consistency between |
| 13 | + // the different Dapr SDKs. |
| 14 | + string? enableKeepAlive = Environment.GetEnvironmentVariable("DAPR_ENABLE_KEEP_ALIVE"); |
| 15 | + if (enableKeepAlive != null) |
| 16 | + { |
| 17 | + options.EnableKeepAlive = bool.Parse(enableKeepAlive); |
| 18 | + } |
| 19 | + string? keepAliveTime = Environment.GetEnvironmentVariable("DAPR_KEEP_ALIVE_TIME"); |
| 20 | + if (keepAliveTime != null) |
| 21 | + { |
| 22 | + options.KeepAliveTime = TimeSpan.FromSeconds(int.Parse(keepAliveTime)); |
| 23 | + } |
| 24 | + string? keepAliveTimeout = Environment.GetEnvironmentVariable("DAPR_KEEP_ALIVE_TIMEOUT"); |
| 25 | + if (keepAliveTimeout != null) |
| 26 | + { |
| 27 | + options.KeepAliveTimeout = TimeSpan.FromSeconds(int.Parse(keepAliveTimeout)); |
| 28 | + } |
| 29 | + string? keepAliveWithoutCalls = Environment.GetEnvironmentVariable("DAPR_KEEP_ALIVE_WITHOUT_CALLS"); |
| 30 | + if (keepAliveWithoutCalls != null) |
| 31 | + { |
| 32 | + options.KeepAliveWithoutCalls = bool.Parse(keepAliveWithoutCalls); |
| 33 | + } |
| 34 | + if (!options.EnableKeepAlive) |
| 35 | + { |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + if (channelOptions.HttpHandler is not SocketsHttpHandler socketsHandler) |
| 40 | + { |
| 41 | + socketsHandler = new SocketsHttpHandler(); |
| 42 | + channelOptions.HttpHandler = socketsHandler; |
| 43 | + } |
| 44 | + |
| 45 | + socketsHandler.KeepAlivePingDelay = options.KeepAliveTime; |
| 46 | + socketsHandler.KeepAlivePingTimeout = options.KeepAliveTimeout; |
| 47 | +#if NET8_0_OR_GREATER |
| 48 | + socketsHandler.KeepAlivePingPolicy = options.KeepAliveWithoutCalls |
| 49 | + ? HttpKeepAlivePingPolicy.Always |
| 50 | + : HttpKeepAlivePingPolicy.WithActiveRequests; |
| 51 | +#endif |
| 52 | + } |
| 53 | + } |
| 54 | +} |
0 commit comments