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

Connecting to gRPC Server over Unix Domain Socket #2428

Open
thegreatco opened this issue May 6, 2024 · 1 comment
Open

Connecting to gRPC Server over Unix Domain Socket #2428

thegreatco opened this issue May 6, 2024 · 1 comment
Labels
question Further information is requested

Comments

@thegreatco
Copy link

Following the example in the docs, I tried to connect to a gRPC server listening on a Unix Domain Socket. I created a small program to test it

using System.Net;
using System.Net.Sockets;

using Grpc.Net.Client;

var socketPath = "/tmp/foo.sock";
var udsEndPoint = new UnixDomainSocketEndPoint(socketPath);
var connectionFactory = new UnixDomainSocketsConnectionFactory(udsEndPoint);
var socketsHttpHandler = new SocketsHttpHandler
{
    ConnectCallback = connectionFactory.ConnectAsync
};

var grpc = GrpcChannel.ForAddress("http://localhost", new GrpcChannelOptions
{
    HttpHandler = socketsHttpHandler
});

await grpc.ConnectAsync();

public class UnixDomainSocketsConnectionFactory
{
    private readonly EndPoint endPoint;

    public UnixDomainSocketsConnectionFactory(EndPoint endPoint)
    {
        this.endPoint = endPoint;
    }

    public async ValueTask<Stream> ConnectAsync(SocketsHttpConnectionContext _,
                                                CancellationToken cancellationToken = default)
    {
        var socket = new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.Unspecified);

        try
        {
            await socket.ConnectAsync(this.endPoint, cancellationToken).ConfigureAwait(false);
            return new NetworkStream(socket, true);
        }
        catch
        {
            socket.Dispose();
            throw;
        }
    }
}

However, I am getting the following error:

Unhandled exception. System.InvalidOperationException: Channel is configured with an HTTP transport doesn't support client-side load balancing or connectivity state tracking. The underlying HTTP transport must be a SocketsHttpHandler with no SocketsHttpHandler.ConnectCallback configured. The HTTP transport must be configured on the channel using GrpcChannelOptions.HttpHandler.
   at Grpc.Net.Client.GrpcChannel.ValidateHttpHandlerSupportsConnectivity()
   at Grpc.Net.Client.GrpcChannel.ConnectAsync(CancellationToken cancellationToken)
   at Program.<Main>$(String[] args) in /mnt/d/Dropbox/dev/GitHub/viam-dotnet-sdk/examples/ConsoleApp1/Program.cs:line 20
   at Program.<Main>(String[] args)
Aborted

I'm guessing this behavior changed since 1-Jan-2024, but not sure what the solution is here.

@thegreatco thegreatco added the question Further information is requested label May 6, 2024
@JamesNK
Copy link
Member

JamesNK commented May 6, 2024

ConnectAsync isn't supported when using unix domain sockets. Remove it and make gRPC calls as usual.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants