-
Notifications
You must be signed in to change notification settings - Fork 4.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Expand service config support #1165
Changes from 25 commits
50d4175
13b5f12
a0b902a
ad16b94
a66f923
f02290b
f1bb70f
6f8b553
fa29686
c6a3937
8788b75
cb02ab4
bab6b61
983d837
9c5f260
eaa9ccb
ecbc34a
ea230c7
3ea2870
d926544
59426b3
bdf9a64
35d77ea
4d2b4b5
7505481
504db8e
d19bbe8
ed64d51
27ae147
cb64938
4a7b4d0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,7 +73,11 @@ func recvResponse(ctx context.Context, dopts dialOptions, t transport.ClientTran | |
} | ||
} | ||
for { | ||
if err = recv(p, dopts.codec, stream, dopts.dc, reply, dopts.maxMsgSize, inPayload); err != nil { | ||
if c.maxReceiveMessageSize == nil { | ||
// TODO(lyuxuan): codes.Internal the right error to return here? | ||
return Errorf(codes.Internal, "callInfo maxReceiveMessageSize field uninitialized(nil)") | ||
} | ||
if err = recv(p, dopts.codec, stream, dopts.dc, reply, *c.maxReceiveMessageSize, inPayload); err != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pass "c" instead? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. recv() is also used by serverStream |
||
if err == io.EOF { | ||
break | ||
} | ||
|
@@ -93,7 +97,7 @@ func recvResponse(ctx context.Context, dopts dialOptions, t transport.ClientTran | |
} | ||
|
||
// sendRequest writes out various information of an RPC such as Context and Message. | ||
func sendRequest(ctx context.Context, dopts dialOptions, compressor Compressor, callHdr *transport.CallHdr, stream *transport.Stream, t transport.ClientTransport, args interface{}, opts *transport.Options) (err error) { | ||
func sendRequest(ctx context.Context, dopts dialOptions, compressor Compressor, c *callInfo, callHdr *transport.CallHdr, stream *transport.Stream, t transport.ClientTransport, args interface{}, opts *transport.Options) (err error) { | ||
defer func() { | ||
if err != nil { | ||
// If err is connection error, t will be closed, no need to close stream here. | ||
|
@@ -118,6 +122,13 @@ func sendRequest(ctx context.Context, dopts dialOptions, compressor Compressor, | |
if err != nil { | ||
return Errorf(codes.Internal, "grpc: %v", err) | ||
} | ||
if c.maxSendMessageSize == nil { | ||
// TODO(lyuxuan): codes.Internal the right error to return here? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove TODO; this is fine. |
||
return Errorf(codes.Internal, "callInfo maxSendMessageSize field uninitialized(nil)") | ||
} | ||
if len(outBuf) > *c.maxSendMessageSize { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if c.maxSendMessageSize != nil && ... ? Or a getter that handles nil and returns the default? Otherwise this could be a nil pointer dereference. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This ideally should be pushed into encode() instead. Otherwise, we will still be allocating more memory than desired. encode() gets the size before it allocates the buffer, so we can safe the work of encoding the object if we check it there after we get the size. |
||
return Errorf(codes.ResourceExhausted, "Sent message larger than max (%d vs. %d)", len(outBuf), *c.maxSendMessageSize) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: this is not sent message. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. C implementation use this sentence. Do we want to be consistent with them or not? |
||
} | ||
err = t.Write(stream, outBuf, opts) | ||
if err == nil && outPayload != nil { | ||
outPayload.SentTime = time.Now() | ||
|
@@ -145,14 +156,18 @@ func Invoke(ctx context.Context, method string, args, reply interface{}, cc *Cli | |
|
||
func invoke(ctx context.Context, method string, args, reply interface{}, cc *ClientConn, opts ...CallOption) (e error) { | ||
c := defaultCallInfo | ||
if mc, ok := cc.getMethodConfig(method); ok { | ||
c.failFast = !mc.WaitForReady | ||
if mc.Timeout > 0 { | ||
var cancel context.CancelFunc | ||
ctx, cancel = context.WithTimeout(ctx, mc.Timeout) | ||
defer cancel() | ||
} | ||
mc := cc.GetMethodConfig(method) | ||
if mc.WaitForReady != nil { | ||
c.failFast = !*mc.WaitForReady | ||
} | ||
|
||
if mc.Timeout != nil && *mc.Timeout >= 0 { | ||
var cancel context.CancelFunc | ||
ctx, cancel = context.WithTimeout(ctx, *mc.Timeout) | ||
defer cancel() | ||
} | ||
|
||
opts = append(cc.dopts.callOptions, opts...) | ||
for _, o := range opts { | ||
if err := o.before(&c); err != nil { | ||
return toRPCErr(err) | ||
|
@@ -163,6 +178,10 @@ func invoke(ctx context.Context, method string, args, reply interface{}, cc *Cli | |
o.after(&c) | ||
} | ||
}() | ||
|
||
c.maxSendMessageSize = getMaxSize(mc.MaxReqSize, c.maxSendMessageSize, defaultClientMaxSendMessageSize) | ||
c.maxReceiveMessageSize = getMaxSize(mc.MaxRespSize, c.maxReceiveMessageSize, defaultClientMaxReceiveMessageSize) | ||
|
||
if EnableTracing { | ||
c.traceInfo.tr = trace.New("grpc.Sent."+methodFamily(method), method) | ||
defer c.traceInfo.tr.Finish() | ||
|
@@ -260,7 +279,7 @@ func invoke(ctx context.Context, method string, args, reply interface{}, cc *Cli | |
} | ||
return toRPCErr(err) | ||
} | ||
err = sendRequest(ctx, cc.dopts, cc.dopts.cp, callHdr, stream, t, args, topts) | ||
err = sendRequest(ctx, cc.dopts, cc.dopts.cp, &c, callHdr, stream, t, args, topts) | ||
if err != nil { | ||
if put != nil { | ||
updateRPCInfoInContext(ctx, rpcInfo{ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,8 +36,8 @@ package grpc | |
import ( | ||
"errors" | ||
"fmt" | ||
"math" | ||
"net" | ||
"strings" | ||
"sync" | ||
"time" | ||
|
||
|
@@ -87,22 +87,25 @@ var ( | |
// dialOptions configure a Dial call. dialOptions are set by the DialOption | ||
// values passed to Dial. | ||
type dialOptions struct { | ||
unaryInt UnaryClientInterceptor | ||
streamInt StreamClientInterceptor | ||
codec Codec | ||
cp Compressor | ||
dc Decompressor | ||
bs backoffStrategy | ||
balancer Balancer | ||
block bool | ||
insecure bool | ||
timeout time.Duration | ||
scChan <-chan ServiceConfig | ||
copts transport.ConnectOptions | ||
maxMsgSize int | ||
unaryInt UnaryClientInterceptor | ||
streamInt StreamClientInterceptor | ||
codec Codec | ||
cp Compressor | ||
dc Decompressor | ||
bs backoffStrategy | ||
balancer Balancer | ||
block bool | ||
insecure bool | ||
timeout time.Duration | ||
scChan <-chan ServiceConfig | ||
copts transport.ConnectOptions | ||
callOptions []CallOption | ||
} | ||
|
||
const defaultClientMaxMsgSize = math.MaxInt32 | ||
const ( | ||
defaultClientMaxReceiveMessageSize = 1024 * 1024 * 4 | ||
defaultClientMaxSendMessageSize = 1024 * 1024 * 4 | ||
) | ||
|
||
// DialOption configures how we set up the connection. | ||
type DialOption func(*dialOptions) | ||
|
@@ -123,10 +126,15 @@ func WithInitialConnWindowSize(s int32) DialOption { | |
} | ||
} | ||
|
||
// WithMaxMsgSize returns a DialOption which sets the maximum message size the client can receive. | ||
// WithMaxMsgSize returns a DialOption which sets the maximum message size the client can receive. Deprecated: use WithMaxReceiveMessageSize instead. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Also, see my following comment, |
||
func WithMaxMsgSize(s int) DialOption { | ||
return WithDefaultCallOptions(WithMaxReceiveMessageSize(s)) | ||
} | ||
|
||
// WithDefaultCallOptions returns a DialOption which sets the default CallOptions for calls over the connection. | ||
func WithDefaultCallOptions(cos ...CallOption) DialOption { | ||
return func(o *dialOptions) { | ||
o.maxMsgSize = s | ||
o.callOptions = append(o.callOptions, cos...) | ||
} | ||
} | ||
|
||
|
@@ -321,7 +329,7 @@ func DialContext(ctx context.Context, target string, opts ...DialOption) (conn * | |
conns: make(map[Address]*addrConn), | ||
} | ||
cc.ctx, cc.cancel = context.WithCancel(context.Background()) | ||
cc.dopts.maxMsgSize = defaultClientMaxMsgSize | ||
|
||
for _, opt := range opts { | ||
opt(&cc.dopts) | ||
} | ||
|
@@ -359,15 +367,16 @@ func DialContext(ctx context.Context, target string, opts ...DialOption) (conn * | |
} | ||
}() | ||
|
||
scSet := false | ||
if cc.dopts.scChan != nil { | ||
// Wait for the initial service config. | ||
// Try to get an initial service config. | ||
select { | ||
case sc, ok := <-cc.dopts.scChan: | ||
if ok { | ||
cc.sc = sc | ||
scSet = true | ||
} | ||
case <-ctx.Done(): | ||
return nil, ctx.Err() | ||
default: | ||
} | ||
} | ||
// Set defaults. | ||
|
@@ -430,7 +439,17 @@ func DialContext(ctx context.Context, target string, opts ...DialOption) (conn * | |
return nil, err | ||
} | ||
} | ||
|
||
if cc.dopts.scChan != nil && !scSet { | ||
// Blocking wait for the initial service config. | ||
select { | ||
case sc, ok := <-cc.dopts.scChan: | ||
if ok { | ||
cc.sc = sc | ||
} | ||
case <-ctx.Done(): | ||
return nil, ctx.Err() | ||
} | ||
} | ||
if cc.dopts.scChan != nil { | ||
go cc.scWatcher() | ||
} | ||
|
@@ -640,12 +659,20 @@ func (cc *ClientConn) resetAddrConn(addr Address, block bool, tearDownErr error) | |
return nil | ||
} | ||
|
||
// GetMethodConfig gets the method config of the input method. If there's no exact | ||
// match for the input method (i.e. /service/method), we will return the default | ||
// config for all methods under the service (/service/). Otherwise, we will return | ||
// an empty MethodConfig. | ||
// TODO: Avoid the locking here. | ||
func (cc *ClientConn) getMethodConfig(method string) (m MethodConfig, ok bool) { | ||
func (cc *ClientConn) GetMethodConfig(method string) MethodConfig { | ||
cc.mu.RLock() | ||
defer cc.mu.RUnlock() | ||
m, ok = cc.sc.Methods[method] | ||
return | ||
m, ok := cc.sc.Methods[method] | ||
if !ok { | ||
i := strings.LastIndex(method, "/") | ||
m, ok = cc.sc.Methods[method[:i+1]] | ||
} | ||
return m | ||
} | ||
|
||
func (cc *ClientConn) getTransport(ctx context.Context, opts BalancerGetOptions) (transport.ClientTransport, func(), error) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -137,12 +137,14 @@ func (d *gzipDecompressor) Type() string { | |
|
||
// callInfo contains all related configuration and information about an RPC. | ||
type callInfo struct { | ||
failFast bool | ||
headerMD metadata.MD | ||
trailerMD metadata.MD | ||
peer *peer.Peer | ||
traceInfo traceInfo // in trace.go | ||
creds credentials.PerRPCCredentials | ||
failFast bool | ||
headerMD metadata.MD | ||
trailerMD metadata.MD | ||
peer *peer.Peer | ||
traceInfo traceInfo // in trace.go | ||
maxReceiveMessageSize *int | ||
maxSendMessageSize *int | ||
creds credentials.PerRPCCredentials | ||
} | ||
|
||
var defaultCallInfo = callInfo{failFast: true} | ||
|
@@ -209,6 +211,22 @@ func FailFast(failFast bool) CallOption { | |
}) | ||
} | ||
|
||
// WithMaxReceiveMessageSize returns a CallOption which sets the maximum message size the client can receive. | ||
func WithMaxReceiveMessageSize(s int) CallOption { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rename this to |
||
return beforeCall(func(o *callInfo) error { | ||
o.maxReceiveMessageSize = &s | ||
return nil | ||
}) | ||
} | ||
|
||
// WithMaxSendMessageSize returns a CallOption which sets the maximum message size the client can send. | ||
func WithMaxSendMessageSize(s int) CallOption { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rename to |
||
return beforeCall(func(o *callInfo) error { | ||
o.maxSendMessageSize = &s | ||
return nil | ||
}) | ||
} | ||
|
||
// PerRPCCredentials returns a CallOption that sets credentials.PerRPCCredentials | ||
// for a call. | ||
func PerRPCCredentials(creds credentials.PerRPCCredentials) CallOption { | ||
|
@@ -251,7 +269,7 @@ type parser struct { | |
// No other error values or types must be returned, which also means | ||
// that the underlying io.Reader must not return an incompatible | ||
// error. | ||
func (p *parser) recvMsg(maxMsgSize int) (pf payloadFormat, msg []byte, err error) { | ||
func (p *parser) recvMsg(maxReceiveMessageSize int) (pf payloadFormat, msg []byte, err error) { | ||
if _, err := io.ReadFull(p.r, p.header[:]); err != nil { | ||
return 0, nil, err | ||
} | ||
|
@@ -262,8 +280,8 @@ func (p *parser) recvMsg(maxMsgSize int) (pf payloadFormat, msg []byte, err erro | |
if length == 0 { | ||
return pf, nil, nil | ||
} | ||
if length > uint32(maxMsgSize) { | ||
return 0, nil, Errorf(codes.Internal, "grpc: received message length %d exceeding the max size %d", length, maxMsgSize) | ||
if length > uint32(maxReceiveMessageSize) { | ||
return 0, nil, Errorf(codes.ResourceExhausted, "grpc: Received message larger than max (%d vs. %d)", length, maxReceiveMessageSize) | ||
} | ||
// TODO(bradfitz,zhaoq): garbage. reuse buffer after proto decoding instead | ||
// of making it for each message: | ||
|
@@ -306,7 +324,7 @@ func encode(c Codec, msg interface{}, cp Compressor, cbuf *bytes.Buffer, outPayl | |
length = uint(len(b)) | ||
} | ||
if length > math.MaxUint32 { | ||
return nil, Errorf(codes.InvalidArgument, "grpc: message too large (%d bytes)", length) | ||
return nil, Errorf(codes.ResourceExhausted, "grpc: message too large (%d bytes)", length) | ||
} | ||
|
||
const ( | ||
|
@@ -347,8 +365,8 @@ func checkRecvPayload(pf payloadFormat, recvCompress string, dc Decompressor) er | |
return nil | ||
} | ||
|
||
func recv(p *parser, c Codec, s *transport.Stream, dc Decompressor, m interface{}, maxMsgSize int, inPayload *stats.InPayload) error { | ||
pf, d, err := p.recvMsg(maxMsgSize) | ||
func recv(p *parser, c Codec, s *transport.Stream, dc Decompressor, m interface{}, maxReceiveMessageSize int, inPayload *stats.InPayload) error { | ||
pf, d, err := p.recvMsg(maxReceiveMessageSize) | ||
if err != nil { | ||
return err | ||
} | ||
|
@@ -364,10 +382,10 @@ func recv(p *parser, c Codec, s *transport.Stream, dc Decompressor, m interface{ | |
return Errorf(codes.Internal, "grpc: failed to decompress the received message %v", err) | ||
} | ||
} | ||
if len(d) > maxMsgSize { | ||
if len(d) > maxReceiveMessageSize { | ||
// TODO: Revisit the error code. Currently keep it consistent with java | ||
// implementation. | ||
return Errorf(codes.Internal, "grpc: received a message of %d bytes exceeding %d limit", len(d), maxMsgSize) | ||
return Errorf(codes.ResourceExhausted, "grpc: Received message larger than max (%d vs. %d)", len(d), maxReceiveMessageSize) | ||
} | ||
if err := c.Unmarshal(d, m); err != nil { | ||
return Errorf(codes.Internal, "grpc: failed to unmarshal the received message %v", err) | ||
|
@@ -493,24 +511,22 @@ type MethodConfig struct { | |
// WaitForReady indicates whether RPCs sent to this method should wait until | ||
// the connection is ready by default (!failfast). The value specified via the | ||
// gRPC client API will override the value set here. | ||
WaitForReady bool | ||
WaitForReady *bool | ||
// Timeout is the default timeout for RPCs sent to this method. The actual | ||
// deadline used will be the minimum of the value specified here and the value | ||
// set by the application via the gRPC client API. If either one is not set, | ||
// then the other will be used. If neither is set, then the RPC has no deadline. | ||
Timeout time.Duration | ||
Timeout *time.Duration | ||
// MaxReqSize is the maximum allowed payload size for an individual request in a | ||
// stream (client->server) in bytes. The size which is measured is the serialized | ||
// payload after per-message compression (but before stream compression) in bytes. | ||
// The actual value used is the minumum of the value specified here and the value set | ||
// by the application via the gRPC client API. If either one is not set, then the other | ||
// will be used. If neither is set, then the built-in default is used. | ||
// TODO: support this. | ||
MaxReqSize uint32 | ||
MaxReqSize *int | ||
// MaxRespSize is the maximum allowed payload size for an individual response in a | ||
// stream (server->client) in bytes. | ||
// TODO: support this. | ||
MaxRespSize uint32 | ||
MaxRespSize *int | ||
} | ||
|
||
// ServiceConfig is provided by the service provider and contains parameters for how | ||
|
@@ -521,9 +537,32 @@ type ServiceConfig struct { | |
// via grpc.WithBalancer will override this. | ||
LB Balancer | ||
// Methods contains a map for the methods in this service. | ||
// If there is an exact match for a method (i.e. /service/method) in the map, use the corresponding MethodConfig. | ||
// If there's no exact match, look for the default config for all methods under the service (/service/) and use the corresponding MethodConfig. | ||
// Otherwise, the method has no MethodConfig to use. | ||
Methods map[string]MethodConfig | ||
} | ||
|
||
func min(a, b *int) *int { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: move these two new functions before the |
||
if *a < *b { | ||
return a | ||
} | ||
return b | ||
} | ||
|
||
func getMaxSize(mcMax, doptMax *int, defaultVal int) *int { | ||
if mcMax == nil && doptMax == nil { | ||
return &defaultVal | ||
} | ||
if mcMax != nil && doptMax != nil { | ||
return min(mcMax, doptMax) | ||
} | ||
if mcMax != nil { | ||
return mcMax | ||
} | ||
return doptMax | ||
} | ||
|
||
// SupportPackageIsVersion4 is referenced from generated protocol buffer files | ||
// to assert that that code is compatible with this version of the grpc package. | ||
// | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SGTM. A broken invariant is an internal problem. (Please remove comment.)