github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/pkg/p2p/client_batch_sender.go (about)

     1  // Copyright 2021 PingCAP, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package p2p
    15  
    16  import (
    17  	"github.com/pingcap/failpoint"
    18  	"github.com/pingcap/tiflow/pkg/errors"
    19  	proto "github.com/pingcap/tiflow/proto/p2p"
    20  )
    21  
    22  const (
    23  	// The maximum size of pre-allocated buffer.
    24  	// 4096 is reasonable given the scenarios under which
    25  	// the peer-message system is used.
    26  	maxPreallocBatchSize = 4096
    27  )
    28  
    29  var _ clientBatchSender[MessageEntry] = &grpcClientBatchSender{}
    30  
    31  // clientBatchSender is a batch sender that
    32  // batches messages and sends them through a gRPC client.
    33  type clientBatchSender[T any] interface {
    34  	Append(msg T) error
    35  	Flush() error
    36  }
    37  
    38  type grpcClientBatchSender struct {
    39  	stream MessageClientStream
    40  
    41  	buffer    []MessageEntry
    42  	sizeBytes int
    43  
    44  	maxEntryCount int
    45  	maxSizeBytes  int
    46  }
    47  
    48  func newClientBatchSender(stream MessageClientStream, maxEntryCount, maxSizeBytes int) clientBatchSender[MessageEntry] {
    49  	sliceCap := maxEntryCount
    50  	if sliceCap > maxPreallocBatchSize {
    51  		sliceCap = maxPreallocBatchSize
    52  	}
    53  	return &grpcClientBatchSender{
    54  		stream:        stream,
    55  		buffer:        make([]MessageEntry, 0, sliceCap),
    56  		maxEntryCount: maxEntryCount,
    57  		maxSizeBytes:  maxSizeBytes,
    58  	}
    59  }
    60  
    61  // Append appends a message to the batch. If the resulting batch contains more than
    62  // maxEntryCount messages or the total size of messages exceeds maxSizeBytes,
    63  // the batch is flushed.
    64  func (s *grpcClientBatchSender) Append(msg MessageEntry) error {
    65  	failpoint.Inject("ClientBatchSenderInjectError", func() {
    66  		failpoint.Return(errors.New("injected error"))
    67  	})
    68  
    69  	s.buffer = append(s.buffer, msg)
    70  	s.sizeBytes += msg.Size()
    71  
    72  	if len(s.buffer) >= s.maxEntryCount || s.sizeBytes >= s.maxSizeBytes {
    73  		return s.Flush()
    74  	}
    75  	return nil
    76  }
    77  
    78  // Flush flushes the batch.
    79  func (s *grpcClientBatchSender) Flush() error {
    80  	failpoint.Inject("ClientBatchSenderInjectError", func() {
    81  		failpoint.Return(errors.New("injected error"))
    82  	})
    83  
    84  	if len(s.buffer) == 0 {
    85  		return nil
    86  	}
    87  
    88  	var messagePacket proto.MessagePacket
    89  	messagePacket.Entries = s.buffer
    90  	err := s.stream.Send(&messagePacket)
    91  	s.sizeBytes = 0
    92  	s.buffer = s.buffer[:0]
    93  	return err
    94  }