Skip to content

Commit

Permalink
transport: reduce memory usage for small reads in server handler
Browse files Browse the repository at this point in the history
  • Loading branch information
lqs committed Dec 26, 2024
1 parent 724f450 commit 3099ffd
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion internal/transport/handler_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,9 +435,16 @@ func (ht *serverHandlerTransport) HandleStreams(ctx context.Context, startStream
for {
buf := ht.bufferPool.Get(http2MaxFrameLen)
n, err := req.Body.Read(*buf)
if n > 0 {
if n > http2MaxFrameLen/4 {
// size is big enough, just use the buffer
*buf = (*buf)[:n]
s.buf.put(recvMsg{buffer: mem.NewBuffer(buf, ht.bufferPool)})
} else if n > 0 {
// move to a smaller buffer and put back the original buffer
newBuf := ht.bufferPool.Get(n)
copy(*newBuf, (*buf)[:n])
ht.bufferPool.Put(buf)
s.buf.put(recvMsg{buffer: mem.NewBuffer(newBuf, ht.bufferPool)})
} else {
ht.bufferPool.Put(buf)
}
Expand Down

0 comments on commit 3099ffd

Please sign in to comment.