-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplify how we handle ping and pong
Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com>
- Loading branch information
1 parent
098be79
commit 90ae985
Showing
5 changed files
with
52 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package peer | ||
|
||
import "time" | ||
|
||
type Pong struct{} | ||
|
||
type PingPongConfig struct { | ||
Interval time.Duration | ||
Deadline time.Duration | ||
PongChannel chan Pong | ||
SendPing func() | ||
OnDeadLine func() | ||
} | ||
|
||
// Starts a goroutine that will execute `onDeadLine` closure in case nothing has been published | ||
// to the `heartBeat` channel for `deadline` duration. The goroutine stops once the channel is closed. | ||
func startPingPong(config PingPongConfig) { | ||
go func() { | ||
for range time.Tick(config.Interval) { | ||
config.SendPing() | ||
|
||
select { | ||
case <-time.After(config.Deadline): | ||
config.OnDeadLine() | ||
return | ||
case _, ok := <-config.PongChannel: | ||
if !ok { | ||
return | ||
} | ||
} | ||
} | ||
}() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters