Browse Source

change the counter of the online users

dev
BreakWa11 9 years ago
parent
commit
3f6f16e4d6
  1. 20
      db_transfer.py
  2. 2
      shadowsocks/tcprelay.py

20
db_transfer.py

@ -6,7 +6,7 @@ import time
import sys import sys
from server_pool import ServerPool from server_pool import ServerPool
import traceback import traceback
from shadowsocks import common, shell from shadowsocks import common, shell, lru_cache
from configloader import load_config, get_config from configloader import load_config, get_config
import importloader import importloader
@ -17,9 +17,11 @@ class DbTransfer(object):
def __init__(self): def __init__(self):
import threading import threading
self.last_get_transfer = {} self.last_get_transfer = {}
self.last_update_transfer = {}
self.event = threading.Event() self.event = threading.Event()
self.user_pass = {} self.user_pass = {}
self.port_uid_table = {} self.port_uid_table = {}
self.onlineuser_cache = lru_cache.LRUCache(timeout=60*30)
def update_all_user(self, dt_transfer): def update_all_user(self, dt_transfer):
import cymysql import cymysql
@ -72,7 +74,7 @@ class DbTransfer(object):
def push_db_all_user(self): def push_db_all_user(self):
#更新用户流量到数据库 #更新用户流量到数据库
last_transfer = self.last_get_transfer last_transfer = self.last_update_transfer
curr_transfer = ServerPool.get_instance().get_servers_transfer() curr_transfer = ServerPool.get_instance().get_servers_transfer()
#上次和本次的增量 #上次和本次的增量
dt_transfer = {} dt_transfer = {}
@ -90,11 +92,18 @@ class DbTransfer(object):
if curr_transfer[id][0] + curr_transfer[id][1] <= 0: if curr_transfer[id][0] + curr_transfer[id][1] <= 0:
continue continue
dt_transfer[id] = [curr_transfer[id][0], curr_transfer[id][1]] dt_transfer[id] = [curr_transfer[id][0], curr_transfer[id][1]]
if id in self.last_get_transfer:
if curr_transfer[id][0] + curr_transfer[id][1] > self.last_get_transfer[id][0] + self.last_get_transfer[id][1]:
self.onlineuser_cache[id] = curr_transfer[id][0] + curr_transfer[id][1]
else:
self.onlineuser_cache[id] = curr_transfer[id][0] + curr_transfer[id][1]
self.onlineuser_cache.sweep()
update_transfer = self.update_all_user(dt_transfer) update_transfer = self.update_all_user(dt_transfer)
for id in update_transfer.keys(): for id in update_transfer.keys():
last = self.last_get_transfer.get(id, [0,0]) last = self.last_update_transfer.get(id, [0,0])
self.last_get_transfer[id] = [last[0] + update_transfer[id][0], last[1] + update_transfer[id][1]] self.last_update_transfer[id] = [last[0] + update_transfer[id][0], last[1] + update_transfer[id][1]]
self.last_get_transfer = curr_transfer
def pull_db_all_user(self): def pull_db_all_user(self):
import cymysql import cymysql
@ -273,7 +282,7 @@ class Dbv3Transfer(DbTransfer):
query_sub_in = None query_sub_in = None
last_time = time.time() last_time = time.time()
alive_user_count = 0 alive_user_count = len(self.onlineuser_cache)
bandwidth_thistime = 0 bandwidth_thistime = 0
if get_config().MYSQL_SSL_ENABLE == 1: if get_config().MYSQL_SSL_ENABLE == 1:
@ -289,7 +298,6 @@ class Dbv3Transfer(DbTransfer):
for id in dt_transfer.keys(): for id in dt_transfer.keys():
transfer = dt_transfer[id] transfer = dt_transfer[id]
alive_user_count = alive_user_count + 1
bandwidth_thistime = bandwidth_thistime + transfer[0] + transfer[1] bandwidth_thistime = bandwidth_thistime + transfer[0] + transfer[1]
update_trs = 1024 * max(2048 - self.user_pass.get(id, 0) * 64, 16) update_trs = 1024 * max(2048 - self.user_pass.get(id, 0) * 64, 16)

2
shadowsocks/tcprelay.py

@ -370,7 +370,7 @@ class TCPRelayHandler(object):
host, port = self._get_redirect_host(client_address, ogn_data) host, port = self._get_redirect_host(client_address, ogn_data)
if port == 0: if port == 0:
raise Exception('can not parse header') raise Exception('can not parse header')
data = b"\x03" + common.chr(len(host)) + common.to_bytes(host) + struct.pack('>H', port) data = b"\x03" + common.to_bytes(common.chr(len(host))) + common.to_bytes(host) + struct.pack('>H', port)
logging.warn("TCP data redir %s:%d %s" % (host, port, binascii.hexlify(data))) logging.warn("TCP data redir %s:%d %s" % (host, port, binascii.hexlify(data)))
return data + ogn_data return data + ogn_data

Loading…
Cancel
Save