Skip to content

Commit 44827e1

Browse files
authored
[Bugfix] Custom triple server message size (apache#2246)
* fix apache#2176
1 parent edf89ae commit 44827e1

13 files changed

+97
-38
lines changed

common/constant/default.go

+10
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
package constant
1919

20+
import "math"
21+
2022
const (
2123
Dubbo = "dubbo"
2224
ProviderProtocol = "provider"
@@ -91,3 +93,11 @@ const (
9193
ServiceDiscoveryDefaultGroup = "DEFAULT_GROUP"
9294
NotAvailable = "N/A"
9395
)
96+
97+
const (
98+
DefaultMaxServerRecvMsgSize = 1024 * 1024 * 4
99+
DefaultMaxServerSendMsgSize = math.MaxInt32
100+
101+
DefaultMaxCallRecvMsgSize = 1024 * 1024 * 4
102+
DefaultMaxCallSendMsgSize = math.MaxInt32
103+
)

config/protocol_config.go

+16
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ type ProtocolConfig struct {
3131
Ip string `yaml:"ip" json:"ip,omitempty" property:"ip"`
3232
Port string `default:"20000" yaml:"port" json:"port,omitempty" property:"port"`
3333
Params interface{} `yaml:"params" json:"params,omitempty" property:"params"`
34+
35+
// MaxServerSendMsgSize max size of server send message, 1mb=1000kb=1000000b 1mib=1024kb=1048576b.
36+
// more detail to see https://pkg.go.dev/github.com/dustin/go-humanize#pkg-constants
37+
MaxServerSendMsgSize string `yaml:"max-server-send-msg-size" json:"max-server-send-msg-size,omitempty"`
38+
// MaxServerRecvMsgSize max size of server receive message
39+
MaxServerRecvMsgSize string `default:"4mib" yaml:"max-server-recv-msg-size" json:"max-server-recv-msg-size,omitempty"`
3440
}
3541

3642
// Prefix dubbo.config-center
@@ -77,6 +83,16 @@ func (pcb *ProtocolConfigBuilder) SetParams(params interface{}) *ProtocolConfigB
7783
return pcb
7884
}
7985

86+
func (pcb *ProtocolConfigBuilder) SetMaxServerSendMsgSize(maxServerSendMsgSize string) *ProtocolConfigBuilder {
87+
pcb.protocolConfig.MaxServerSendMsgSize = maxServerSendMsgSize
88+
return pcb
89+
}
90+
91+
func (pcb *ProtocolConfigBuilder) SetMaxServerRecvMsgSize(maxServerRecvMsgSize string) *ProtocolConfigBuilder {
92+
pcb.protocolConfig.MaxServerRecvMsgSize = maxServerRecvMsgSize
93+
return pcb
94+
}
95+
8096
func (pcb *ProtocolConfigBuilder) Build() *ProtocolConfig {
8197
return pcb.protocolConfig
8298
}

config/protocol_config_test.go

+3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ func TestGetProtocolsConfig(t *testing.T) {
3535
// default
3636
assert.Equal(t, "dubbo", protocols["dubbo"].Name)
3737
assert.Equal(t, string("20000"), protocols["dubbo"].Port)
38+
assert.Equal(t, "4mib", protocols["dubbo"].MaxServerRecvMsgSize)
3839
})
3940

4041
t.Run("use config", func(t *testing.T) {
@@ -45,5 +46,7 @@ func TestGetProtocolsConfig(t *testing.T) {
4546
// default
4647
assert.Equal(t, "dubbo", protocols["dubbo"].Name)
4748
assert.Equal(t, string("20000"), protocols["dubbo"].Port)
49+
assert.Equal(t, "4mib", protocols["dubbo"].MaxServerSendMsgSize)
50+
assert.Equal(t, "4mib", protocols["dubbo"].MaxServerRecvMsgSize)
4851
})
4952
}

