|
| 1 | +--- |
| 2 | +title: Inter-process communication with gRPC |
| 3 | +author: jamesnk |
| 4 | +description: Learn how to use gRPC for inter-process communication. |
| 5 | +monikerRange: '>= aspnetcore-5.0' |
| 6 | +ms.author: jamesnk |
| 7 | +ms.date: 09/16/2020 |
| 8 | +no-loc: ["ASP.NET Core Identity", cookie, Cookie, Blazor, "Blazor Server", "Blazor WebAssembly", "Identity", "Let's Encrypt", Razor, SignalR] |
| 9 | +uid: grpc/interprocess |
| 10 | +--- |
| 11 | +# Inter-process communication with gRPC |
| 12 | + |
| 13 | +By [James Newton-King](https://twitter.com/jamesnk) |
| 14 | + |
| 15 | +gRPC calls between a client and service are usually sent over TCP sockets. TCP was designed for communicating across a network. [Inter-process communication (IPC)](https://wikipedia.org/wiki/Inter-process_communication) is more efficient than TCP when the client and service are on the same machine. This document explains how to use gRPC with custom transports in IPC scenarios. |
| 16 | + |
| 17 | +## Server configuration |
| 18 | + |
| 19 | +Custom transports are supported by [Kestrel](xref:fundamentals/servers/kestrel). Kestrel is configured in *Program.cs*: |
| 20 | + |
| 21 | +```csharp |
| 22 | +public static readonly string SocketPath = Path.Combine(Path.GetTempPath(), "socket.tmp"); |
| 23 | + |
| 24 | +public static IHostBuilder CreateHostBuilder(string[] args) => |
| 25 | + Host.CreateDefaultBuilder(args) |
| 26 | + .ConfigureWebHostDefaults(webBuilder => |
| 27 | + { |
| 28 | + webBuilder.UseStartup<Startup>(); |
| 29 | + webBuilder.ConfigureKestrel(options => |
| 30 | + { |
| 31 | + if (File.Exists(SocketPath)) |
| 32 | + { |
| 33 | + File.Delete(SocketPath); |
| 34 | + } |
| 35 | + options.ListenUnixSocket(SocketPath); |
| 36 | + }); |
| 37 | + }); |
| 38 | +``` |
| 39 | + |
| 40 | +The preceding example: |
| 41 | + |
| 42 | +* Configures Kestrel's endpoints in `ConfigureKestrel`. |
| 43 | +* Calls <xref:Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerOptions.ListenUnixSocket*> to listen to a [Unix domain socket (UDS)](https://wikipedia.org/wiki/Unix_domain_socket) with the specified path. |
| 44 | + |
| 45 | +Kestrel has built-in support for UDS endpoints. UDS are supported on Linux, macOS and [modern versions of Windows](https://devblogs.microsoft.com/commandline/af_unix-comes-to-windows/). |
| 46 | + |
| 47 | +## Client configuration |
| 48 | + |
| 49 | +`GrpcChannel` supports making gRPC calls over custom transports. When a channel is created, it can be configured with a `SocketsHttpHandler` that has a custom `ConnectCallback`. The callback allows the client to make connections over custom transports and then send HTTP requests over that transport. |
| 50 | + |
| 51 | +> [!IMPORTANT] |
| 52 | +> `SocketsHttpHandler.ConnectCallback` is a new API in .NET 5 release candidate 2. |
| 53 | +
|
| 54 | +Unix domain sockets connection factory example: |
| 55 | + |
| 56 | +```csharp |
| 57 | +public class UnixDomainSocketConnectionFactory |
| 58 | +{ |
| 59 | + private readonly EndPoint _endPoint; |
| 60 | + |
| 61 | + public UnixDomainSocketConnectionFactory(EndPoint endPoint) |
| 62 | + { |
| 63 | + _endPoint = endPoint; |
| 64 | + } |
| 65 | + |
| 66 | + public async ValueTask<Stream> ConnectAsync(SocketsHttpConnectionContext _, |
| 67 | + CancellationToken cancellationToken = default) |
| 68 | + { |
| 69 | + var socket = new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.Unspecified); |
| 70 | + |
| 71 | + try |
| 72 | + { |
| 73 | + await socket.ConnectAsync(_endPoint, cancellationToken).ConfigureAwait(false); |
| 74 | + return new NetworkStream(socket, true); |
| 75 | + } |
| 76 | + catch |
| 77 | + { |
| 78 | + socket.Dispose(); |
| 79 | + throw; |
| 80 | + } |
| 81 | + } |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | +Using the custom connection factory to create a channel: |
| 86 | + |
| 87 | +```csharp |
| 88 | +public static readonly string SocketPath = Path.Combine(Path.GetTempPath(), "socket.tmp"); |
| 89 | + |
| 90 | +public static GrpcChannel CreateChannel() |
| 91 | +{ |
| 92 | + var udsEndPoint = new UnixDomainSocketEndPoint(SocketPath); |
| 93 | + var connectionFactory = new UnixDomainSocketConnectionFactory(udsEndPoint); |
| 94 | + var socketsHttpHandler = new SocketsHttpHandler |
| 95 | + { |
| 96 | + ConnectionFactory = connectionFactory.ConnectAsync |
| 97 | + }; |
| 98 | + |
| 99 | + return GrpcChannel.ForAddress("http://localhost", new GrpcChannelOptions |
| 100 | + { |
| 101 | + HttpHandler = socketsHttpHandler |
| 102 | + }); |
| 103 | +} |
| 104 | +``` |
| 105 | + |
| 106 | +Channels created using the preceding code send gRPC calls over Unix domain sockets. Support for other IPC technologies can be implemented using the extensibility in Kestrel and `SocketsHttpHandler`. |
0 commit comments