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

How to create a channel from URL #164

Open
CaptTwigg opened this issue Sep 1, 2022 · 7 comments
Open

How to create a channel from URL #164

CaptTwigg opened this issue Sep 1, 2022 · 7 comments

Comments

@CaptTwigg
Copy link

How do I create a channel where the host is an URL?
eg: Channel(host="localhost/backendDev", port=5000)

The server is made in c# and runs in an windows service and the LaunchSettings look like this:

{
  "profiles": {
    "BackendLocalGrpc": {
      "commandName": "Project",
      "dotnetRunMessages": "true",
      "launchBrowser": false,
      "applicationUrl": "http://localhost:5000",
      "launchUrl": "backendDev",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

Running it with just localhost works fine, but I use backend to distinguish between prod, test and dev environments.

@vmagamedov
Copy link
Owner

I don't know any gRPC server or client implementation which supports serving requests with some path prefix.

https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md

Some gRPC implementations may allow the Path format shown above to be overridden, but this functionality is strongly discouraged. gRPC does not go out of its way to break users that are using this kind of override, but we do not actively support it, and some functionality (e.g., service config support) will not work when the path is not of the form shown above.

I think that it is better to find another way to distinguish environments.

Is this server written by you or by someone else?

@CaptTwigg
Copy link
Author

Have it in c#, but after a little testing it seem not to do anything, I will get back when I know more.

var channelOptionsLocal = new GrpcChannelOptions { MaxReceiveMessageSize = null };   // Remove MaxMessageSize limit
LocalManagerChannel = GrpcChannel.ForAddress(@"http://localhost:5000/backendDev", channelOptionsLocal);

I have control of the server, so first thought would be to have a unique port for each environment.

But I'm not sure how to create a channel to our azure service then, I tried with the IP from the URL, but then I get an connection lost exception. It's a https connection.

Channel(host="backendDev.azure.com", port=443)
Traceback (most recent call last):
  File "C:\Users\mhj\AppData\Local\Programs\Python\Python310\lib\site-packages\grpclib\client.py", line 368, in recv_initial_metadata
    headers = await self._stream.recv_headers()
  File "C:\Users\mhj\AppData\Local\Programs\Python\Python310\lib\site-packages\grpclib\protocol.py", line 342, in recv_headers
    await self.headers_received.wait()
  File "C:\Users\mhj\AppData\Local\Programs\Python\Python310\lib\asyncio\locks.py", line 214, in wait
    await fut
asyncio.exceptions.CancelledError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Dev/pythonProject/TestGrpc/main.py", line 43, in main
    await read_version()
  File "C:/Dev/pythonProject/TestGrpc/main.py", line 12, in read_version
    data = await backend.storage_manager.read_db_version(database_type_enum=DatabaseTypeEnum.DATABASE_TYPE_ENUM_MASTER_DATABASE)
  File "C:\Users\mhj\AppData\Local\Programs\Python\Python310\lib\site-packages\invisio_backend\Protos\Interfaces\__init__.py", line 2459, in read_db_version
    return await self._unary_unary(
  File "C:\Users\mhj\AppData\Local\Programs\Python\Python310\lib\site-packages\betterproto\grpc\grpclib_client.py", line 85, in _unary_unary
    response = await stream.recv_message()
  File "C:\Users\mhj\AppData\Local\Programs\Python\Python310\lib\site-packages\grpclib\client.py", line 425, in recv_message
    await self.recv_initial_metadata()
  File "C:\Users\mhj\AppData\Local\Programs\Python\Python310\lib\site-packages\grpclib\client.py", line 367, in recv_initial_metadata
    with self._wrapper:
  File "C:\Users\mhj\AppData\Local\Programs\Python\Python310\lib\site-packages\grpclib\utils.py", line 70, in __exit__
    raise self._error
grpclib.exceptions.StreamTerminatedError: Connection lost

@vmagamedov
Copy link
Owner

Try this way:

Channel(host="backendDev.azure.com", port=443, ssl=True)
                                               ~~~~~~~~

https://grpclib.readthedocs.io/en/latest/client.html#secure-channels

@CaptTwigg
Copy link
Author

Now I get an GRPCError

grpclib.exceptions.GRPCError: (<Status.UNKNOWN: 2>, "Received :status = '426'", None)

Tried https://grpclib.readthedocs.io/en/latest/errors.html#error-details but details is None

@CaptTwigg
Copy link
Author

The problem seems to be that the cloud service is running http 1.1 with UseGrpcWeb and only http 2 is supported.
Could this be correct?

@vmagamedov
Copy link
Owner

Maybe there is some kind of misconfiguration on the server-side or a bug like this: dotnet/aspnetcore#14139

grpclib sends only HTTP/2 requests. So the server must support original (not grpc-web) HTTP/2-based gRPC protocol.

@tmsrikanth
Copy link

tmsrikanth commented Dec 2, 2022

The connection is not secure , hence the error. Is the service deployed as part of kube cluster in AKS. They would have used ingress to expose the service endpoint. Try to get the client certificate and try a secure channel creation.

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

No branches or pull requests

3 participants