config/service_config.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ type ServiceConfig struct {
7676
NotRegister bool `yaml:"not_register" json:"not_register,omitempty" property:"not_register"`
7777
ParamSign string `yaml:"param.sign" json:"param.sign,omitempty" property:"param.sign"`
7878
Tag string `yaml:"tag" json:"tag,omitempty" property:"tag"`
79-
GrpcMaxMessageSize int `default:"4" yaml:"max_message_size" json:"max_message_size,omitempty"`
8079
TracingKey string `yaml:"tracing-key" json:"tracing-key,omitempty" propertiy:"tracing-key"`
8180

8281
RCProtocolsMap map[string]*ProtocolConfig
@@ -278,6 +277,9 @@ func (s *ServiceConfig) Export() error {
278277
common.WithMethods(strings.Split(methods, ",")),
279278
common.WithToken(s.Token),
280279
common.WithParamsValue(constant.MetadataTypeKey, s.metadataType),
280+
// fix https://github.com/apache/dubbo-go/issues/2176
281+
common.WithParamsValue(constant.MaxServerSendMsgSize, proto.MaxServerSendMsgSize),
282+
common.WithParamsValue(constant.MaxServerRecvMsgSize, proto.MaxServerRecvMsgSize),
281283
)
282284
if len(s.Tag) > 0 {
283285
ivkURL.AddParam(constant.Tagkey, s.Tag)
@@ -331,13 +333,13 @@ func (s *ServiceConfig) Export() error {
331333
return nil
332334
}
333335

334-
//setRegistrySubURL set registry sub url is ivkURl
336+
// setRegistrySubURL set registry sub url is ivkURl
335337
func setRegistrySubURL(ivkURL *common.URL, regUrl *common.URL) {
336338
ivkURL.AddParam(constant.RegistryKey, regUrl.GetParam(constant.RegistryKey, ""))
337339
regUrl.SubURL = ivkURL
338340
}
339341

340-
//loadProtocol filter protocols by ids
342+
// loadProtocol filter protocols by ids
341343
func loadProtocol(protocolIds []string, protocols map[string]*ProtocolConfig) []*ProtocolConfig {
342344
returnProtocols := make([]*ProtocolConfig, 0, len(protocols))
343345
for _, v := range protocolIds {
@@ -435,7 +437,6 @@ func (s *ServiceConfig) getUrlMap() url.Values {
435437
urlMap.Set(constant.RegistryRoleKey, strconv.Itoa(common.PROVIDER))
436438
urlMap.Set(constant.ReleaseKey, "dubbo-golang-"+constant.Version)
437439
urlMap.Set(constant.SideKey, (common.RoleType(common.PROVIDER)).Role())
438-
urlMap.Set(constant.MessageSizeKey, strconv.Itoa(s.GrpcMaxMessageSize))
439440
// todo: move
440441
urlMap.Set(constant.SerializationKey, s.Serialization)
441442
// application config info

config/testdata/config/protocol/application.yaml

+6-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,9 @@ dubbo:
44
timeout: 5s
55
group: dev
66
address: nacos://127.0.0.1:8848
7-
protocols:
7+
protocols:
8+
dubbo:
9+
name: dubbo
10+
port: 20000
11+
max-server-send-msg-size: 4mib
12+
max-server-recv-msg-size: 4mib

go.mod

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ require (
1717
github.com/dubbogo/go-zookeeper v1.0.4-0.20211212162352-f9d2183d89d5
1818
github.com/dubbogo/gost v1.13.2
1919
github.com/dubbogo/grpc-go v1.42.10
20-
github.com/dubbogo/triple v1.2.2-rc2
20+
github.com/dubbogo/triple v1.2.2-rc3
21+
github.com/dustin/go-humanize v1.0.0
2122
github.com/emicklei/go-restful/v3 v3.10.1
2223
github.com/envoyproxy/go-control-plane v0.11.0
2324
github.com/fsnotify/fsnotify v1.6.0

go.sum

+4-5
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,6 @@ github.com/apache/dubbo-getty v1.4.9 h1:Y8l1EYJqIc7BnmyfYtvG4H4Nmu4v7P1uS31fFQGd
405405
github.com/apache/dubbo-getty v1.4.9/go.mod h1:6qmrqBSPGs3B35zwEuGhEYNVsx1nfGT/xzV2yOt2amM=
406406
github.com/apache/dubbo-go-hessian2 v1.9.1/go.mod h1:xQUjE7F8PX49nm80kChFvepA/AvqAZ0oh/UaB6+6pBE=
407407
github.com/apache/dubbo-go-hessian2 v1.9.3/go.mod h1:xQUjE7F8PX49nm80kChFvepA/AvqAZ0oh/UaB6+6pBE=
408-
github.com/apache/dubbo-go-hessian2 v1.11.4/go.mod h1:QP9Tc0w/B/mDopjusebo/c7GgEfl6Lz8jeuFg8JA6yw=
409408
github.com/apache/dubbo-go-hessian2 v1.12.0 h1:n2JXPMGc4u/ihBbOt25d3mmv1k92X9TvLnqfgyNscKQ=
410409
github.com/apache/dubbo-go-hessian2 v1.12.0/go.mod h1:QP9Tc0w/B/mDopjusebo/c7GgEfl6Lz8jeuFg8JA6yw=
411410
github.com/apache/thrift v0.12.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb+bacwQ=
@@ -524,8 +523,8 @@ github.com/dubbogo/grpc-go v1.42.10/go.mod h1:JMkPt1mIHL96GAFeYsMoMjew6f1ROKycik
524523
github.com/dubbogo/jsonparser v1.0.1/go.mod h1:tYAtpctvSP/tWw4MeelsowSPgXQRVHHWbqL6ynps8jU=
525524
github.com/dubbogo/net v0.0.4/go.mod h1:1CGOnM7X3he+qgGNqjeADuE5vKZQx/eMSeUkpU3ujIc=
526525
github.com/dubbogo/triple v1.0.9/go.mod h1:1t9me4j4CTvNDcsMZy6/OGarbRyAUSY0tFXGXHCp7Iw=
527-
github.com/dubbogo/triple v1.2.2-rc2 h1:2AaLd+uKwnNnR3qOIXTNPU/OHk77qIDNGMX3GstEtaY=
528-
github.com/dubbogo/triple v1.2.2-rc2/go.mod h1:8qprF2uJX82IE5hjiIuswp416sEr0oL/+bb7IjiizYs=
526+
github.com/dubbogo/triple v1.2.2-rc3 h1:9rxLqru35MmJkypCHJMiZb1VzwH+zmbPBend9Cq+VOI=
527+
github.com/dubbogo/triple v1.2.2-rc3/go.mod h1:9pgEahtmsY/avYJp3dzUQE8CMMVe1NtGBmUhfICKLJk=
529528
github.com/dustin/go-humanize v0.0.0-20171111073723-bb3d318650d4/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
530529
github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=
531530
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
@@ -1154,8 +1153,8 @@ github.com/tmc/grpc-websocket-proxy v0.0.0-20201229170055-e5319fda7802 h1:uruHq4
11541153
github.com/tmc/grpc-websocket-proxy v0.0.0-20201229170055-e5319fda7802/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
11551154
github.com/toolkits/concurrent v0.0.0-20150624120057-a4371d70e3e3/go.mod h1:QDlpd3qS71vYtakd2hmdpqhJ9nwv6mD6A30bQ1BPBFE=
11561155
github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926/go.mod h1:9ESjWnEqriFuLhtthL60Sar/7RFoluCcXsuvEwTV5KM=
1157-
github.com/uber/jaeger-client-go v2.30.0+incompatible h1:D6wyKGCecFaSRUpo8lCVbaOOb6ThwMmTEbhRwtKR97o=
1158-
github.com/uber/jaeger-client-go v2.30.0+incompatible/go.mod h1:WVhlPFC8FDjOFMMWRy2pZqQJSXxYSwNYOkTr/Z6d3Kk=
1156+
github.com/uber/jaeger-client-go v2.29.1+incompatible h1:R9ec3zO3sGpzs0abd43Y+fBZRJ9uiH6lXyR/+u6brW4=
1157+
github.com/uber/jaeger-client-go v2.29.1+incompatible/go.mod h1:WVhlPFC8FDjOFMMWRy2pZqQJSXxYSwNYOkTr/Z6d3Kk=
11591158
github.com/uber/jaeger-lib v2.4.1+incompatible h1:td4jdvLcExb4cBISKIpHuGoVXh+dVKhn2Um6rjCsSsg=
11601159
github.com/uber/jaeger-lib v2.4.1+incompatible/go.mod h1:ComeNDZlWwrWnDv8aPp0Ba6+uUTzImX/AauajbLI56U=
11611160
github.com/ugorji/go v1.2.6 h1:tGiWC9HENWE2tqYycIqFTNorMmFRVhNwCpDOpWqnk8E=

protocol/dubbo3/dubbo3_invoker.go

+9-8
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828

2929
import (
3030
"github.com/dubbogo/gost/log/logger"
31+
"github.com/dustin/go-humanize"
3132

3233
"github.com/dubbogo/grpc-go/metadata"
3334

@@ -84,16 +85,16 @@ func NewDubboInvoker(url *common.URL) (*DubboInvoker, error) {
8485
triConfig.WithHeaderGroup(url.GetParam(constant.GroupKey, "")),
8586
triConfig.WithLogger(logger.GetLogger()),
8687
}
87-
if maxCall := url.GetParam(constant.MaxCallRecvMsgSize, ""); maxCall != "" {
88-
if size, err := strconv.Atoi(maxCall); err == nil && size != 0 {
89-
opts = append(opts, triConfig.WithGRPCMaxCallRecvMessageSize(size))
90-
}
88+
maxCallRecvMsgSize := constant.DefaultMaxCallRecvMsgSize
89+
if maxCall, err := humanize.ParseBytes(url.GetParam(constant.MaxCallRecvMsgSize, "")); err == nil && maxCall != 0 {
90+
maxCallRecvMsgSize = int(maxCall)
9191
}
92-
if maxCall := url.GetParam(constant.MaxCallSendMsgSize, ""); maxCall != "" {
93-
if size, err := strconv.Atoi(maxCall); err == nil && size != 0 {
94-
opts = append(opts, triConfig.WithGRPCMaxCallSendMessageSize(size))
95-
}
92+
maxCallSendMsgSize := constant.DefaultMaxCallSendMsgSize
93+
if maxCall, err := humanize.ParseBytes(url.GetParam(constant.MaxCallSendMsgSize, "")); err == nil && maxCall != 0 {
94+
maxCallSendMsgSize = int(maxCall)
9695
}
96+
opts = append(opts, triConfig.WithGRPCMaxCallRecvMessageSize(maxCallRecvMsgSize))
97+
opts = append(opts, triConfig.WithGRPCMaxCallSendMessageSize(maxCallSendMsgSize))
9798

9899
tracingKey := url.GetParam(constant.TracingConfigKey, "")
99100
if tracingKey != "" {

protocol/dubbo3/dubbo3_protocol.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ import (
2121
"context"
2222
"fmt"
2323
"reflect"
24-
"strconv"
2524
"sync"
2625
)
2726

2827
import (
2928
"github.com/dubbogo/gost/log/logger"
29+
"github.com/dustin/go-humanize"
3030

3131
"github.com/dubbogo/grpc-go"
3232
"github.com/dubbogo/grpc-go/metadata"
@@ -242,16 +242,16 @@ func (dp *DubboProtocol) openServer(url *common.URL, tripleCodecType tripleConst
242242
}
243243
}
244244

245-
if maxCall := url.GetParam(constant.MaxServerRecvMsgSize, ""); maxCall != "" {
246-
if size, err := strconv.Atoi(maxCall); err == nil && size != 0 {
247-
opts = append(opts, triConfig.WithGRPCMaxServerRecvMessageSize(size))
248-
}
245+
maxServerRecvMsgSize := constant.DefaultMaxServerRecvMsgSize
246+
if recvMsgSize, err := humanize.ParseBytes(url.GetParam(constant.MaxServerRecvMsgSize, "")); err == nil && recvMsgSize != 0 {
247+
maxServerRecvMsgSize = int(recvMsgSize)
249248
}
250-
if maxCall := url.GetParam(constant.MaxServerSendMsgSize, ""); maxCall != "" {
251-
if size, err := strconv.Atoi(maxCall); err == nil && size != 0 {
252-
opts = append(opts, triConfig.WithGRPCMaxServerSendMessageSize(size))
253-
}
249+
maxServerSendMsgSize := constant.DefaultMaxServerSendMsgSize
250+
if sendMsgSize, err := humanize.ParseBytes(url.GetParam(constant.MaxServerSendMsgSize, "")); err == nil && sendMsgSize != 0 {
251+
maxServerSendMsgSize = int(sendMsgSize)
254252
}
253+
opts = append(opts, triConfig.WithGRPCMaxServerRecvMessageSize(maxServerRecvMsgSize))
254+
opts = append(opts, triConfig.WithGRPCMaxServerSendMessageSize(maxServerSendMsgSize))
255255

256256
triOption := triConfig.NewTripleOption(opts...)
257257

protocol/grpc/client.go

+13-4
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ package grpc
1919

2020
import (
2121
"reflect"
22-
"strconv"
2322
"sync"
2423
"time"
2524
)
2625

2726
import (
2827
"github.com/dubbogo/gost/log/logger"
28+
"github.com/dustin/go-humanize"
2929

3030
"github.com/grpc-ecosystem/grpc-opentracing/go/otgrpc"
3131

@@ -61,7 +61,16 @@ func NewClient(url *common.URL) (*Client, error) {
6161
// If not, will return NoopTracer.
6262
tracer := opentracing.GlobalTracer()
6363
dialOpts := make([]grpc.DialOption, 0, 4)
64-
maxMessageSize, _ := strconv.Atoi(url.GetParam(constant.MessageSizeKey, "4"))
64+
65+
// set max send and recv msg size
66+
maxCallRecvMsgSize := constant.DefaultMaxCallRecvMsgSize
67+
if recvMsgSize, err := humanize.ParseBytes(url.GetParam(constant.MaxCallRecvMsgSize, "")); err == nil && recvMsgSize > 0 {
68+
maxCallRecvMsgSize = int(recvMsgSize)
69+
}
70+
maxCallSendMsgSize := constant.DefaultMaxCallSendMsgSize
71+
if sendMsgSize, err := humanize.ParseBytes(url.GetParam(constant.MaxCallSendMsgSize, "")); err == nil && sendMsgSize > 0 {
72+
maxCallSendMsgSize = int(sendMsgSize)
73+
}
6574

6675
// consumer config client connectTimeout
6776
//connectTimeout := config.GetConsumerConfig().ConnectTimeout
@@ -74,8 +83,8 @@ func NewClient(url *common.URL) (*Client, error) {
7483
grpc.WithStreamInterceptor(otgrpc.OpenTracingStreamClientInterceptor(tracer, otgrpc.LogPayloads())),
7584
grpc.WithDefaultCallOptions(
7685
grpc.CallContentSubtype(clientConf.ContentSubType),
77-
grpc.MaxCallRecvMsgSize(1024*1024*maxMessageSize),
78-
grpc.MaxCallSendMsgSize(1024*1024*maxMessageSize),
86+
grpc.MaxCallRecvMsgSize(maxCallRecvMsgSize),
87+
grpc.MaxCallSendMsgSize(maxCallSendMsgSize),
7988
),
8089
)
8190
tlsConfig := config.GetRootConfig().TLSConfig

protocol/grpc/client_test.go

+7
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ package grpc
1919

2020
import (
2121
"context"
22+
"fmt"
23+
"github.com/dustin/go-humanize"
2224
"testing"
2325
)
2426

@@ -88,3 +90,8 @@ func TestStreamClient(t *testing.T) {
8890
assert.NoError(t, err)
8991
routeguide.RunRouteChat(routeChatStream)
9092
}
93+
94+
func TestT(t *testing.T) {
95+
bytes, err := humanize.ParseBytes("0")
96+
fmt.Println(bytes, err)
97+
}

protocol/grpc/grpc_protocol.go

-4
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
package grpc
1919

2020
import (
21-
"strconv"
2221
"sync"
2322
)
2423

@@ -28,7 +27,6 @@ import (
2827

2928
import (
3029
"dubbo.apache.org/dubbo-go/v3/common"
31-
"dubbo.apache.org/dubbo-go/v3/common/constant"
3230
"dubbo.apache.org/dubbo-go/v3/common/extension"
3331
"dubbo.apache.org/dubbo-go/v3/protocol"
3432
)
@@ -82,9 +80,7 @@ func (gp *GrpcProtocol) openServer(url *common.URL) {
8280
panic("[GrpcProtocol]" + url.Key() + "is not existing")
8381
}
8482

85-
grpcMessageSize, _ := strconv.Atoi(url.GetParam(constant.MessageSizeKey, "4"))
8683
srv := NewServer()
87-
srv.SetBufferSize(grpcMessageSize)
8884
gp.serverMap[url.Location] = srv
8985
srv.Start(url)
9086
}

protocol/grpc/server.go

+13-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727

2828
import (
2929
"github.com/dubbogo/gost/log/logger"
30+
"github.com/dustin/go-humanize"
3031

3132
"github.com/grpc-ecosystem/grpc-opentracing/go/otgrpc"
3233

@@ -40,6 +41,7 @@ import (
4041

4142
import (
4243
"dubbo.apache.org/dubbo-go/v3/common"
44+
"dubbo.apache.org/dubbo-go/v3/common/constant"
4345
"dubbo.apache.org/dubbo-go/v3/config"
4446
"dubbo.apache.org/dubbo-go/v3/protocol"
4547
)
@@ -81,15 +83,24 @@ func (s *Server) Start(url *common.URL) {
8183
panic(err)
8284
}
8385

86+
maxServerRecvMsgSize := constant.DefaultMaxServerRecvMsgSize
87+
if recvMsgSize, convertErr := humanize.ParseBytes(url.GetParam(constant.MaxServerRecvMsgSize, "")); convertErr == nil && recvMsgSize != 0 {
88+
maxServerRecvMsgSize = int(recvMsgSize)
89+
}
90+
maxServerSendMsgSize := constant.DefaultMaxServerSendMsgSize
91+
if sendMsgSize, convertErr := humanize.ParseBytes(url.GetParam(constant.MaxServerSendMsgSize, "")); err == convertErr && sendMsgSize != 0 {
92+
maxServerSendMsgSize = int(sendMsgSize)
93+
}
94+
8495
// If global trace instance was set, then server tracer instance
8596
// can be get. If not, will return NoopTracer.
8697
tracer := opentracing.GlobalTracer()
8798
var serverOpts []grpc.ServerOption
8899
serverOpts = append(serverOpts,
89100
grpc.UnaryInterceptor(otgrpc.OpenTracingServerInterceptor(tracer)),
90101
grpc.StreamInterceptor(otgrpc.OpenTracingStreamServerInterceptor(tracer)),
91-
grpc.MaxRecvMsgSize(1024*1024*s.bufferSize),
92-
grpc.MaxSendMsgSize(1024*1024*s.bufferSize),
102+
grpc.MaxRecvMsgSize(maxServerRecvMsgSize),
103+
grpc.MaxSendMsgSize(maxServerSendMsgSize),
93104
)
94105

95106
tlsConfig := config.GetRootConfig().TLSConfig

0 commit comments

Comments
 (0)