Skip to content

Commit cfa8668

Browse files
Update peer activity every 30 seconds (#290)
Like it was before: d5f1053 But do send the ping packet every 2 seconds still.
1 parent 8556f8e commit cfa8668

1 file changed

Lines changed: 10 additions & 3 deletions

File tree

internal/signaling/handler.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import (
1919

2020
const LobbyCleanInterval = 30 * time.Minute
2121
const LobbyCleanThreshold = 24 * time.Hour
22+
const peerPingDuration = 2 * time.Second
23+
const peerActiveUpdateInterval = 30 * time.Second
2224

2325
func Handler(ctx context.Context, store stores.Store, cloudflare *cloudflare.CredentialsClient) (*sync.WaitGroup, http.HandlerFunc) {
2426
manager := &TimeoutManager{
@@ -82,9 +84,10 @@ func Handler(ctx context.Context, store stores.Store, cloudflare *cloudflare.Cre
8284
}
8385
}()
8486

85-
go func() { // Sending ping packet every 2 seconds to check if the tcp connection is still alive.
86-
ticker := time.NewTicker(2 * time.Second)
87+
go func() { // Sending ping packet every X to check if the tcp connection is still alive.
88+
ticker := time.NewTicker(peerPingDuration)
8789
defer ticker.Stop()
90+
var lastActiveUpdate time.Time
8891
for {
8992
select {
9093
case <-ticker.C:
@@ -100,7 +103,11 @@ func Handler(ctx context.Context, store stores.Store, cloudflare *cloudflare.Cre
100103
// If we can send a ping packet, and the peer has an ID, we update the peer as being active.
101104
// If the peer doesn't have an ID yet, it's still in the process of connecting, so we don't update it.
102105
if peer.ID != "" {
103-
manager.MarkPeerAsActive(ctx, peer.ID)
106+
now := util.NowUTC(ctx)
107+
if lastActiveUpdate.IsZero() || now.Sub(lastActiveUpdate) >= peerActiveUpdateInterval {
108+
manager.MarkPeerAsActive(ctx, peer.ID)
109+
lastActiveUpdate = now
110+
}
104111
}
105112
}
106113
case <-ctx.Done():

0 commit comments

Comments
 (0)