grpc / grpc-swift

The Swift language implementation of gRPC.
Apache License 2.0
2.01k stars 414 forks source link

Difficulties to configure requiredInterfaceType for GRPCChannel #1943

Open jbgosselin opened 2 months ago

jbgosselin commented 2 months ago

What are you trying to achieve?

Hi grpc-swift team, I would like to be able to force a GRPC connection/call to happen on the cellular network only.

My best shot so far was to create a NWConnection with NWParameters.requiredInterfaceType. But I can't find a way to either configure this with grpc-swift or being able to pass a NIO's Channel to the library.

I see there is some wrappers around generating the NIO Channel here https://swiftinit.org/docs/grpc-swift/grpc/clientbootstrapprotocol, but it seems that I cannot call withExistingNWConnection on it https://swiftpackageindex.com/apple/swift-nio-transport-services/1.21.0/documentation/niotransportservices/niotsconnectionbootstrap/withexistingnwconnection(_:)

Do you have some advice if this is currently possible, if so how to achieve this ? Otherwise, what would be the course of action to add the possibility to force the interface used ?

What have you tried so far?

Here is a code snippet to create a NIO Channel with such connection.

import Network
import NIOTransportServices
import GRPC

func fooBar() async throws {
    guard let url = URL(string: "https://www.google.com") else {
        return
    }
    let endpoint = NWEndpoint.url(url)

    let parameters = NWParameters(tls: nil, tcp: Network.NWProtocolTCP.Options())
    parameters.requiredInterfaceType = .cellular
    let connection = NWConnection(to: endpoint, using: parameters)

    let eventLoopGroup = NIOTSEventLoopGroup()
    let channel = try await NIOTSConnectionBootstrap.init(group: eventLoopGroup).withExistingNWConnection(connection).get()
}

Thanks in advance !

glbrntt commented 2 months ago

This isn't currently possible, but it is something we should be able to add. It's somewhat related to https://github.com/grpc/grpc-swift/issues/1913 which also requires more API for creating connections with NWConnections/NWListeners.

jbgosselin commented 2 months ago

Thanks a lot for the fast answer ! Yes this looks similar than the mentioned issue. We are looking forward this addition in the future 🙌 Just for understanding the change, would it be adding a ConnectionTarget that would receive a NWConnection ?