aboutsummaryrefslogtreecommitdiffstats
path: root/p2p/discover/udp.go
diff options
context:
space:
mode:
authorPéter Szilágyi <peterke@gmail.com>2016-02-24 19:12:52 +0800
committerPéter Szilágyi <peterke@gmail.com>2016-02-24 19:12:52 +0800
commit1415669ac31cf8f06d107e06681b95c2b5e1c040 (patch)
treed3042ee9a19d9fb379afa35de236722a8218f7a7 /p2p/discover/udp.go
parent1e62cd6c79afdc4b39a30932f950673405f0c702 (diff)
parentac954f48bd9f9897f3118782ff2e68eeb3e1ed4e (diff)
downloadgo-tangerine-1415669ac31cf8f06d107e06681b95c2b5e1c040.tar
go-tangerine-1415669ac31cf8f06d107e06681b95c2b5e1c040.tar.gz
go-tangerine-1415669ac31cf8f06d107e06681b95c2b5e1c040.tar.bz2
go-tangerine-1415669ac31cf8f06d107e06681b95c2b5e1c040.tar.lz
go-tangerine-1415669ac31cf8f06d107e06681b95c2b5e1c040.tar.xz
go-tangerine-1415669ac31cf8f06d107e06681b95c2b5e1c040.tar.zst
go-tangerine-1415669ac31cf8f06d107e06681b95c2b5e1c040.zip
Merge pull request #2218 from karalabe/time-drift-warning
p2p/discover: NTP sanity check clock drift in case of expirations
Diffstat (limited to 'p2p/discover/udp.go')
-rw-r--r--p2p/discover/udp.go26
1 files changed, 22 insertions, 4 deletions
diff --git a/p2p/discover/udp.go b/p2p/discover/udp.go
index 81674f552..03fa0c348 100644
--- a/p2p/discover/udp.go
+++ b/p2p/discover/udp.go
@@ -51,6 +51,10 @@ const (
respTimeout = 500 * time.Millisecond
sendTimeout = 500 * time.Millisecond
expiration = 20 * time.Second
+
+ ntpFailureThreshold = 32 // Continuous timeouts after which to check NTP
+ ntpWarningCooldown = 10 * time.Minute // Minimum amount of time to pass before repeating NTP warning
+ driftThreshold = 10 * time.Second // Allowed clock drift before warning user
)
// RPC packet types
@@ -316,13 +320,15 @@ func (t *udp) handleReply(from NodeID, ptype byte, req packet) bool {
}
}
-// loop runs in its own goroutin. it keeps track of
+// loop runs in its own goroutine. it keeps track of
// the refresh timer and the pending reply queue.
func (t *udp) loop() {
var (
- plist = list.New()
- timeout = time.NewTimer(0)
- nextTimeout *pending // head of plist when timeout was last reset
+ plist = list.New()
+ timeout = time.NewTimer(0)
+ nextTimeout *pending // head of plist when timeout was last reset
+ contTimeouts = 0 // number of continuous timeouts to do NTP checks
+ ntpWarnTime = time.Unix(0, 0)
)
<-timeout.C // ignore first timeout
defer timeout.Stop()
@@ -377,19 +383,31 @@ func (t *udp) loop() {
p.errc <- nil
plist.Remove(el)
}
+ // Reset the continuous timeout counter (time drift detection)
+ contTimeouts = 0
}
}
r.matched <- matched
case now := <-timeout.C:
nextTimeout = nil
+
// Notify and remove callbacks whose deadline is in the past.
for el := plist.Front(); el != nil; el = el.Next() {
p := el.Value.(*pending)
if now.After(p.deadline) || now.Equal(p.deadline) {
p.errc <- errTimeout
plist.Remove(el)
+ contTimeouts++
+ }
+ }
+ // If we've accumulated too many timeouts, do an NTP time sync check
+ if contTimeouts > ntpFailureThreshold {
+ if time.Since(ntpWarnTime) >= ntpWarningCooldown {
+ ntpWarnTime = time.Now()
+ go checkClockDrift()
}
+ contTimeouts = 0
}
}
}