Search code examples
node.jsgrpcenvoyproxygrpc-node

Envoy GRPC Reverse Bridge - Received RST_STREAM with code 0


I am trying to access HTTP1.1 rest API via GRPC client, through Envoy GRPC Reverse bridge. But when I test it I get the below error. Any help or sample code snippet would be much appreciated. Thanks!

Error: 13 INTERNAL: Received RST_STREAM with code 0
    at Object.callErrorFromStatus (/Users/meiyappan/grpc-node/grpc/examples/node/node_modules/@grpc/grpc-js/build/src/call.js:31:26)
    at Object.onReceiveStatus (/Users/meiyappan/grpc-node/grpc/examples/node/node_modules/@grpc/grpc-js/build/src/client.js:176:52)
    

My Envoy proxy configuration XML

admin:
  address:
    socket_address:
      address: 0.0.0.0
      port_value: 9901
static_resources:
  listeners:
  - name: listener_0
    address:
      socket_address:
        address: 0.0.0.0
        port_value: 50051
    filter_chains:
    - filters:
      - name: envoy.filters.network.http_connection_manager
        typed_config:
          "@type": type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager
          access_log:
          - name: envoy.access_loggers.stdout
            typed_config:
              "@type": type.googleapis.com/envoy.extensions.access_loggers.stream.v3.StdoutAccessLog
          stat_prefix: ingress_http
          route_config:
            name: local_route
            virtual_hosts:
            - name: local_service
              domains: ["*"]
              routes:
              - match:
                  prefix: "/"
                route:
                  cluster: grpc
                  timeout: 50.00s
                # per_filter_config disables the filter for this route
                typed_per_filter_config:
                  envoy.filters.http.grpc_http1_reverse_bridge:
                    "@type": type.googleapis.com/envoy.extensions.filters.http.grpc_http1_reverse_bridge.v3.FilterConfigPerRoute
                    disabled: true
          http_filters:
          - name: envoy.filters.http.grpc_http1_reverse_bridge
            typed_config:
              "@type": type.googleapis.com/envoy.extensions.filters.http.grpc_http1_reverse_bridge.v3.FilterConfig
              content_type: application/grpc
              withhold_grpc_frames: true
          - name: envoy.filters.http.router
  clusters:
  - name: grpc
    type: STRICT_DNS
    lb_policy: ROUND_ROBIN
    load_assignment:
      cluster_name: grpc
      endpoints:
      - lb_endpoints:
        - endpoint:
            address:
              socket_address:
                address: localhost
                port_value: 80

This is the Proto File

// The greeting service definition.
service Greeter {
  // Sends a greeting
  rpc SayHello (HelloRequest) returns (HelloReply) {}
}

// The request message containing the user's name.
message HelloRequest {
  string name = 1;
}

// The response message containing the greetings
message HelloReply {
  string message = 1;
}

My HTTP Rest API, using Node-Express

app.post('/helloworld.greeter/SayHello', async (req, res) => {
    console.log(req)
const root = await protobuf.load(PROTO_PATH);

  const HelloReply = root.lookupType('helloworld.HelloReply');
  const buf = HelloReply.encode({ message: 'Hello Bill'}).finish();
  res.send(buf)
  res.end();
})

GRPC Client Code in node js

function main() {
  
  var  target = 'localhost:50051';
  
  var client = new hello_proto.Greeter(target,
                                       grpc.credentials.createInsecure());
  var user = 'world';
  
  var metadata = new grpc.Metadata();
  metadata.add('Content-Type', 'application/grpc')
  client.sayHello({name: user},metadata, function(err, response) {
    console.log(err)
    console.log('Greeting:', response);
  });
}

Solution

  • I have successfully reproduced your issue. There are two things wrong:

    In your Envoy config, remove the typed_per_filter_config, because here you are saying to not use the grpc_http1_reverse_bridge for / but you should use it.

    In your server implementation, add:

    res.header("Content-Type", "application/grpc");
    

    just before res.send(buf).

    Then, in your client, if you write:

    console.log('Greeting:', response.getMessage());
    

    You should see: Greeting: Hello Bill.

    Retrieve request attributes

    Also, if you want to retrieve the request body, you should use the raw express middleware in the server:

    app.use(express.raw({type: "application/grpc"}));
    

    And then:

    const root = await protobuf.load(PROTO_PATH);
    
    app.post('/helloworld.greeter/SayHello', (req, res) => {
        var message = "Hello";
    
        // if there is a body, transform it to HelloRequest and retrieve name
        if (req.body !== undefined && req.body instanceof Buffer && req.body.length != 0) {
            const HelloRequest = root.lookupType('helloworld.HelloRequest');
            const helloReq = HelloRequest.decode(req.body);
            message += " " + helloReq.name;
        }
    
        res.header("Content-Type", "application/grpc");
    
        const HelloReply = root.lookupType('helloworld.HelloReply');
        res.send(HelloReply.encode({ message: message }).finish());
    });
    

    Client side, you can pass the name like this:

    var request = new HelloRequest();
    request.setName("World");
    

    By executing the client, you should now get: Greeting: Hello World.