server/grpc: add MaxConn option to limit max inflight requests (#1247)

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
2020-02-24 16:48:56 +03:00
parent 28ef20ef4a
commit f727b75b6f
2 changed files with 20 additions and 6 deletions

20
grpc.go
View File

@@ -24,6 +24,7 @@ import (
"github.com/micro/go-micro/v2/util/addr"
mgrpc "github.com/micro/go-micro/v2/util/grpc"
mnet "github.com/micro/go-micro/v2/util/net"
"golang.org/x/net/netutil"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
@@ -163,13 +164,14 @@ func (g *grpcServer) getGrpcOptions() []grpc.ServerOption {
}
func (g *grpcServer) getListener() net.Listener {
if g.opts.Context != nil {
if v := g.opts.Context.Value(netListener{}); v != nil {
if l, ok := v.(net.Listener); ok {
return l
}
}
if g.opts.Context == nil {
return nil
}
if l, ok := g.opts.Context.Value(netListener{}).(net.Listener); ok && l != nil {
return l
}
return nil
}
@@ -799,6 +801,12 @@ func (g *grpcServer) Start() error {
}
}
if g.opts.Context != nil {
if c, ok := g.opts.Context.Value(maxConnKey{}).(int); ok && c > 0 {
ts = netutil.LimitListener(ts, c)
}
}
log.Infof("Server [grpc] Listening on %s", ts.Addr().String())
g.Lock()
g.opts.Address = ts.Addr().String()