Browse Source

add timer coalescing for the common case

tags/v2.3.0-rc1
Shivaram Lingamneni 3 years ago
parent
commit
b54d0f8f34
1 changed files with 9 additions and 2 deletions
  1. 9
    2
      irc/client.go

+ 9
- 2
irc/client.go View File

54
 	DefaultTotalTimeout = 2*time.Minute + 30*time.Second
54
 	DefaultTotalTimeout = 2*time.Minute + 30*time.Second
55
 	// Resumeable clients (clients who have negotiated caps.Resume) get longer:
55
 	// Resumeable clients (clients who have negotiated caps.Resume) get longer:
56
 	ResumeableTotalTimeout = 3*time.Minute + 30*time.Second
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
 // ResumeDetails is a place to stash data at various stages of
62
 // ResumeDetails is a place to stash data at various stages of
811
 	timeUntilDestroy := session.lastTouch.Add(totalTimeout).Sub(now)
814
 	timeUntilDestroy := session.lastTouch.Add(totalTimeout).Sub(now)
812
 	timeUntilPing := session.lastTouch.Add(pingTimeout).Sub(now)
815
 	timeUntilPing := session.lastTouch.Add(pingTimeout).Sub(now)
813
 	shouldDestroy := session.pingSent && timeUntilDestroy <= 0
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
 	if !shouldDestroy {
822
 	if !shouldDestroy {
816
 		if shouldSendPing {
823
 		if shouldSendPing {
817
 			session.pingSent = true
824
 			session.pingSent = true
821
 		// 2. the next time we would send PING (if they don't send any more lines)
828
 		// 2. the next time we would send PING (if they don't send any more lines)
822
 		// 3. the next time we would destroy (if they don't send any more lines)
829
 		// 3. the next time we would destroy (if they don't send any more lines)
823
 		nextTimeout := pingTimeout
830
 		nextTimeout := pingTimeout
824
-		if 0 < timeUntilPing && timeUntilPing < nextTimeout {
831
+		if PingCoalesceThreshold < timeUntilPing && timeUntilPing < nextTimeout {
825
 			nextTimeout = timeUntilPing
832
 			nextTimeout = timeUntilPing
826
 		}
833
 		}
827
 		if 0 < timeUntilDestroy && timeUntilDestroy < nextTimeout {
834
 		if 0 < timeUntilDestroy && timeUntilDestroy < nextTimeout {

Loading…
Cancel
Save