Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support unix domain sockets as host in grpc_client #64

Merged
merged 2 commits into from
Nov 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,17 @@ If no channel is specified in the options to a rpc call the `default_channel` is
{client, #{channels => [{default_channel, [{http, "localhost", 8080, []}], #{}}]}}
```

Unix sockets (UDS) may also be used with the same notation that is defined in `gen_tcp`. Considerations:
* for UDS, only the `http` scheme is permitted
* the port must strictly be `0`
* only available on POSIX operating systems
* abstract UDS are only available on Linux, and such sockets' names must start with a zero byte
```
{client, #{channels => [{default_channel, [{http, {local, "/path/to/unix/socket_name"}, 0, []}], #{}}]}}
%% or to use an abstract Unix socket:
%% {client, #{channels => [{default_channel, [{http, {local, [0 | "socket_name"]}, 0, []}], #{}}]}}
```

The empty map at the end can contain configuration for the load balancing algorithm, interceptors, statistics handling and compression:

```
Expand Down
2 changes: 1 addition & 1 deletion src/grpcbox.app.src
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
acceptor_pool,
gproc,
ctx]},
{env, [{client, #{channels => [%% {default_channel, round_robin, [{http, "localhost", 8080, []}], #{}}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed this example, the round_robin option should have been inside the last argument (#{}) but it is the default value anyway so just removed it.

{env, [{client, #{channels => [%% {default_channel, [{http, "localhost", 8080, []}], #{}}
]}},

{grpc_opts, #{service_protos => [],
Expand Down
18 changes: 18 additions & 0 deletions src/grpcbox_subchannel.erl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,24 @@
endpoint=Endpoint,
channel=Channel}}.

%% In case of unix socket transport
%% (defined as tuple {local, _UnixPath} in gen_tcp),
%% there is no standard on what the authority field value
%% should be, as HTTP/2 over UDS is not formally specified.
%% To follow other gRPC implementations' behavior,
%% the "localhost" value is used.
info_map({Scheme, {local, _UnixPath} = Host, Port, _}, Encoding, StatsHandler) ->
case {Scheme, Port} of

Check warning on line 46 in src/grpcbox_subchannel.erl

View check run for this annotation

Codecov / codecov/patch

src/grpcbox_subchannel.erl#L46

Added line #L46 was not covered by tests
%% The ssl layer is not functional over unix sockets currently,
%% and the port is strictly required to be 0 by gen_tcp.
{http, 0} ->
#{authority => <<"localhost">>,

Check warning on line 50 in src/grpcbox_subchannel.erl

View check run for this annotation

Codecov / codecov/patch

src/grpcbox_subchannel.erl#L50

Added line #L50 was not covered by tests
scheme => <<"http">>,
encoding => Encoding,
stats_handler => StatsHandler};
_ ->
error({badarg, [Scheme, Host, Port]})

Check warning on line 55 in src/grpcbox_subchannel.erl

View check run for this annotation

Codecov / codecov/patch

src/grpcbox_subchannel.erl#L55

Added line #L55 was not covered by tests
end;
info_map({http, Host, 80, _}, Encoding, StatsHandler) ->
#{authority => list_to_binary(Host),
scheme => <<"http">>,
Expand Down