Skip to content

Option "grpc.so_reuseport" does not work correctly on Linux OS. #794

Closed

Description

@plitvinov

Problem description

Setting the server option "grpc.so_reuseport" to 0 does not work on a Linux server.
On Windows it works.

Reproduction steps

Start several grpc-servers (node service.js) on the same machine. Every server binds to the same port.

File: service.js

var port = 48000;
var grpc = require('grpc');
var protoLoader = require('@grpc/proto-loader');
var packageDefinition = protoLoader.loadSync(
    __dirname + '/service.proto',
    {keepCase: true,
     longs: String,
     enums: String,
     defaults: true,
     oneofs: true
    });

var MyPackage = grpc.loadPackageDefinition(packageDefinition).MyPackage; 
var routeServer = new grpc.Server({"grpc.so_reuseport": 0});
routeServer.addService(MyPackage.MyService.service, {    getOne: getOne    });
if(routeServer.bind('0.0.0.0:'+port, grpc.ServerCredentials.createInsecure()) == port){
    console.log("Binded to port "+port);
}else{
    console.log("FAILED to bind to port "+port);
}
routeServer.start();

function getOne(call, callback) {
    console.log("getOne: "+ JSON.stringify(call.request));
    var ResponseData = {D: "getOne "+(counters.getOne++)}
    callback(null, ResponseData);
} 

service.proto:

syntax = "proto3";
package MyPackage;
service MyService{
    rpc getOne(Request) returns (Data){}
}
message Request{
    string R = 1;
}
message Data{
    string D = 1;
}

Environment

  • OS name, Linux, CentOS, OracleLinux
  • Node version 8.11.3 (and upper)
  • "grpc": "^1.19.0"
  • "@grpc/proto-loader": "^0.4.0

Additional context

Activity

  1. murgatroid99 commented on Mar 19, 2019

    @murgatroid99
    Member

    I agree that it would be good to implement this functionality in this library.

    However, we do not recommend using that feature to accomplish what it looks like you are trying to accomplish. If you want to force your server instances to bind to different ports, you should either bind to port 0 or use some out-of-band mechanism to assign ports to your server instances. In both cases, you shouldn't be frequently running into this case where two servers are attempting to bind the same port.

  2. plitvinov commented on Mar 20, 2019

    @plitvinov
    Author

    Hi

    We have several different grpc-microservices whish can be run on the same server within selected port range and which register themself to Service Discovery Server.
    In case of enabled flag 'so_reuseport' two or more different services can be binded to the same port on the server.
    For example: Service1 on 48000, Service2 on 48000.
    In this case any grpc-client will have a chance to connect to wrong service.
    For example: a grpc-client wants to use Service2 and finds port 48000 on the ServiceDiscovery and has a chance to connect to Service1 instead.

    In the C++ code we set the option GRPC_ARG_ALLOW_REUSEPORT to 0 ant it works fine.
    In Nodejs the same option {"grpc.so_reuseport": 0} works on Windows but does not work Linux.
    It seems there is the different behaviour of the one code on different systems.

  3. nicolasnoble commented on Mar 20, 2019

    @nicolasnoble
    Contributor

    Why can't you bind on port 0?

  4. plitvinov commented on Mar 20, 2019

    @plitvinov
    Author

    We need to bind to port range due network firewall rules.
    We have only a group of ports opened by firewall rules.

  5. self-assigned this
    on Mar 29, 2019
  6. zyf0330 commented on Nov 11, 2020

    @zyf0330

    Hello, is there any progress?

  7. murgatroid99 commented on May 11, 2021

    @murgatroid99
    Member

    The grpc package has been deprecated, so we will not be making further changes to this functionality. We recommend using @grpc/grpc-js instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions