Skip to content
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

feat: implement configurable ping timeout for nhooyr.io/websocket #113

Merged
merged 15 commits into from
May 16, 2024

Conversation

denopink
Copy link
Contributor

@denopink denopink commented May 2, 2024

implements #83 #88

  • Add configurable pingTimeout to internal/nhooyr.io/websocket/conn.go's Conn struct.
  • Implement timeouts for pings.
  • Add ping/pong listeners support for context timeout/cancel

@denopink denopink added the feature new functionality proposal/implementation label May 2, 2024
@denopink denopink self-assigned this May 2, 2024
@denopink denopink linked an issue May 2, 2024 that may be closed by this pull request
@denopink denopink marked this pull request as draft May 2, 2024 17:32
@denopink denopink force-pushed the denopink/feat/configurable-ping-timeout branch from 6decc55 to 67a106f Compare May 2, 2024 23:59
- added `pingTimeout   time.Duration` to `internal/nhooyr.io/websocket/conn.go`'s `Conn struct`
- implemented `SetPingTimeout`, following how `nhooyr.io/websocket` exposes configurable conn values
- if `conn`'s `pingTimeout` is nonpositive, then use `handleControl`'s 5 second timeout https://github.com/xmidt-org/xmidt-agent/blob/78dffe0cad394ab82f581940e3a8117f04941077/internal/nhooyr.io/websocket/read.go#L301
@denopink denopink force-pushed the denopink/feat/configurable-ping-timeout branch from 67a106f to 924a2c6 Compare May 3, 2024 00:01
@denopink denopink marked this pull request as ready for review May 3, 2024 00:05
@denopink denopink changed the title feat: implement configurable ping timeout for nhooyr.io/websocket #88 feat: implement configurable ping timeout for nhooyr.io/websocket #83 May 3, 2024
@denopink denopink changed the title feat: implement configurable ping timeout for nhooyr.io/websocket #83 feat: implement configurable ping timeout for nhooyr.io/websocket May 3, 2024
internal/websocket/e2e_test.go Outdated Show resolved Hide resolved
internal/websocket/e2e_test.go Outdated Show resolved Hide resolved
internal/websocket/e2e_test.go Outdated Show resolved Hide resolved
internal/websocket/e2e_test.go Outdated Show resolved Hide resolved
internal/websocket/e2e_test.go Outdated Show resolved Hide resolved
Comment on lines -160 to +163
writeErr := c.writeControl(context.Background(), opClose, p)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()

writeErr := c.writeControl(ctx, opClose, p)
Copy link
Contributor Author

@denopink denopink May 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

c.writeControl uses the 5 seconds default timeout

Comment on lines -238 to 237
ctx, cancel := context.WithTimeout(ctx, time.Second*5)
defer cancel()

_, err := c.writeFrame(ctx, true, false, opcode, p)
Copy link
Contributor Author

@denopink denopink May 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this default time.Second*5 is only used here https://github.com/xmidt-org/xmidt-agent/pull/113/files#r1591363724

@denopink denopink linked an issue May 7, 2024 that may be closed by this pull request
Comment on lines +98 to +104
// SetPingTimeout sets the maximum time allowed between PINGs for the connection
// before the connection is closed.
// Nonpositive PingTimeout will default to handleControl's 5 second timeout.
func (c *Conn) SetPingTimeout(d time.Duration) {
c.pingTimeout = d
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

following how nhooyr.io/websocket exposes configurable conn values

Comment on lines +237 to +239
if ctx.Err() != nil {
return
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in case of timeouts/cancels

@denopink denopink merged commit 232dd3a into main May 16, 2024
14 checks passed
@denopink denopink deleted the denopink/feat/configurable-ping-timeout branch May 16, 2024 20:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature new functionality proposal/implementation
Projects
None yet
2 participants