Skip to content

Commit

Permalink
fix: 兼容 grpc.MaxCallRecvMsgSize 写法
Browse files Browse the repository at this point in the history
  • Loading branch information
kl7sn committed Feb 19, 2024
1 parent d421906 commit 40b214f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
4 changes: 3 additions & 1 deletion client/egrpc/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"github.com/gotomicro/ego/core/util/xtime"
)

const DefaultMaxCallRecvMsgSize = 1024 * 1024 * 4

// Config ...
type Config struct {
Addr string // 连接地址,直连为127.0.0.1:9001,服务发现为etcd:///appname
Expand Down Expand Up @@ -58,6 +60,6 @@ func DefaultConfig() *Config {
EnableAccessInterceptorRes: false,
EnableServiceConfig: true,
// EnableCPUUsage: true,
MaxCallRecvMsgSize: 1024 * 1024 * 4,
MaxCallRecvMsgSize: DefaultMaxCallRecvMsgSize,
}
}
8 changes: 5 additions & 3 deletions client/egrpc/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,13 @@ func (c *Container) Build(options ...Option) *Component {
WithDialOption(grpc.WithChainUnaryInterceptor(c.metricUnaryClientInterceptor())),
)
}
options = append(options, WithDialOption(grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(c.config.MaxCallRecvMsgSize))))

for _, option := range options {
option(c)
}

// 兼容代码直接配置 grpc.MaxCallRecvMsgSize
// 并保持配置文件高优先级
if c.config.MaxCallRecvMsgSize != DefaultMaxCallRecvMsgSize {
WithDialOption(grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(c.config.MaxCallRecvMsgSize)))(c)
}
return newComponent(c.name, c.config, c.logger)
}
17 changes: 16 additions & 1 deletion client/egrpc/option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import (
"time"

"github.com/BurntSushi/toml"
"github.com/gotomicro/ego/core/econf"
"github.com/stretchr/testify/assert"
"google.golang.org/grpc"
"google.golang.org/grpc/test/bufconn"

"github.com/gotomicro/ego/core/econf"
)

func newCmp(t *testing.T, opt Option) *Component {
Expand Down Expand Up @@ -74,3 +76,16 @@ func TestWithName(t *testing.T) {
cmp := newCmp(t, opt)
assert.Equal(t, "hello", cmp.name)
}

func TestMaxCallRecvMsgSize(t *testing.T) {
opt := WithMaxRecvMsgSize(1024)
cmp := newCmp(t, opt)
assert.Equal(t, 1024, cmp.config.MaxCallRecvMsgSize)
}

func TestMaxCallRecvMsgSizeWithDialOption(t *testing.T) {
var opts []grpc.DialOption
opts = append(opts, grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(2048)))
cmp := newCmp(t, WithDialOption(opts...))
assert.Equal(t, "grpc", cmp.name)
}

0 comments on commit 40b214f

Please sign in to comment.