1
0
mirror of https://github.com/go-micro/go-micro.git synced 2024-12-24 10:07:04 +02:00

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:
Vasiliy Tolstov 2020-02-24 16:48:56 +03:00 committed by GitHub
parent cf0b39eaac
commit 24d574ae71
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 6 deletions

View File

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

View File

@ -18,6 +18,7 @@ type codecsKey struct{}
type grpcOptions struct{} type grpcOptions struct{}
type netListener struct{} type netListener struct{}
type maxMsgSizeKey struct{} type maxMsgSizeKey struct{}
type maxConnKey struct{}
type tlsAuth struct{} type tlsAuth struct{}
// gRPC Codec to be used to encode/decode requests for a given content type // gRPC Codec to be used to encode/decode requests for a given content type
@ -40,6 +41,11 @@ func AuthTLS(t *tls.Config) server.Option {
return setServerOption(tlsAuth{}, t) return setServerOption(tlsAuth{}, t)
} }
// MaxConn specifies maximum number of max simultaneous connections to server
func MaxConn(n int) server.Option {
return setServerOption(maxConnKey{}, n)
}
// Listener specifies the net.Listener to use instead of the default // Listener specifies the net.Listener to use instead of the default
func Listener(l net.Listener) server.Option { func Listener(l net.Listener) server.Option {
return setServerOption(netListener{}, l) return setServerOption(netListener{}, l)