-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Use sync.Pool to share per-connection transport write buffer. #6309
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 all commits
0f1058d
63f4ba3
4faff38
f594b29
ec75cb1
95bde71
a243c24
a7ededc
16345bb
c8036d0
09407f4
0b42642
e1bdb20
9b97327
d274f35
00e8de7
9aeb148
195a467
82a400e
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 |
---|---|---|
|
@@ -30,6 +30,7 @@ import ( | |
"net/url" | ||
"strconv" | ||
"strings" | ||
"sync" | ||
"time" | ||
"unicode/utf8" | ||
|
||
|
@@ -309,19 +310,25 @@ func decodeGrpcMessageUnchecked(msg string) string { | |
} | ||
|
||
type bufWriter struct { | ||
pool *sync.Pool | ||
buf []byte | ||
offset int | ||
batchSize int | ||
conn net.Conn | ||
err error | ||
} | ||
|
||
func newBufWriter(conn net.Conn, batchSize int) *bufWriter { | ||
return &bufWriter{ | ||
buf: make([]byte, batchSize*2), | ||
func newBufWriter(conn net.Conn, batchSize int, pool *sync.Pool) *bufWriter { | ||
w := &bufWriter{ | ||
batchSize: batchSize, | ||
conn: conn, | ||
pool: pool, | ||
} | ||
// this indicates that we should use non shared buf | ||
if pool == nil { | ||
w.buf = make([]byte, batchSize) | ||
} | ||
return w | ||
} | ||
|
||
func (w *bufWriter) Write(b []byte) (n int, err error) { | ||
|
@@ -332,19 +339,34 @@ func (w *bufWriter) Write(b []byte) (n int, err error) { | |
n, err = w.conn.Write(b) | ||
return n, toIOError(err) | ||
} | ||
if w.buf == nil { | ||
b := w.pool.Get().(*[]byte) | ||
w.buf = *b | ||
} | ||
for len(b) > 0 { | ||
nn := copy(w.buf[w.offset:], b) | ||
b = b[nn:] | ||
w.offset += nn | ||
n += nn | ||
if w.offset >= w.batchSize { | ||
err = w.Flush() | ||
err = w.flushKeepBuffer() | ||
} | ||
} | ||
return n, err | ||
} | ||
|
||
func (w *bufWriter) Flush() error { | ||
err := w.flushKeepBuffer() | ||
// Only release the buffer if we are in a "shared" mode | ||
if w.buf != nil && w.pool != nil { | ||
b := w.buf | ||
w.pool.Put(&b) | ||
easwars marked this conversation as resolved.
Show resolved
Hide resolved
|
||
w.buf = nil | ||
} | ||
return err | ||
} | ||
|
||
func (w *bufWriter) flushKeepBuffer() error { | ||
if w.err != nil { | ||
return w.err | ||
} | ||
|
@@ -381,15 +403,22 @@ type framer struct { | |
fr *http2.Framer | ||
} | ||
|
||
func newFramer(conn net.Conn, writeBufferSize, readBufferSize int, maxHeaderListSize uint32) *framer { | ||
var writeBufferPoolMap map[int]*sync.Pool = make(map[int]*sync.Pool) | ||
var writeBufferMutex sync.Mutex | ||
|
||
func newFramer(conn net.Conn, writeBufferSize, readBufferSize int, sharedWriteBuffer bool, maxHeaderListSize uint32) *framer { | ||
if writeBufferSize < 0 { | ||
writeBufferSize = 0 | ||
} | ||
var r io.Reader = conn | ||
if readBufferSize > 0 { | ||
r = bufio.NewReaderSize(r, readBufferSize) | ||
} | ||
w := newBufWriter(conn, writeBufferSize) | ||
var pool *sync.Pool | ||
if sharedWriteBuffer { | ||
pool = getWriteBufferPool(writeBufferSize) | ||
} | ||
w := newBufWriter(conn, writeBufferSize, pool) | ||
f := &framer{ | ||
writer: w, | ||
fr: http2.NewFramer(w, r), | ||
|
@@ -403,6 +432,24 @@ func newFramer(conn net.Conn, writeBufferSize, readBufferSize int, maxHeaderList | |
return f | ||
} | ||
|
||
func getWriteBufferPool(writeBufferSize int) *sync.Pool { | ||
writeBufferMutex.Lock() | ||
defer writeBufferMutex.Unlock() | ||
size := writeBufferSize * 2 | ||
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. What is the significance of the 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. IIRC this matches the legacy behavior, which always doubled for reasons nobody knows but we don't want to change now due to it affecting existing applications. 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. That's fair. Do you mind expanding on the legacy behavior you are describing? It seems like if there is no shared buffer pool, the buffer is allocated per the input size. It only seems to be in the case of the buffer pool that we're allocating 2x the size (and this seems to be the PR the write buffer pool was added)? 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. You may be right.. The old code multiplied by 2 when doing this allocation, so it seems we have changed the behavior for legacy users already: 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. Yeah - it just felt somewhat bizarre because this is the very PR that also removed that same 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. If you think it's worth updating, I created #6983 |
||
pool, ok := writeBufferPoolMap[size] | ||
if ok { | ||
return pool | ||
} | ||
pool = &sync.Pool{ | ||
New: func() interface{} { | ||
b := make([]byte, size) | ||
return &b | ||
}, | ||
} | ||
writeBufferPoolMap[size] = pool | ||
return pool | ||
} | ||
|
||
// parseDialTarget returns the network and address to pass to dialer. | ||
func parseDialTarget(target string) (string, string) { | ||
net := "tcp" | ||
|
Uh oh!
There was an error while loading. Please reload this page.