-
Notifications
You must be signed in to change notification settings - Fork 4.7k
transport: Pool read buffers used by the HTTP/2 framer #9032
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,6 +36,9 @@ import ( | |
| "golang.org/x/net/http2" | ||
| "golang.org/x/net/http2/hpack" | ||
| "google.golang.org/grpc/codes" | ||
| "google.golang.org/grpc/internal/envconfig" | ||
| imem "google.golang.org/grpc/internal/mem" | ||
| "google.golang.org/grpc/internal/transport/readyreader" | ||
| "google.golang.org/grpc/mem" | ||
| ) | ||
|
|
||
|
|
@@ -296,15 +299,15 @@ func decodeGrpcMessageUnchecked(msg string) string { | |
| } | ||
|
|
||
| type bufWriter struct { | ||
| pool *sync.Pool | ||
| pool *imem.SimpleBufferPool | ||
| buf []byte | ||
| offset int | ||
| batchSize int | ||
| conn io.Writer | ||
| err error | ||
| } | ||
|
|
||
| func newBufWriter(conn io.Writer, batchSize int, pool *sync.Pool) *bufWriter { | ||
| func newBufWriter(conn io.Writer, batchSize int, pool *imem.SimpleBufferPool) *bufWriter { | ||
| w := &bufWriter{ | ||
| batchSize: batchSize, | ||
| conn: conn, | ||
|
|
@@ -326,7 +329,7 @@ func (w *bufWriter) Write(b []byte) (int, error) { | |
| return n, toIOError(err) | ||
| } | ||
| if w.buf == nil { | ||
| b := w.pool.Get().(*[]byte) | ||
| b := w.pool.Get(w.batchSize) | ||
| w.buf = *b | ||
| } | ||
| written := 0 | ||
|
|
@@ -407,22 +410,33 @@ type framer struct { | |
| errDetail error | ||
| } | ||
|
|
||
| var writeBufferPoolMap = make(map[int]*sync.Pool) | ||
| var writeBufferMutex sync.Mutex | ||
| var ioBufferPoolMap = make(map[int]*imem.SimpleBufferPool) | ||
| var ioBufferMutex sync.Mutex | ||
|
easwars marked this conversation as resolved.
|
||
|
|
||
| func bufferedReader(r io.Reader, bufSize int) io.Reader { | ||
| if bufSize <= 0 { | ||
| return r | ||
| } | ||
| if !envconfig.EnableHTTPFramerReadBufferPooling { | ||
| return bufio.NewReaderSize(r, bufSize) | ||
| } | ||
| if rr := readyreader.NewNonBlocking(r); rr != nil { | ||
| readPool := getIOBufferPool(bufSize) | ||
| return readyreader.NewBuffered(rr, bufSize, readPool) | ||
| } | ||
| return bufio.NewReaderSize(r, bufSize) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Can we simplify this as:
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To check if if envconfig.EnableHTTPFramerReadBufferPooling {
if rr := readyreader.NewNonBlocking(r); rr != nil {
readPool := getIOBufferPool(bufSize)
return readyreader.NewBuffered(rr, bufSize, readPool)
}
}To avoid this nesting and keep the code flat, I opted to use an early return pattern instead. I'm fine the nesting style also.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nesting is what I meant, but I just wrote it as a single conditional in the pseudo code. The only reason I ask for the nesting is because currently we have two code paths that do the same
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changed to nested style. |
||
| } | ||
|
|
||
| func newFramer(conn io.ReadWriter, writeBufferSize, readBufferSize int, sharedWriteBuffer bool, maxHeaderListSize uint32, memPool mem.BufferPool) *framer { | ||
| if writeBufferSize < 0 { | ||
| writeBufferSize = 0 | ||
| } | ||
| var r io.Reader = conn | ||
| if readBufferSize > 0 { | ||
| r = bufio.NewReaderSize(r, readBufferSize) | ||
| } | ||
| var pool *sync.Pool | ||
| r := bufferedReader(conn, readBufferSize) | ||
| var writePool *imem.SimpleBufferPool | ||
| if sharedWriteBuffer { | ||
| pool = getWriteBufferPool(writeBufferSize) | ||
| writePool = getIOBufferPool(writeBufferSize) | ||
| } | ||
| w := newBufWriter(conn, writeBufferSize, pool) | ||
| w := newBufWriter(conn, writeBufferSize, writePool) | ||
| f := &framer{ | ||
| writer: w, | ||
| fr: http2.NewFramer(w, r), | ||
|
|
@@ -578,20 +592,15 @@ func (df *parsedDataFrame) Header() http2.FrameHeader { | |
| return df.FrameHeader | ||
| } | ||
|
|
||
| func getWriteBufferPool(size int) *sync.Pool { | ||
| writeBufferMutex.Lock() | ||
| defer writeBufferMutex.Unlock() | ||
| pool, ok := writeBufferPoolMap[size] | ||
| func getIOBufferPool(size int) *imem.SimpleBufferPool { | ||
|
easwars marked this conversation as resolved.
Outdated
|
||
| ioBufferMutex.Lock() | ||
| defer ioBufferMutex.Unlock() | ||
| pool, ok := ioBufferPoolMap[size] | ||
| if ok { | ||
| return pool | ||
| } | ||
| pool = &sync.Pool{ | ||
| New: func() any { | ||
| b := make([]byte, size) | ||
| return &b | ||
| }, | ||
| } | ||
| writeBufferPoolMap[size] = pool | ||
| pool = imem.NewDirtySimplePool() | ||
| ioBufferPoolMap[size] = pool | ||
| return pool | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.