Skip to content
This repository was archived by the owner on Mar 5, 2023. It is now read-only.

Commit 6e665b9

Browse files
committed
make a few adjustments to buffer reuse strategies to minimize large allocs
1 parent 6827e8f commit 6e665b9

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

gateway.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ const (
114114
)
115115

116116
// max size of buffers before they're discarded (e.g after a big incmoing event)
117-
const MaxIntermediaryBuffersSize = 100000
117+
const MaxIntermediaryBuffersSize = 10000
118118

119119
// GatewayIdentifyRatelimiter is if you need some custom identify ratelimit logic (if you're running shards across processes for example)
120120
type GatewayIdentifyRatelimiter interface {
@@ -1107,7 +1107,7 @@ func (g *GatewayConnection) handleReadMessage() {
11071107
return
11081108
}
11091109

1110-
if g.decodedBuffer.Cap() > MaxIntermediaryBuffersSize {
1110+
if g.decodedBuffer.Cap() > MaxIntermediaryBuffersSize && readLen < MaxIntermediaryBuffersSize {
11111111
maybeThrowawayBytesBuf(&g.decodedBuffer, MaxIntermediaryBuffersSize)
11121112
g.jsonDecoder = gojay.NewDecoder(g.teeReader)
11131113
}
@@ -1175,6 +1175,8 @@ func (g *GatewayConnection) handleHello(event *Event) error {
11751175

11761176
func (g *GatewayConnection) handleDispatch(e *Event) error {
11771177

1178+
size := len(e.RawData)
1179+
11781180
// Map event to registered event handlers and pass it along to any registered handlers.
11791181
if eh, ok := registeredInterfaceProviders[e.Type]; ok {
11801182
e.Struct = eh.New()
@@ -1222,7 +1224,7 @@ func (g *GatewayConnection) handleDispatch(e *Event) error {
12221224
g.log(LogWarning, "unknown event: Op: %d, Seq: %d, Type: %s, Data: %s", e.Operation, e.Sequence, e.Type, string(e.RawData))
12231225
}
12241226

1225-
if g.secondPassBuf.Cap() > MaxIntermediaryBuffersSize {
1227+
if g.secondPassBuf.Cap() > MaxIntermediaryBuffersSize && size < MaxIntermediaryBuffersSize {
12261228
maybeThrowawayBytesBuf(g.secondPassBuf, MaxIntermediaryBuffersSize)
12271229
g.secondPassJsonDecoder = json.NewDecoder(g.secondPassBuf)
12281230
}

0 commit comments

Comments
 (0)