limit number of open connections

Refs #1740

also, expose limit option for number concurrent streams for gRPC
(unlimited by default)
This commit is contained in:
Anton Kaliaev
2018-06-20 18:38:42 +04:00
parent 3e1baf68f8
commit 936a655990
15 changed files with 245 additions and 38 deletions

View File

@ -6,13 +6,21 @@ import (
"strings"
"time"
"golang.org/x/net/netutil"
"google.golang.org/grpc"
cmn "github.com/tendermint/tmlibs/common"
)
// Start the grpcServer in a go routine
func StartGRPCServer(protoAddr string) (net.Listener, error) {
// Config is an gRPC server configuration.
type Config struct {
MaxOpenConnections int
}
// StartGRPCServer starts a new gRPC BroadcastAPIServer, listening on
// protoAddr, in a goroutine. Returns a listener and an error, if it fails to
// parse an address.
func StartGRPCServer(protoAddr string, config Config) (net.Listener, error) {
parts := strings.SplitN(protoAddr, "://", 2)
if len(parts) != 2 {
return nil, fmt.Errorf("Invalid listen address for grpc server (did you forget a tcp:// prefix?) : %s", protoAddr)
@ -22,6 +30,9 @@ func StartGRPCServer(protoAddr string) (net.Listener, error) {
if err != nil {
return nil, err
}
if config.MaxOpenConnections > 0 {
ln = netutil.LimitListener(ln, config.MaxOpenConnections)
}
grpcServer := grpc.NewServer()
RegisterBroadcastAPIServer(grpcServer, &broadcastAPI{})
@ -30,7 +41,8 @@ func StartGRPCServer(protoAddr string) (net.Listener, error) {
return ln, nil
}
// Start the client by dialing the server
// StartGRPCClient dials the gRPC server using protoAddr and returns a new
// BroadcastAPIClient.
func StartGRPCClient(protoAddr string) BroadcastAPIClient {
conn, err := grpc.Dial(protoAddr, grpc.WithInsecure(), grpc.WithDialer(dialerFunc))
if err != nil {