@@ -19,6 +19,8 @@ import (
1919
2020const LobbyCleanInterval = 30 * time .Minute
2121const LobbyCleanThreshold = 24 * time .Hour
22+ const peerPingDuration = 2 * time .Second
23+ const peerActiveUpdateInterval = 30 * time .Second
2224
2325func 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