Skip to content

Commit 63cd079

Browse files
authored
New gRPC inter-process doc (#19891)
1 parent 960019b commit 63cd079

3 files changed

Lines changed: 116 additions & 2 deletions

File tree

aspnetcore/grpc/interprocess.md

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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`.

aspnetcore/grpc/performance.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ There are many L7 proxies available. Some options are:
106106

107107
::: moniker range=">= aspnetcore-5.0"
108108

109+
## Inter-process communication
110+
111+
gRPC calls between a client and service are usually sent over TCP sockets. TCP is great for communicating across a network, but [inter-process communication (IPC)](https://wikipedia.org/wiki/Inter-process_communication) is more efficient when the client and service are on the same machine.
112+
113+
Consider using a transport like Unix domain sockets or named pipes for gRPC calls between processes on the same machine. For more information, see <xref:grpc/interprocess>.
114+
109115
## Keep alive pings
110116

111117
Keep alive pings can be used to keep HTTP/2 connections alive during periods of inactivity. Having an existing HTTP/2 connection ready when an app resumes activity allows for the initial gRPC calls to be made quickly, without a delay caused by the connection being reestablished.

aspnetcore/toc.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,10 @@
740740
uid: grpc/security
741741
- name: Performance best practices
742742
uid: grpc/performance
743+
- name: Inter-process communication
744+
uid: grpc/interprocess
745+
- name: Create JSON Web APIs from gRPC
746+
uid: grpc/httpapi
743747
- name: Manage Protobuf references with dotnet-grpc
744748
uid: grpc/dotnet-grpc
745749
- name: Test services with gRPC tools
@@ -750,8 +754,6 @@
750754
uid: grpc/wcf
751755
- name: Compare gRPC services with HTTP APIs
752756
uid: grpc/comparison
753-
- name: Create JSON Web APIs from gRPC
754-
uid: grpc/httpapi
755757
- name: Samples
756758
href: https://github.com/grpc/grpc-dotnet/tree/master/examples
757759
- name: Troubleshoot

0 commit comments

Comments
 (0)