Skip to content

Commit

Permalink
More accurate connected users
Browse files Browse the repository at this point in the history
  • Loading branch information
AriAlavi committed Sep 1, 2020
1 parent 1b75a54 commit d9cc41d
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions network.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,16 @@ def __init__(self, shared_reference):
def add(self, ip):
self.conns[ip] = time.time()
self.shared_reference[0] = len(self.conns.keys())
def remove(self, ip):
try:
del self.conns[ip]
except:
pass
def purge(self):
now = time.time()
to_remove = []
for ip, last in self.conns.items():
if now - last > 60:
if now - last > 60 * 10:
to_remove.append(ip)
for ip in to_remove:
del self.conns[ip]
Expand Down Expand Up @@ -177,9 +182,12 @@ async def _handle_client(self, r, w):
assert isinstance(w, asyncio.StreamWriter)
try:
func = self.BYTE_MAP[await r.read(1)]
self.connection_q.add(r._transport.get_extra_info('peername'))
ip = r._transport.get_extra_info('peername')
self.connection_q.add(ip)
await func(self, r, w)
self.connection_q.remove(ip)
except ConnectionResetError:
self.connection_q.remove(ip)
pass
async def killServer(self):

Expand Down

0 comments on commit d9cc41d

Please sign in to comment.