Browse Source

lower latency

dev
破娃酱 8 years ago
parent
commit
6732244ddf
  1. 2
      shadowsocks/eventloop.py
  2. 10
      shadowsocks/tcprelay.py

2
shadowsocks/eventloop.py

@ -223,7 +223,7 @@ class EventLoop(object):
callback() callback()
self._last_time = now self._last_time = now
if events and not handle: if events and not handle:
time.sleep(0.02) time.sleep(0.001)
def __del__(self): def __del__(self):
self._impl.close() self._impl.close()

10
shadowsocks/tcprelay.py

@ -97,7 +97,7 @@ UDP_MAX_BUF_SIZE = 65536
class SpeedTester(object): class SpeedTester(object):
def __init__(self, max_speed = 0): def __init__(self, max_speed = 0):
self.max_speed = max_speed * 1024 self.max_speed = max_speed * 1024
self.timeout = 3 self.timeout = 5
self._cache = deque() self._cache = deque()
self.sum_len = 0 self.sum_len = 0
@ -109,12 +109,12 @@ class SpeedTester(object):
def isExceed(self): def isExceed(self):
if self.max_speed > 0: if self.max_speed > 0:
if self.sum_len > 0: if self.sum_len > 0:
if self._cache[0][0] + self.timeout < time.time(): cut_t = time.time()
t = max(cut_t - self._cache[0][0], 0.01)
speed = self.sum_len / t
if self._cache[0][0] + self.timeout < cut_t:
self.sum_len -= self._cache[0][1] self.sum_len -= self._cache[0][1]
self._cache.popleft() self._cache.popleft()
if self.sum_len > 0:
t = max(time.time() - self._cache[0][0], 0.1)
speed = (self.sum_len - self._cache[0][1]) / (time.time() - self._cache[0][0])
return speed >= self.max_speed return speed >= self.max_speed
return False return False

Loading…
Cancel
Save