Bladeren bron

add timer coalescing for the common case

tags/v2.3.0-rc1
Shivaram Lingamneni 3 jaren geleden
bovenliggende
commit
b54d0f8f34
1 gewijzigde bestanden met toevoegingen van 9 en 2 verwijderingen
  1. 9
    2
      irc/client.go

+ 9
- 2
irc/client.go Bestand weergeven

@@ -54,6 +54,9 @@ const (
54 54
 	DefaultTotalTimeout = 2*time.Minute + 30*time.Second
55 55
 	// Resumeable clients (clients who have negotiated caps.Resume) get longer:
56 56
 	ResumeableTotalTimeout = 3*time.Minute + 30*time.Second
57
+
58
+	// round off the ping interval by this much, see below:
59
+	PingCoalesceThreshold = time.Second
57 60
 )
58 61
 
59 62
 // ResumeDetails is a place to stash data at various stages of
@@ -811,7 +814,11 @@ func (session *Session) handleIdleTimeout() {
811 814
 	timeUntilDestroy := session.lastTouch.Add(totalTimeout).Sub(now)
812 815
 	timeUntilPing := session.lastTouch.Add(pingTimeout).Sub(now)
813 816
 	shouldDestroy := session.pingSent && timeUntilDestroy <= 0
814
-	shouldSendPing := !session.pingSent && timeUntilPing <= 0
817
+	// XXX this should really be time <= 0, but let's do some hacky timer coalescing:
818
+	// a typical idling client will do nothing other than respond immediately to our pings,
819
+	// so we'll PING at t=0, they'll respond at t=0.05, then we'll wake up at t=90 and find
820
+	// that we need to PING again at t=90.05. Rather than wake up again, just send it now:
821
+	shouldSendPing := !session.pingSent && timeUntilPing <= PingCoalesceThreshold
815 822
 	if !shouldDestroy {
816 823
 		if shouldSendPing {
817 824
 			session.pingSent = true
@@ -821,7 +828,7 @@ func (session *Session) handleIdleTimeout() {
821 828
 		// 2. the next time we would send PING (if they don't send any more lines)
822 829
 		// 3. the next time we would destroy (if they don't send any more lines)
823 830
 		nextTimeout := pingTimeout
824
-		if 0 < timeUntilPing && timeUntilPing < nextTimeout {
831
+		if PingCoalesceThreshold < timeUntilPing && timeUntilPing < nextTimeout {
825 832
 			nextTimeout = timeUntilPing
826 833
 		}
827 834
 		if 0 < timeUntilDestroy && timeUntilDestroy < nextTimeout {

Laden…
Annuleren
Opslaan