dsmq 0.4.0__py3-none-any.whl → 0.5.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
dsmq/dsmq.py CHANGED
@@ -10,6 +10,8 @@ DEFAULT_PORT = 30008
10
10
 
11
11
  N_RETRIES = 5
12
12
  FIRST_RETRY = 0.01 # seconds
13
+ TIME_TO_LIVE = 60 # seconds
14
+ TIME_TO_LIVE = 5 # seconds
13
15
 
14
16
 
15
17
  def start_server(host=DEFAULT_HOST, port=DEFAULT_PORT):
@@ -79,8 +81,9 @@ def _handle_client_connection(socket_conn):
79
81
  sqlite_conn = sqlite3.connect("file:mem1?mode=memory&cache=shared")
80
82
  cursor = sqlite_conn.cursor()
81
83
 
82
- creation_time = time.time()
83
- last_read = {}
84
+ client_creation_time = time.time()
85
+ last_read_times = {}
86
+ time_of_last_purge = time.time()
84
87
 
85
88
  while True:
86
89
  data = socket_conn.recv(1024)
@@ -123,10 +126,10 @@ VALUES (:timestamp, :topic, :message)
123
126
 
124
127
  elif msg["action"] == "get":
125
128
  try:
126
- last_read_time = last_read[topic]
129
+ last_read_time = last_read_times[topic]
127
130
  except KeyError:
128
- last_read[topic] = creation_time
129
- last_read_time = last_read[topic]
131
+ last_read_times[topic] = client_creation_time
132
+ last_read_time = last_read_times[topic]
130
133
  msg["last_read_time"] = last_read_time
131
134
 
132
135
  # This block allows for multiple retries if the database
@@ -159,7 +162,7 @@ AND timestamp = a.min_time
159
162
  result = cursor.fetchall()[0]
160
163
  message = result[0]
161
164
  timestamp = result[1]
162
- last_read[topic] = timestamp
165
+ last_read_times[topic] = timestamp
163
166
  except IndexError:
164
167
  # Handle the case where no results are returned
165
168
  message = ""
@@ -169,6 +172,20 @@ AND timestamp = a.min_time
169
172
  else:
170
173
  print("Action must either be 'put' or 'get'")
171
174
 
175
+ # Periodically clean out messages from the queue that are
176
+ # past their sell buy date.
177
+ # This operation is pretty fast. I clock it at 12 us on my machine.
178
+ if time.time() - time_of_last_purge > TIME_TO_LIVE:
179
+ cursor.execute(
180
+ """
181
+ DELETE FROM messages
182
+ WHERE timestamp < :time_threshold
183
+ """,
184
+ {"time_threshold": time_of_last_purge}
185
+ )
186
+ sqlite_conn.commit()
187
+ time_of_last_purge = time.time()
188
+
172
189
  sqlite_conn.close()
173
190
 
174
191
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: dsmq
3
- Version: 0.4.0
3
+ Version: 0.5.0
4
4
  Summary: A dead simple message queue
5
5
  Requires-Python: >=3.10
6
6
  Description-Content-Type: text/markdown
@@ -1,10 +1,10 @@
1
1
  dsmq/__init__.py,sha256=YCgbnQAk8YbtHRyMcU0v2O7RdRhPhlT-vS_q40a7Q6g,50
2
2
  dsmq/demo_linux.py,sha256=7yLglGmirDLuuyMxppYSK-dfx2Fg2Q0dIWB4cl2yV1c,622
3
- dsmq/dsmq.py,sha256=5fo3wQfw2lIXiDGB05sRwVLDN0ZoUSw7e7WO8xOLXJY,5558
3
+ dsmq/dsmq.py,sha256=dKYtGkg8Kwll2sCytCqSI5klFs2cwm2pyXAkyQk4RdU,6206
4
4
  dsmq/example_get_client.py,sha256=chFfB2949PBENmgdUc3ASrATq1m4wvHGBzEnOC-o_Xs,296
5
5
  dsmq/example_put_client.py,sha256=mUKCRhmUieMZEpHLFWFvzeKB6IR7A8l4tWN8TvPzdKU,348
6
6
  dsmq/example_server.py,sha256=kkXOPaaTzVxf9_iIM76zU9pZhkPna_1vcGWkPrhCjus,61
7
- dsmq-0.4.0.dist-info/METADATA,sha256=T3godcTW__I3b2hj2dgJCjMJEGEo6S1HP_Iq5EQD6Ok,3878
8
- dsmq-0.4.0.dist-info/WHEEL,sha256=3U_NnUcV_1B1kPkYaPzN-irRckL5VW_lytn0ytO_kRY,87
9
- dsmq-0.4.0.dist-info/licenses/LICENSE,sha256=3Yu1mAp5VsKmnDtzkiOY7BdmrLeNwwZ3t6iWaLnlL0Y,1071
10
- dsmq-0.4.0.dist-info/RECORD,,
7
+ dsmq-0.5.0.dist-info/METADATA,sha256=EQISO0WcVsb7smXIie-ZP7pIO1itY8FZfjGw3DPuZDM,3878
8
+ dsmq-0.5.0.dist-info/WHEEL,sha256=3U_NnUcV_1B1kPkYaPzN-irRckL5VW_lytn0ytO_kRY,87
9
+ dsmq-0.5.0.dist-info/licenses/LICENSE,sha256=3Yu1mAp5VsKmnDtzkiOY7BdmrLeNwwZ3t6iWaLnlL0Y,1071
10
+ dsmq-0.5.0.dist-info/RECORD,,
File without changes