istio-ecosystem / wasm-extensions

Contains miscellaneous Wasm extensions for Istio
Apache License 2.0
105 stars 34 forks source link

wasm extension with gRPC bidi streams #58

Open NomadXD opened 3 years ago

NomadXD commented 3 years ago

Is there any example of a wasm extension that uses gRPC bidi streaming ? For gRPC unary, there are some examples. But for gRPC bidi streams couldn't find any examples. I would really appreciate if someone can provide a sample code snippet of using grpc bidi streams from a filter since there are no resources currently available for that. Thanks

bianpengyuan commented 3 years ago

No we don't have one yet. We can add that.

NomadXD commented 3 years ago

@bianpengyuan yeah that would be great to have. I have a native C++ filter and I'm trying to re-implement it in WASM , so I don't have to build it everytime with bazel. I have some question regarding the grpc streaming APIs in proxy_wasm_api.h. Can I ask it here if you won't mind ?

bianpengyuan commented 3 years ago

I've never tried the gRPC stream API, so not sure how much I can help.

Meanwhile, I don't think bazel is quite avoidable when you write Wasm module with C++. Especially when you use protobuf / gRPC. Although Bazel based build for C++ Wasm extension is way easier than native Envoy filter.

NomadXD commented 3 years ago

@bianpengyuan yeah that's true. I just want to avoid the native envoy build. I actually want to have both native and WASM filters, so it's more flexible to have options. I'll just describe my question below, if you are familiar with it you can answer. So my filter needs to use the gRPC bidi streams to communicate with an external service. I want to create a stream per every filter object and use the stream to send data to the external service.

The WASM api has this following method will creates a stream and delegates the stream management to the GrpcStreamHanlder class.

WasmResult grpcStreamHandler(std::string_view service, std::string_view service_name,
                               std::string_view method_name,
                               const HeaderStringPairs &initial_metadata,
                               std::unique_ptr<GrpcStreamHandlerBase> handler);

So we have to implement that class like ExampleGrpcStreamHanlder: public GrpcStreamHanlder<Request,Response> and implement relevant methods like onResponse(), onRemoteClose(). And if I want to send a message to the external service, I have to call the send() method in GrpcStreamHanlder. To call it from my filter I need to have some reference to GrpcStreamHanlder. In the C++ native filter, I pass the grpc client to the filter constructor. Here I'm not sure where I should pass that reference. Sorry if what i'm saying is not clear to you.