Skip to content

Commit

Permalink
Only apply uptime penalty on repeated restarts
Browse files Browse the repository at this point in the history
  • Loading branch information
sfan5 committed Mar 15, 2021
1 parent a9ecf55 commit 5d5f31d
Showing 1 changed file with 40 additions and 8 deletions.
48 changes: 40 additions & 8 deletions server.py
Expand Up @@ -91,17 +91,17 @@ def announce():

if action == "update" and not old:
if app.config["ALLOW_UPDATE_WITHOUT_OLD"]:
old = server
old["start"] = time.time()
old["clients_top"] = 0
old["updates"] = 0
old["total_clients"] = 0
action = "start"
else:
return "Server to update not found."

server["update_time"] = time.time()
server["update_time"] = int(time.time())

server["start"] = time.time() if action == "start" else old["start"]
if action == "start":
server["start"] = int(time.time())
tracker.push("%s:%d" % (server["ip"], server["port"]), server["start"])
else:
server["start"] = old["start"]

if "clients_list" in server:
server["clients"] = len(server["clients_list"])
Expand Down Expand Up @@ -301,6 +301,29 @@ def asyncFinishThread(server):
serverList.update(server)


class UptimeTracker:
def __init__(self):
self.d = {}
self.cleanTime = 0
self.lock = RLock()
def push(self, id, ts):
with self.lock:
if time.time() >= self.cleanTime: # clear once in a while
self.d.clear()
self.cleanTime = time.time() + 48*60*60

if id in self.d:
self.d[id] = self.d[id][-1:] + [ts]
else:
self.d[id] = [0, ts]
# returns the before-last start time, in bulk
def getStartTimes(self):
ret = {}
with self.lock:
for k, v in self.d.items():
ret[k] = v[0]
return ret

class ServerList:
def __init__(self):
self.list = []
Expand Down Expand Up @@ -329,6 +352,8 @@ def remove(self, server):
pass

def sort(self):
start_times = tracker.getStartTimes()

def server_points(server):
points = 0

Expand Down Expand Up @@ -366,10 +391,13 @@ def server_points(server):
points -= (server["ping"] - 0.4) * 8

# Up to -8 for less than an hour of uptime (penalty linearly decreasing)
# only if the server has restarted before within the last 2 hours
HOUR_SECS = 60 * 60
uptime = server["uptime"]
if uptime < HOUR_SECS:
points -= ((HOUR_SECS - uptime) / HOUR_SECS) * 8
start_time = start_times.get("%s:%d" % (server["ip"], server["port"]), 0)
if start_time >= time.time() - 2 * HOUR_SECS:
points -= ((HOUR_SECS - uptime) / HOUR_SECS) * 8

# reduction to 40% for servers that support both legacy (v4) and v5 clients
if server["proto_min"] <= 32 and server["proto_max"] > 36:
Expand Down Expand Up @@ -446,6 +474,10 @@ def run(self):
time.sleep(60)
serverList.purgeOld()

# Globals / Startup

tracker = UptimeTracker()

serverList = ServerList()

PurgeThread().start()
Expand Down

0 comments on commit 5d5f31d

Please sign in to comment.