Skip to content

Commit 06c0736

Browse files
authored
Merge pull request #18991 from serathius/reuse-grpc-overhead
Reuse grpcOverheadBytes calculation by creating a function for that
2 parents 6fa7342 + c77ea65 commit 06c0736

File tree

4 files changed

+17
-10
lines changed

4 files changed

+17
-10
lines changed

server/config/config.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ import (
3434
"go.etcd.io/etcd/server/v3/storage/datadir"
3535
)
3636

37+
const (
38+
grpcOverheadBytes = 512 * 1024
39+
)
40+
3741
// ServerConfig holds the configuration of etcd as taken from the command line or discovery.
3842
type ServerConfig struct {
3943
Name string
@@ -358,3 +362,7 @@ func (c *ServerConfig) BootstrapTimeoutEffective() time.Duration {
358362
}
359363

360364
func (c *ServerConfig) BackendPath() string { return datadir.ToBackendFileName(c.DataDir) }
365+
366+
func (c *ServerConfig) MaxRequestBytesWithOverhead() uint {
367+
return c.MaxRequestBytes + grpcOverheadBytes
368+
}

server/etcdserver/api/v3rpc/grpc.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ import (
3030
)
3131

3232
const (
33-
grpcOverheadBytes = 512 * 1024
34-
maxSendBytes = math.MaxInt32
33+
maxSendBytes = math.MaxInt32
3534
)
3635

3736
func Server(s *etcdserver.EtcdServer, tls *tls.Config, interceptor grpc.UnaryServerInterceptor, gopts ...grpc.ServerOption) *grpc.Server {
@@ -62,7 +61,7 @@ func Server(s *etcdserver.EtcdServer, tls *tls.Config, interceptor grpc.UnarySer
6261
opts = append(opts, grpc.ChainUnaryInterceptor(chainUnaryInterceptors...))
6362
opts = append(opts, grpc.ChainStreamInterceptor(chainStreamInterceptors...))
6463

65-
opts = append(opts, grpc.MaxRecvMsgSize(int(s.Cfg.MaxRequestBytes+grpcOverheadBytes)))
64+
opts = append(opts, grpc.MaxRecvMsgSize(int(s.Cfg.MaxRequestBytesWithOverhead())))
6665
opts = append(opts, grpc.MaxSendMsgSize(maxSendBytes))
6766
opts = append(opts, grpc.MaxConcurrentStreams(s.Cfg.MaxConcurrentStreams))
6867

server/etcdserver/api/v3rpc/watch.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ type watchServer struct {
4343
clusterID int64
4444
memberID int64
4545

46-
maxRequestBytes int
46+
maxRequestBytes uint
4747

4848
sg apply.RaftStatusGetter
4949
watchable mvcc.WatchableKV
@@ -58,7 +58,7 @@ func NewWatchServer(s *etcdserver.EtcdServer) pb.WatchServer {
5858
clusterID: int64(s.Cluster().ID()),
5959
memberID: int64(s.MemberID()),
6060

61-
maxRequestBytes: int(s.Cfg.MaxRequestBytes + grpcOverheadBytes),
61+
maxRequestBytes: s.Cfg.MaxRequestBytesWithOverhead(),
6262

6363
sg: s,
6464
watchable: s.Watchable(),
@@ -126,7 +126,7 @@ type serverWatchStream struct {
126126
clusterID int64
127127
memberID int64
128128

129-
maxRequestBytes int
129+
maxRequestBytes uint
130130

131131
sg apply.RaftStatusGetter
132132
watchable mvcc.WatchableKV
@@ -544,12 +544,12 @@ func IsCreateEvent(e mvccpb.Event) bool {
544544

545545
func sendFragments(
546546
wr *pb.WatchResponse,
547-
maxRequestBytes int,
547+
maxRequestBytes uint,
548548
sendFunc func(*pb.WatchResponse) error,
549549
) error {
550550
// no need to fragment if total request size is smaller
551551
// than max request limit or response contains only one event
552-
if wr.Size() < maxRequestBytes || len(wr.Events) < 2 {
552+
if uint(wr.Size()) < maxRequestBytes || len(wr.Events) < 2 {
553553
return sendFunc(wr)
554554
}
555555

@@ -562,7 +562,7 @@ func sendFragments(
562562
cur := ow
563563
for _, ev := range wr.Events[idx:] {
564564
cur.Events = append(cur.Events, ev)
565-
if len(cur.Events) > 1 && cur.Size() >= maxRequestBytes {
565+
if len(cur.Events) > 1 && uint(cur.Size()) >= maxRequestBytes {
566566
cur.Events = cur.Events[:len(cur.Events)-1]
567567
break
568568
}

server/etcdserver/api/v3rpc/watch_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import (
2727
func TestSendFragment(t *testing.T) {
2828
tt := []struct {
2929
wr *pb.WatchResponse
30-
maxRequestBytes int
30+
maxRequestBytes uint
3131
fragments int
3232
werr error
3333
}{

0 commit comments

Comments
 (0)