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

Provide a simple callback function for when you need to know a ping came in. #428

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
9 changes: 9 additions & 0 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ type Conn struct {
pingCounter int32
activePingsMu sync.Mutex
activePings map[string]chan<- struct{}

pingCallback func()
}

type connConfig struct {
Expand Down Expand Up @@ -251,6 +253,13 @@ func (c *Conn) ping(ctx context.Context, p string) error {
}
}

// SetPingCallback sets a callback that is called when a ping is received.
// The callback is called synchronously from the Reader goroutine and must
// not block.
func (c *Conn) SetPingCallback(cb func()) {
c.pingCallback = cb
}

type mu struct {
c *Conn
ch chan struct{}
Expand Down
3 changes: 3 additions & 0 deletions read.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,9 @@ func (c *Conn) handleControl(ctx context.Context, h header) (err error) {

switch h.opcode {
case opPing:
if c.pingCallback != nil {
c.pingCallback()
}
return c.writeControl(ctx, opPong, b)
case opPong:
c.activePingsMu.Lock()
Expand Down
Loading