In the gRPC web client, how can I abort a call and return an early response through an interceptor?

152 weergaven
Naar het eerste ongelezen bericht

Jun

ongelezen,
27 aug 2023, 09:41:0327-08-2023
aan grpc.io
I'm trying to return a cached response through the interceptor, but I couldn't find a way to make an early return. When the call is cancelled in the interceptor, the callback function doesn't get run. I get neither the error nor the response. Ideally, I would like to return a cached response after the call is cancelled.

Here's the interceptor. I called the cancel method inside:

```js
const StreamInterceptor = function () {};
StreamInterceptor.prototype.intercept = function (request, invoker) {
  const InterceptedStream = function (stream) {
    this.stream = stream;
  };
  InterceptedStream.prototype.on = function (eventType, callback) {
    this.stream.on(eventType, callback);
    return this;
  };
  const stream = invoker(request);
  stream.cancel();
  return new InterceptedStream(stream);
};
```

Here's the client and the method call. The call is unary.

```js
const client = new ServiceClient('http://localhost:8080', null, {
  streamInterceptors: [new StreamInterceptor()],
});
client.hello(message, {}, (err, res) => {
  console.log(res);
});
```

Eryu Xia

ongelezen,
30 aug 2023, 19:09:4630-08-2023
aan grpc.io
Hi Jun,

Thanks for reaching out!

My understanding is that it might not be possible to achieve what you want with the current grpc-web library.

The reason is that when you call stream.cancel(), you're essentially cancelling the underlying server request (e.g. XHR), which is cutting off this chain of response as indicated in the diagram below:


grpc-web-interceptors.jpg

I wonder if it'd be possible to implement caching one layer above the grpc-web library — since in this case it doesn't seem like you necessarily need to make this RPC at all?

Thanks!

Jun

ongelezen,
30 aug 2023, 21:59:5130-08-2023
aan grpc.io
Thanks for the response! We looked through the source code and didn't find a solution either. We were hoping to access the metadata through an interceptor. Our current solution is like you said, to implement caching outside of the call.
Allen beantwoorden
Auteur beantwoorden
Doorsturen
0 nieuwe berichten