grpc / grpc-web

gRPC for Web Clients
https://grpc.io
Apache License 2.0
8.51k stars 766 forks source link

Not able to set deadline in grpc-web client call #1268

Closed alexcpn closed 1 year ago

alexcpn commented 1 year ago

I have a simple unary server; Protobuffer definition https://github.com/alexcpn/go_grpc_2022/blob/main/interfaces/microservice_1/test.proto

service SearchService {
  // Method to Search a test service
  rpc Search (SearchRequest) returns (SearchResponse) {}
}

I am generating a grpc-web client using the following Make file as in

compile_grpcweb:
    -mkdir -p generated
    protoc  -I ../../interfaces/ ../../interfaces/microservice_1/*.proto \
    --js_out=import_style=commonjs,binary:./generated \
    --grpc-web_out=import_style=typescript,mode=grpcweb:./generated

https://github.com/alexcpn/go_grpc_2022/blob/c8381e2ceb538cfce7be78089740419f2df1083a/microservice_1/ts_client/Makefile#L40-L44

The generated file has only two signatures

 search(
    request: microservice_1_test_pb.SearchRequest,
    metadata: grpcWeb.Metadata | null): Promise<microservice_1_test_pb.SearchResponse>;

  search(
    request: microservice_1_test_pb.SearchRequest,
    metadata: grpcWeb.Metadata | null,
    callback: (err: grpcWeb.RpcError,
               response: microservice_1_test_pb.SearchResponse) => void): grpcWeb.ClientReadableStream<microservice_1_test_pb.SearchResponse>;

https://github.com/alexcpn/go_grpc_2022/blob/main/microservice_1/ts_client/generated/microservice_1/TestServiceClientPb.ts

and in both not able to set deadline as mentioned in the documentation


    var dl = new Date();
    dl.setSeconds(dl.getSeconds() + 1);

   var result = this.searchService.search(searchRequest,{deadline:dl.getTime()})
    result.then(response => {
          this.responseData.data =response.getSearchResponse()
    }).catch(error  =>{
      console.log("Got error from server: grpcWeb.RpcError).message-->",(error as grpcWeb.RpcError).message)
    })

   const call =    this.searchService.search(searchRequest,{deadline:dl.getTime()},  (err: grpcWeb.RpcError, response: SearchResponse) =>  {
      if (err) {
        console.error("GRPC Error", err);
        this.responseData.error = err;
        this.searchService = <SearchServiceClient>{};
        return;
      }
      this.responseData.data =  response.getSearchResponse();
      console.log("Grpc-greeter.service.ts Response -->", this.responseData.data);
    });

The output of ng build

Error: src/app/grpc-greeter.service.ts:37:58 - error TS2322: Type 'number' is not assignable to type 'string'.

37    var result = this.searchService.search(searchRequest,{deadline:dl.getTime()})
                                                            ~~~~~~~~

Error: src/app/grpc-greeter.service.ts:44:61 - error TS2322: Type 'number' is not assignable to type 'string'.

44    const call =    this.searchService.search(searchRequest,{deadline:dl.getTime()},  (err: grpcWeb.RpcError, response: SearchResponse) =>  {
                                                               ~~~~~~~~
sampajano commented 1 year ago

Thanks for the report!

There is actually a type mismatch in our demo.. As Metadata can only receive String values.

You should be able to fix your coee by changing deadline:dl.getTime() -> deadline:dl.getTime().toString().

I'll make a change to the demo to call toString() as well. Thanks!!