Created
September 17, 2019 21:08
-
-
Save claudijd/fb9bf14d69abb65263bc3b75bdf48a98 to your computer and use it in GitHub Desktop.
Simplistic prototype for connections expiry
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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