Allow more than one "last ping", this should make stuff better on worse connections
This commit is contained in:
parent
bccbda23b7
commit
4e8cd4b6b3
@ -1,5 +1,6 @@
|
|||||||
#coding:utf8
|
#coding:utf8
|
||||||
|
|
||||||
|
from collections import deque
|
||||||
import time
|
import time
|
||||||
import random
|
import random
|
||||||
|
|
||||||
@ -142,8 +143,7 @@ class WatcherInfo(object):
|
|||||||
self.ping = None
|
self.ping = None
|
||||||
self.time_offset = 0
|
self.time_offset = 0
|
||||||
self.time_offset_data = []
|
self.time_offset_data = []
|
||||||
self.last_ping_time = None
|
self.pings_sent = dict()
|
||||||
self.last_ping_value = None
|
|
||||||
|
|
||||||
self.counter = 0
|
self.counter = 0
|
||||||
|
|
||||||
@ -278,19 +278,21 @@ class SyncFactory(Factory):
|
|||||||
if not watcher:
|
if not watcher:
|
||||||
return
|
return
|
||||||
|
|
||||||
if watcher.last_ping_value == value:
|
ping_time = watcher.pings_sent.pop(value, None)
|
||||||
|
if ping_time is not None:
|
||||||
curtime = time.time()
|
curtime = time.time()
|
||||||
watcher.ping = ping = (curtime - watcher.last_ping_time)/2
|
watcher.ping = ping = (curtime - ping_time)/2
|
||||||
|
|
||||||
if watcher.time_offset_data is not None:
|
if watcher.time_offset_data is not None:
|
||||||
time_offset = curtime - (ctime + ping)
|
time_offset = curtime - (ctime + ping)
|
||||||
watcher.time_offset_data.append((ping, time_offset))
|
watcher.time_offset_data.append((ping, time_offset))
|
||||||
|
|
||||||
if len(watcher.time_offset_data) > 1:
|
if len(watcher.time_offset_data) > 1:
|
||||||
pmax = max(p for p,_ in watcher.time_offset_data)
|
pmin = min(p for p,_ in watcher.time_offset_data)
|
||||||
|
pmax = max(p for p,_ in watcher.time_offset_data) - pmin
|
||||||
psum, pweights = 0, 0
|
psum, pweights = 0, 0
|
||||||
for ping, offset in watcher.time_offset_data:
|
for ping, offset in watcher.time_offset_data:
|
||||||
ping = 1-(ping/pmax)
|
ping = 1-((ping-pmin)/pmax)
|
||||||
pweights += ping
|
pweights += ping
|
||||||
psum += ping*offset
|
psum += ping*offset
|
||||||
watcher.time_offset = psum/pweights
|
watcher.time_offset = psum/pweights
|
||||||
@ -303,10 +305,17 @@ class SyncFactory(Factory):
|
|||||||
#print watcher.name, 'last ping', watcher.ping, 'time offset %.6f' % watcher.time_offset
|
#print watcher.name, 'last ping', watcher.ping, 'time offset %.6f' % watcher.time_offset
|
||||||
|
|
||||||
def send_ping_to(self, watcher):
|
def send_ping_to(self, watcher):
|
||||||
chars = random_chars()
|
chars = None
|
||||||
watcher.last_ping_time = time.time()
|
while not chars or chars in watcher.pings_sent:
|
||||||
watcher.last_ping_value = chars
|
chars = random_chars()
|
||||||
|
|
||||||
|
ctime = time.time()
|
||||||
|
|
||||||
watcher.watcher_proto.send_ping(chars)
|
watcher.watcher_proto.send_ping(chars)
|
||||||
|
watcher.pings_sent[chars] = time.time()
|
||||||
|
|
||||||
|
if len(watcher.pings_sent) > 30:
|
||||||
|
watcher.pings_sent.pop(min((time, key) for key, time in watcher.pings_sent.iteritems())[1])
|
||||||
|
|
||||||
self.schedule_send_ping(watcher)
|
self.schedule_send_ping(watcher)
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user