Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ type Conn struct {

readTimeoutStop atomic.Pointer[func() bool]
writeTimeoutStop atomic.Pointer[func() bool]
readTimeoutFunc func() // Guarded by readMu.
writeTimeoutFunc func() // Guarded by writeFrameMu.

// Read state.
readMu *mu
Expand Down Expand Up @@ -173,10 +175,14 @@ func (c *Conn) setupWriteTimeout(ctx context.Context) bool {
return false
}

stop := context.AfterFunc(ctx, func() {
c.clearWriteTimeout()
c.close()
})
if c.writeTimeoutFunc == nil {
// Made on first use and reused, so each frame does not allocate it.
c.writeTimeoutFunc = func() {
c.clearWriteTimeout()
c.close()
}
}
stop := context.AfterFunc(ctx, c.writeTimeoutFunc)
swapTimeoutStop(&c.writeTimeoutStop, &stop)
return true
}
Expand All @@ -190,10 +196,14 @@ func (c *Conn) setupReadTimeout(ctx context.Context) bool {
return false
}

stop := context.AfterFunc(ctx, func() {
c.clearReadTimeout()
c.close()
})
if c.readTimeoutFunc == nil {
// Made on first use and reused, so each frame does not allocate it.
c.readTimeoutFunc = func() {
c.clearReadTimeout()
c.close()
}
}
stop := context.AfterFunc(ctx, c.readTimeoutFunc)
swapTimeoutStop(&c.readTimeoutStop, &stop)
return true
}
Expand Down
Loading