update the merge of client api and sc
This commit is contained in:
28
call.go
28
call.go
@ -152,9 +152,7 @@ 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
|
||||
maxReceiveMessageSize := defaultClientMaxReceiveMessageSize
|
||||
maxSendMessageSize := defaultClientMaxSendMessageSize
|
||||
if mc, ok := cc.GetMethodConfig(method); ok {
|
||||
mc := cc.GetMethodConfig(method)
|
||||
if mc.WaitForReady != nil {
|
||||
c.failFast = !*mc.WaitForReady
|
||||
}
|
||||
@ -165,29 +163,9 @@ func invoke(ctx context.Context, method string, args, reply interface{}, cc *Cli
|
||||
defer cancel()
|
||||
}
|
||||
|
||||
if mc.MaxReqSize != nil && cc.dopts.maxSendMessageSize >= 0 {
|
||||
maxSendMessageSize = min(*mc.MaxReqSize, cc.dopts.maxSendMessageSize)
|
||||
} else if mc.MaxReqSize != nil {
|
||||
maxSendMessageSize = *mc.MaxReqSize
|
||||
} else if mc.MaxReqSize == nil && cc.dopts.maxSendMessageSize >= 0 {
|
||||
maxSendMessageSize = cc.dopts.maxSendMessageSize
|
||||
}
|
||||
maxSendMessageSize := getMaxSize(mc.MaxReqSize, cc.dopts.maxSendMessageSize, defaultClientMaxSendMessageSize)
|
||||
maxReceiveMessageSize := getMaxSize(mc.MaxRespSize, cc.dopts.maxReceiveMessageSize, defaultClientMaxReceiveMessageSize)
|
||||
|
||||
if mc.MaxRespSize != nil && cc.dopts.maxReceiveMessageSize >= 0 {
|
||||
maxReceiveMessageSize = min(*mc.MaxRespSize, cc.dopts.maxReceiveMessageSize)
|
||||
} else if mc.MaxRespSize != nil {
|
||||
maxReceiveMessageSize = *mc.MaxRespSize
|
||||
} else if mc.MaxRespSize == nil && cc.dopts.maxReceiveMessageSize >= 0 {
|
||||
maxReceiveMessageSize = cc.dopts.maxReceiveMessageSize
|
||||
}
|
||||
} else {
|
||||
if cc.dopts.maxSendMessageSize >= 0 {
|
||||
maxSendMessageSize = cc.dopts.maxSendMessageSize
|
||||
}
|
||||
if cc.dopts.maxReceiveMessageSize >= 0 {
|
||||
maxReceiveMessageSize = cc.dopts.maxReceiveMessageSize
|
||||
}
|
||||
}
|
||||
for _, o := range opts {
|
||||
if err := o.before(&c); err != nil {
|
||||
return toRPCErr(err)
|
||||
|
@ -98,8 +98,8 @@ type dialOptions struct {
|
||||
timeout time.Duration
|
||||
scChan <-chan ServiceConfig
|
||||
copts transport.ConnectOptions
|
||||
maxReceiveMessageSize int
|
||||
maxSendMessageSize int
|
||||
maxReceiveMessageSize *int
|
||||
maxSendMessageSize *int
|
||||
}
|
||||
|
||||
const (
|
||||
@ -120,14 +120,14 @@ func WithMaxMsgSize(s int) DialOption {
|
||||
// WithMaxReceiveMessageSize returns a DialOption which sets the maximum message size the client can receive. Negative input is invalid and has the same effect as not setting the field.
|
||||
func WithMaxReceiveMessageSize(s int) DialOption {
|
||||
return func(o *dialOptions) {
|
||||
o.maxReceiveMessageSize = s
|
||||
*o.maxReceiveMessageSize = s
|
||||
}
|
||||
}
|
||||
|
||||
// WithMaxSendMessageSize returns a DialOption which sets the maximum message size the client can send. Negative input is invalid and has the same effect as not seeting the field.
|
||||
func WithMaxSendMessageSize(s int) DialOption {
|
||||
return func(o *dialOptions) {
|
||||
o.maxSendMessageSize = s
|
||||
*o.maxSendMessageSize = s
|
||||
}
|
||||
}
|
||||
|
||||
@ -324,10 +324,6 @@ func DialContext(ctx context.Context, target string, opts ...DialOption) (conn *
|
||||
}
|
||||
cc.ctx, cc.cancel = context.WithCancel(context.Background())
|
||||
|
||||
// initialize maxReceiveMessageSize and maxSendMessageSize to -1 before applying DialOption functions to distinguish whether the user set the message limit or not.
|
||||
cc.dopts.maxReceiveMessageSize = -1
|
||||
cc.dopts.maxSendMessageSize = -1
|
||||
|
||||
for _, opt := range opts {
|
||||
opt(&cc.dopts)
|
||||
}
|
||||
@ -646,13 +642,13 @@ func (cc *ClientConn) resetAddrConn(addr Address, block bool, tearDownErr error)
|
||||
|
||||
// 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/).
|
||||
// TODO: Avoid the locking here.
|
||||
func (cc *ClientConn) GetMethodConfig(method string) (m MethodConfig, ok bool) {
|
||||
func (cc *ClientConn) GetMethodConfig(method string) (m MethodConfig) {
|
||||
cc.mu.RLock()
|
||||
defer cc.mu.RUnlock()
|
||||
m, ok = cc.sc.Methods[method]
|
||||
m, ok := cc.sc.Methods[method]
|
||||
if !ok {
|
||||
i := strings.LastIndex(method, "/")
|
||||
m, ok = cc.sc.Methods[method[:i+1]]
|
||||
m, _ = cc.sc.Methods[method[:i+1]]
|
||||
}
|
||||
return
|
||||
}
|
||||
|
13
rpc_util.go
13
rpc_util.go
@ -483,4 +483,17 @@ func min(a, b int) int {
|
||||
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
|
||||
}
|
||||
|
||||
const grpcUA = "grpc-go/" + Version
|
||||
|
29
stream.go
29
stream.go
@ -113,9 +113,7 @@ func newClientStream(ctx context.Context, desc *StreamDesc, cc *ClientConn, meth
|
||||
cancel context.CancelFunc
|
||||
)
|
||||
c := defaultCallInfo
|
||||
maxReceiveMessageSize := defaultClientMaxReceiveMessageSize
|
||||
maxSendMessageSize := defaultClientMaxSendMessageSize
|
||||
if mc, ok := cc.GetMethodConfig(method); ok {
|
||||
mc := cc.GetMethodConfig(method)
|
||||
if mc.WaitForReady != nil {
|
||||
c.failFast = !*mc.WaitForReady
|
||||
}
|
||||
@ -126,29 +124,8 @@ func newClientStream(ctx context.Context, desc *StreamDesc, cc *ClientConn, meth
|
||||
defer cancel()
|
||||
}
|
||||
|
||||
if mc.MaxReqSize != nil && cc.dopts.maxSendMessageSize >= 0 {
|
||||
maxSendMessageSize = min(*mc.MaxReqSize, cc.dopts.maxSendMessageSize)
|
||||
} else if mc.MaxReqSize != nil {
|
||||
maxSendMessageSize = *mc.MaxReqSize
|
||||
} else if mc.MaxReqSize == nil && cc.dopts.maxSendMessageSize >= 0 {
|
||||
maxSendMessageSize = cc.dopts.maxSendMessageSize
|
||||
}
|
||||
|
||||
if mc.MaxRespSize != nil && cc.dopts.maxReceiveMessageSize >= 0 {
|
||||
maxReceiveMessageSize = min(*mc.MaxRespSize, cc.dopts.maxReceiveMessageSize)
|
||||
} else if mc.MaxRespSize != nil {
|
||||
maxReceiveMessageSize = *mc.MaxRespSize
|
||||
} else if mc.MaxRespSize == nil && cc.dopts.maxReceiveMessageSize >= 0 {
|
||||
maxReceiveMessageSize = cc.dopts.maxReceiveMessageSize
|
||||
}
|
||||
} else {
|
||||
if cc.dopts.maxSendMessageSize >= 0 {
|
||||
maxSendMessageSize = cc.dopts.maxSendMessageSize
|
||||
}
|
||||
if cc.dopts.maxReceiveMessageSize >= 0 {
|
||||
maxReceiveMessageSize = cc.dopts.maxReceiveMessageSize
|
||||
}
|
||||
}
|
||||
maxSendMessageSize := getMaxSize(mc.MaxReqSize, cc.dopts.maxSendMessageSize, defaultClientMaxSendMessageSize)
|
||||
maxReceiveMessageSize := getMaxSize(mc.MaxRespSize, cc.dopts.maxReceiveMessageSize, defaultClientMaxReceiveMessageSize)
|
||||
for _, o := range opts {
|
||||
if err := o.before(&c); err != nil {
|
||||
return nil, toRPCErr(err)
|
||||
|
Reference in New Issue
Block a user