-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpostgres.py
More file actions
612 lines (515 loc) · 20 KB
/
postgres.py
File metadata and controls
612 lines (515 loc) · 20 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
"""PostgreSQL-backed StateAdapter implementation.
Python port of ``@chat-adapter/state-pg`` (index.ts).
Uses ``asyncpg`` for async PostgreSQL operations.
Suitable for production use with row-level locking and persistent state.
"""
from __future__ import annotations
import asyncio
import datetime as _dt
import json
import logging
import os
import time
import uuid
from typing import Any
from chat_sdk.types import Lock, QueueEntry
_logger = logging.getLogger(__name__)
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
def _generate_token() -> str:
return f"pg_{uuid.uuid4()}"
def _now_ms() -> int:
return int(time.time() * 1000)
# ---------------------------------------------------------------------------
# Schema DDL
# ---------------------------------------------------------------------------
_SCHEMA_STATEMENTS: list[str] = [
"""
CREATE TABLE IF NOT EXISTS chat_state_subscriptions (
key_prefix text NOT NULL,
thread_id text NOT NULL,
created_at timestamptz NOT NULL DEFAULT now(),
PRIMARY KEY (key_prefix, thread_id)
)
""",
"""
CREATE TABLE IF NOT EXISTS chat_state_locks (
key_prefix text NOT NULL,
thread_id text NOT NULL,
token text NOT NULL,
expires_at timestamptz NOT NULL,
updated_at timestamptz NOT NULL DEFAULT now(),
PRIMARY KEY (key_prefix, thread_id)
)
""",
"""
CREATE TABLE IF NOT EXISTS chat_state_cache (
key_prefix text NOT NULL,
cache_key text NOT NULL,
value text NOT NULL,
expires_at timestamptz,
updated_at timestamptz NOT NULL DEFAULT now(),
PRIMARY KEY (key_prefix, cache_key)
)
""",
"""
CREATE INDEX IF NOT EXISTS chat_state_locks_expires_idx
ON chat_state_locks (expires_at)
""",
"""
CREATE INDEX IF NOT EXISTS chat_state_cache_expires_idx
ON chat_state_cache (expires_at)
""",
"""
CREATE TABLE IF NOT EXISTS chat_state_lists (
key_prefix text NOT NULL,
list_key text NOT NULL,
seq bigserial NOT NULL,
value text NOT NULL,
expires_at timestamptz,
PRIMARY KEY (key_prefix, list_key, seq)
)
""",
"""
CREATE INDEX IF NOT EXISTS chat_state_lists_expires_idx
ON chat_state_lists (expires_at)
""",
"""
CREATE TABLE IF NOT EXISTS chat_state_queues (
key_prefix text NOT NULL,
thread_id text NOT NULL,
seq bigserial NOT NULL,
value text NOT NULL,
expires_at timestamptz NOT NULL,
PRIMARY KEY (key_prefix, thread_id, seq)
)
""",
"""
CREATE INDEX IF NOT EXISTS chat_state_queues_expires_idx
ON chat_state_queues (expires_at)
""",
]
# ---------------------------------------------------------------------------
# PostgresStateAdapter
# ---------------------------------------------------------------------------
class PostgresStateAdapter:
"""PostgreSQL state adapter for production use.
Provides persistent subscriptions and row-level locking.
Auto-creates required tables on first ``connect()``.
Implements the full :class:`~chat_sdk.types.StateAdapter` protocol.
"""
def __init__(
self,
*,
url: str | None = None,
pool: Any | None = None,
key_prefix: str = "chat-sdk",
) -> None:
self._key_prefix = key_prefix
self._connected = False
self._connect_lock = asyncio.Lock()
self._owns_pool = pool is None
if pool is not None:
self._pool = pool
self._url: str | None = None
else:
self._pool = None # created lazily in connect()
resolved_url = url or os.environ.get("POSTGRES_URL") or os.environ.get("DATABASE_URL")
if not resolved_url:
raise ValueError(
"Postgres url is required. Set POSTGRES_URL or DATABASE_URL, or provide url in options."
)
self._url = resolved_url
# -- lifecycle -----------------------------------------------------------
async def connect(self) -> None:
if self._connected:
return
async with self._connect_lock:
if self._connected:
return
try:
if self._pool is None:
import asyncpg
self._pool = await asyncpg.create_pool(dsn=self._url)
# Verify connectivity
await self._pool.fetchval("SELECT 1")
await self._ensure_schema()
self._connected = True
except Exception:
_logger.exception("Postgres connect failed")
raise
async def disconnect(self) -> None:
if not self._connected:
return
if self._owns_pool and self._pool is not None:
await self._pool.close()
self._connected = False
# -- subscriptions -------------------------------------------------------
async def subscribe(self, thread_id: str) -> None:
self._ensure_connected()
await self._pool.execute(
"""INSERT INTO chat_state_subscriptions (key_prefix, thread_id)
VALUES ($1, $2)
ON CONFLICT DO NOTHING""",
self._key_prefix,
thread_id,
)
async def unsubscribe(self, thread_id: str) -> None:
self._ensure_connected()
await self._pool.execute(
"""DELETE FROM chat_state_subscriptions
WHERE key_prefix = $1 AND thread_id = $2""",
self._key_prefix,
thread_id,
)
async def is_subscribed(self, thread_id: str) -> bool:
self._ensure_connected()
row = await self._pool.fetchrow(
"""SELECT 1 FROM chat_state_subscriptions
WHERE key_prefix = $1 AND thread_id = $2
LIMIT 1""",
self._key_prefix,
thread_id,
)
return row is not None
# -- locking -------------------------------------------------------------
async def acquire_lock(self, thread_id: str, ttl_ms: int) -> Lock | None:
self._ensure_connected()
token = _generate_token()
# Atomic upsert: INSERT succeeds for new rows; ON CONFLICT DO UPDATE
# fires only when the existing row is expired (WHERE expires_at <= now()).
# Postgres acquires a row lock on the conflicting row, so only one
# concurrent caller can win — eliminating the TOCTOU race that existed
# in the previous two-step INSERT-then-UPDATE approach.
row = await self._pool.fetchrow(
"""INSERT INTO chat_state_locks (key_prefix, thread_id, token, expires_at)
VALUES ($1, $2, $3, now() + make_interval(secs => $4::float / 1000))
ON CONFLICT (key_prefix, thread_id) DO UPDATE
SET token = EXCLUDED.token,
expires_at = EXCLUDED.expires_at,
updated_at = now()
WHERE chat_state_locks.expires_at <= now()
RETURNING thread_id, token, expires_at""",
self._key_prefix,
thread_id,
token,
ttl_ms,
)
if row is None:
return None
return Lock(
thread_id=row["thread_id"],
token=row["token"],
expires_at=int(row["expires_at"].timestamp() * 1000),
)
async def force_release_lock(self, thread_id: str) -> None:
self._ensure_connected()
await self._pool.execute(
"""DELETE FROM chat_state_locks
WHERE key_prefix = $1 AND thread_id = $2""",
self._key_prefix,
thread_id,
)
async def release_lock(self, lock: Lock) -> None:
self._ensure_connected()
await self._pool.execute(
"""DELETE FROM chat_state_locks
WHERE key_prefix = $1 AND thread_id = $2 AND token = $3""",
self._key_prefix,
lock.thread_id,
lock.token,
)
async def extend_lock(self, lock: Lock, ttl_ms: int) -> bool:
self._ensure_connected()
result = await self._pool.execute(
"""UPDATE chat_state_locks
SET expires_at = now() + $1 * interval '1 millisecond',
updated_at = now()
WHERE key_prefix = $2
AND thread_id = $3
AND token = $4
AND expires_at > now()""",
ttl_ms,
self._key_prefix,
lock.thread_id,
lock.token,
)
# asyncpg returns a command tag like "UPDATE 1" or "UPDATE 0"
return result is not None and result.endswith("1")
# -- key/value cache -----------------------------------------------------
async def get(self, key: str) -> Any | None:
self._ensure_connected()
row = await self._pool.fetchrow(
"""SELECT value FROM chat_state_cache
WHERE key_prefix = $1 AND cache_key = $2
AND (expires_at IS NULL OR expires_at > now())
LIMIT 1""",
self._key_prefix,
key,
)
if row is None:
# Opportunistic cleanup of expired entry
await self._pool.execute(
"""DELETE FROM chat_state_cache
WHERE key_prefix = $1 AND cache_key = $2
AND expires_at <= now()""",
self._key_prefix,
key,
)
return None
try:
return json.loads(row["value"])
except (json.JSONDecodeError, TypeError):
return row["value"]
async def set(self, key: str, value: Any, ttl_ms: int | None = None) -> None:
self._ensure_connected()
serialized = json.dumps(value)
expires_at = _pg_timestamp_from_ms(ttl_ms) if ttl_ms else None
await self._pool.execute(
"""INSERT INTO chat_state_cache (key_prefix, cache_key, value, expires_at)
VALUES ($1, $2, $3, $4)
ON CONFLICT (key_prefix, cache_key) DO UPDATE
SET value = EXCLUDED.value,
expires_at = EXCLUDED.expires_at,
updated_at = now()""",
self._key_prefix,
key,
serialized,
expires_at,
)
async def set_if_not_exists(self, key: str, value: Any, ttl_ms: int | None = None) -> bool:
self._ensure_connected()
serialized = json.dumps(value)
expires_at = _pg_timestamp_from_ms(ttl_ms) if ttl_ms else None
result = await self._pool.execute(
"""INSERT INTO chat_state_cache (key_prefix, cache_key, value, expires_at)
VALUES ($1, $2, $3, $4)
ON CONFLICT (key_prefix, cache_key) DO NOTHING""",
self._key_prefix,
key,
serialized,
expires_at,
)
# asyncpg returns "INSERT 0 1" on success, "INSERT 0 0" on conflict
return result is not None and result.endswith("1")
async def delete(self, key: str) -> None:
self._ensure_connected()
await self._pool.execute(
"""DELETE FROM chat_state_cache
WHERE key_prefix = $1 AND cache_key = $2""",
self._key_prefix,
key,
)
# -- lists ---------------------------------------------------------------
async def append_to_list(
self,
key: str,
value: Any,
*,
max_length: int | None = None,
ttl_ms: int | None = None,
) -> None:
self._ensure_connected()
serialized = json.dumps(value)
expires_at = _pg_timestamp_from_ms(ttl_ms) if ttl_ms else None
# Insert the new entry
await self._pool.execute(
"""INSERT INTO chat_state_lists (key_prefix, list_key, value, expires_at)
VALUES ($1, $2, $3, $4)""",
self._key_prefix,
key,
serialized,
expires_at,
)
# Trim overflow if max_length is specified
if max_length:
await self._pool.execute(
"""DELETE FROM chat_state_lists
WHERE key_prefix = $1 AND list_key = $2 AND seq IN (
SELECT seq FROM chat_state_lists
WHERE key_prefix = $1 AND list_key = $2
ORDER BY seq ASC
OFFSET 0
LIMIT GREATEST(
(SELECT count(*) FROM chat_state_lists
WHERE key_prefix = $1 AND list_key = $2) - $3,
0
)
)""",
self._key_prefix,
key,
max_length,
)
# Update TTL on all entries for this key
if expires_at:
await self._pool.execute(
"""UPDATE chat_state_lists
SET expires_at = $3
WHERE key_prefix = $1 AND list_key = $2""",
self._key_prefix,
key,
expires_at,
)
async def get_list(self, key: str) -> list[Any]:
self._ensure_connected()
rows = await self._pool.fetch(
"""SELECT value FROM chat_state_lists
WHERE key_prefix = $1 AND list_key = $2
AND (expires_at IS NULL OR expires_at > now())
ORDER BY seq ASC""",
self._key_prefix,
key,
)
result: list[Any] = []
for row in rows:
try:
result.append(json.loads(row["value"]))
except (json.JSONDecodeError, TypeError):
result.append(row["value"])
return result
# -- queues --------------------------------------------------------------
async def enqueue(self, thread_id: str, entry: QueueEntry, max_size: int) -> int:
self._ensure_connected()
if hasattr(entry.message, "to_json"):
msg_data = entry.message.to_json()
elif isinstance(entry.message, dict):
msg_data = entry.message
else:
msg_data = entry.message.__dict__
serialized = json.dumps(
{
"enqueued_at": entry.enqueued_at,
"expires_at": entry.expires_at,
"message": msg_data,
}
)
expires_at = _pg_timestamp_from_epoch_ms(entry.expires_at)
# Wrap insert + trim in a transaction to avoid TOCTOU races
async with self._pool.acquire() as conn, conn.transaction():
# Purge expired entries first
await conn.execute(
"""DELETE FROM chat_state_queues
WHERE key_prefix = $1 AND thread_id = $2 AND expires_at <= now()""",
self._key_prefix,
thread_id,
)
# Insert the new entry
await conn.execute(
"""INSERT INTO chat_state_queues (key_prefix, thread_id, value, expires_at)
VALUES ($1, $2, $3, $4)""",
self._key_prefix,
thread_id,
serialized,
expires_at,
)
# Trim overflow (keep newest max_size non-expired entries)
if max_size > 0:
await conn.execute(
"""DELETE FROM chat_state_queues
WHERE key_prefix = $1 AND thread_id = $2 AND seq IN (
SELECT seq FROM chat_state_queues
WHERE key_prefix = $1 AND thread_id = $2
AND expires_at > now()
ORDER BY seq ASC
OFFSET 0
LIMIT GREATEST(
(SELECT count(*) FROM chat_state_queues
WHERE key_prefix = $1 AND thread_id = $2 AND expires_at > now()) - $3,
0
)
)""",
self._key_prefix,
thread_id,
max_size,
)
# Return current non-expired depth
depth = await conn.fetchval(
"""SELECT count(*) FROM chat_state_queues
WHERE key_prefix = $1 AND thread_id = $2 AND expires_at > now()""",
self._key_prefix,
thread_id,
)
return int(depth)
async def dequeue(self, thread_id: str) -> QueueEntry | None:
self._ensure_connected()
# Purge expired entries first
await self._pool.execute(
"""DELETE FROM chat_state_queues
WHERE key_prefix = $1 AND thread_id = $2 AND expires_at <= now()""",
self._key_prefix,
thread_id,
)
# Atomically select + delete the oldest non-expired entry
row = await self._pool.fetchrow(
"""DELETE FROM chat_state_queues
WHERE key_prefix = $1 AND thread_id = $2
AND seq = (
SELECT seq FROM chat_state_queues
WHERE key_prefix = $1 AND thread_id = $2
AND expires_at > now()
ORDER BY seq ASC
LIMIT 1
)
RETURNING value""",
self._key_prefix,
thread_id,
)
if row is None:
return None
data = json.loads(row["value"])
msg_data = data["message"]
if isinstance(msg_data, dict) and msg_data.get("_type") == "chat:Message":
from chat_sdk.types import Message
msg = Message.from_json(msg_data)
else:
msg = msg_data
return QueueEntry(
enqueued_at=data["enqueued_at"],
expires_at=data["expires_at"],
message=msg,
)
async def queue_depth(self, thread_id: str) -> int:
self._ensure_connected()
depth = await self._pool.fetchval(
"""SELECT count(*) FROM chat_state_queues
WHERE key_prefix = $1 AND thread_id = $2 AND expires_at > now()""",
self._key_prefix,
thread_id,
)
return int(depth)
# -- introspection -------------------------------------------------------
def get_pool(self) -> Any:
"""Return the underlying asyncpg pool for advanced usage."""
return self._pool
# -- internal ------------------------------------------------------------
def _ensure_connected(self) -> None:
if not self._connected:
raise RuntimeError("PostgresStateAdapter is not connected. Call connect() first.")
async def _ensure_schema(self) -> None:
"""Create required tables and indexes if they do not exist."""
for stmt in _SCHEMA_STATEMENTS:
await self._pool.execute(stmt)
# ---------------------------------------------------------------------------
# Timestamp helpers
# ---------------------------------------------------------------------------
def _pg_timestamp_from_ms(ttl_ms: int) -> _dt.datetime:
"""Return a timezone-aware datetime ``ttl_ms`` milliseconds from now."""
return _dt.datetime.now(_dt.UTC) + _dt.timedelta(milliseconds=ttl_ms)
def _pg_timestamp_from_epoch_ms(epoch_ms: int) -> _dt.datetime:
"""Return a timezone-aware datetime from an epoch-millisecond value."""
return _dt.datetime.fromtimestamp(epoch_ms / 1000, tz=_dt.UTC)
# ---------------------------------------------------------------------------
# Factory
# ---------------------------------------------------------------------------
def create_postgres_state(
*,
url: str | None = None,
pool: Any | None = None,
key_prefix: str = "chat-sdk",
) -> PostgresStateAdapter:
"""Create a new PostgreSQL state adapter.
Either provide a ``url`` or an existing asyncpg ``pool``. If neither is
given, the ``POSTGRES_URL`` / ``DATABASE_URL`` environment variable is used.
"""
return PostgresStateAdapter(url=url, pool=pool, key_prefix=key_prefix)