soheilhy / cmux

Connection multiplexer for GoLang: serve different services on the same port!
Apache License 2.0
2.58k stars 199 forks source link

Cannot get Matchers to mux h2c gRPC and HTTP2(REST) requests #78

Open Liu-Chunhui opened 4 years ago

Liu-Chunhui commented 4 years ago

Hi there,

Situration: my project is running in istio + k8s. When the http1.1 requests comes into the istio, it will be upgraded to http2 and passed to k8s pod. Also, I need to run non-tls gRPC and HTTP server on the same port, e.g. :9000

Issue: I try to use cmux to create machers to split grpc and rest requests but i am failed.

Could you please provide an example to achieve that?

Thanks,

drunkpig commented 4 years ago

see my explain https://github.com/soheilhy/cmux/issues/79

howardjohn commented 3 years ago

I think the issue you will find here is that your gRPC requests and HTTP2 requests are likely multiplexed over the same connection. cmux does matching at the connection level, so you end up with detecting one way or the other based on the first request, then all future requests are locked to whatever was originally matched.

letian0805 commented 3 years ago

You can use cmux.HTTP2MatchHeaderFieldSendSettings("content-type, "application/grpc") match gRPC first, and then use cmux.Any match other proto and create new listener for new cmux.CMux

BRUHItsABunny commented 2 years ago

I was unable to get it to work with cmux, h2 REST would always fail to connect

I ended up following a similar approach to this: https://ahmet.im/blog/grpc-http-mux-go/ For my use case this ended up working (http1.1, h2 REST and grpc all work as expected)