Skip to content

Instantly share code, notes, and snippets.

@claudijd
Created September 17, 2019 21:08
Show Gist options
  • Save claudijd/fb9bf14d69abb65263bc3b75bdf48a98 to your computer and use it in GitHub Desktop.
Save claudijd/fb9bf14d69abb65263bc3b75bdf48a98 to your computer and use it in GitHub Desktop.
Simplistic prototype for connections expiry
import datetime
import time
# Simple connections management dict in Python example
connections = {}
class Connection:
def __init__(self):
self.creation_time = datetime.datetime.now()
for conn_id in range(10):
print("Adding connection " + str(conn_id))
connections[conn_id] = Connection()
time.sleep(1)
while len(connections) > 0:
for conn_id in list(connections):
expire_window = datetime.datetime.now() - datetime.timedelta(seconds=20)
if connections[conn_id].creation_time < expire_window:
print("Expiring connection " + str(conn_id))
connections.pop(conn_id)
print("Waiting for next expiry check iterval")
time.sleep(5)
print("No more work to do, it's quitting time!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment