Skip to content

Commit fe96bdd

Browse files
committed
add batchDiscard
Signed-off-by: ningmingxiao <ning.mingxiao@zte.com.cn>
1 parent ea3d589 commit fe96bdd

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

channel.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,25 @@ func newChannel(conn net.Conn) *channel {
111111
}
112112
}
113113

114+
func batchDiscard(br *bufio.Reader, total int, batchSize int) (int, error) {
115+
var discarded int
116+
for discarded < total {
117+
needDiscard := total - discarded
118+
currentBatch := batchSize
119+
if needDiscard < currentBatch {
120+
currentBatch = needDiscard
121+
}
122+
123+
n, err := br.Discard(currentBatch)
124+
discarded += n
125+
126+
if err != nil {
127+
return discarded, err
128+
}
129+
}
130+
return discarded, nil
131+
}
132+
114133
// recv a message from the channel. The returned buffer contains the message.
115134
//
116135
// If a valid grpc status is returned, the message header
@@ -124,7 +143,7 @@ func (ch *channel) recv() (messageHeader, []byte, error) {
124143
}
125144

126145
if mh.Length > uint32(messageLengthMax) {
127-
if _, err := ch.br.Discard(int(mh.Length)); err != nil {
146+
if _, err := batchDiscard(ch.br, int(mh.Length), messageLengthMax); err != nil {
128147
return mh, nil, fmt.Errorf("failed to discard after receiving oversized message: %w", err)
129148
}
130149

0 commit comments

Comments
 (0)