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:
		
							
								
								
									
										20
									
								
								grpc.go
									
									
									
									
									
								
							
							
						
						
									
										20
									
								
								grpc.go
									
									
									
									
									
								
							| @@ -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() | ||||
|   | ||||
| @@ -18,6 +18,7 @@ type codecsKey struct{} | ||||
| type grpcOptions struct{} | ||||
| type netListener struct{} | ||||
| type maxMsgSizeKey struct{} | ||||
| type maxConnKey struct{} | ||||
| type tlsAuth struct{} | ||||
|  | ||||
| // 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) | ||||
| } | ||||
|  | ||||
| // 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 | ||||
| func Listener(l net.Listener) server.Option { | ||||
| 	return setServerOption(netListener{}, l) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user