popoto.backends.postgres.listen¶
popoto.backends.postgres.listen
¶
One shared LISTEN session per process and DSN (#799).
Every Postgres LISTEN in a process -- each Subscriber's
:class:~.pubsub.PostgresPubSub and each blocking stream read's
:class:~.events.EventListener, sync or async -- rides one dedicated
session per DSN, owned by a :class:ListenHub. The hub's thread holds that
connection (outside the pool, as a LISTEN must be: ยง3 Topology),
LISTEN\ s the union of the channels its sinks want, and hands each
notification to every sink of its channel, in arrival order, on that one
thread. A channel is UNLISTEN\ ed when its last sink detaches, and the
session is closed when no sink is left, so an idle process holds none.
Attaching. :meth:ListenHub.attach returns once the sink is live: the
first sink of a channel is live from the moment its LISTEN runs; a later
one from a barrier -- a pg_notify the hub sends itself on that channel
and swallows when it comes back. Postgres delivers notifications in commit
order, so the barrier splits the stream exactly: what committed before it is
never handed to the new sink (as a fresh session's LISTEN never sees an
earlier NOTIFY), what commits after it always is. Stream waiters skip the
barrier (barrier=False): an extra wake-up only costs them one read.
Reconnecting. When the session drops (pg_terminate_backend, a
restart, a network fault) the hub reconnects at once, then with backoff
(:data:LISTEN_RECONNECT_BACKOFF_SECONDS doubling to
:data:LISTEN_RECONNECT_MAX_BACKOFF_SECONDS), re-LISTEN\ s every
channel, and tells each sink (:meth:Sink.reconnected). Notifications sent
while it was down are lost -- NOTIFY is not stored for a session that
is not listening. Stream readers lose nothing by it: entries are rows, and a
reconnect wakes every waiter to read again. Pub/sub loses what was published
in the gap, as a Redis subscriber loses what was published while its
connection was down.
Dead connections (#803). A peer that vanished without a FIN or RST (a
NAT entry expiring) never makes the socket readable, so the session sets
TCP keepalives (Defaults.PG_LISTEN_KEEPALIVES*, unless the DSN names
them) and the hub's wait is bounded: after
Defaults.PG_LISTEN_LIVENESS_INTERVAL_SECONDS without traffic it sends
SELECT 1 and drops the session -- reconnecting as above -- when no reply
comes within Defaults.PG_LISTEN_LIVENESS_TIMEOUT_SECONDS.
Undecodable payloads. A notification is decoded with the session's
encoding. Bytes it cannot decode -- possible only on a SQL_ASCII
database, where the server stores bytes unchecked, so any non-ASCII
NOTIFY (even valid UTF-8) fails the ascii codec -- are decoded as
UTF-8 with errors="replace" (U+FFFD for each invalid sequence) and
delivered as str, the sink contract; the first is logged at WARNING,
once per hub. popoto's own payloads are ASCII and never take that path, and
pub/sub drops a foreign payload anyway, so this only keeps a stranger's
NOTIFY from stopping the hub's thread. A replaced payload is measured
by its decoded form, so its byte size may differ from the server's.
Fork. Hubs are per (dsn, pid): a child process never touches the
parent's socket (it never even closes it -- closing would end the parent's
session), and opens its own on first use. Sinks re-attach when they notice
the pid changed.
Threads. Every hub method is safe from any thread; only the hub's own
thread touches its connection. Sinks are called on the hub's thread and must
not block -- :meth:Sink.resume included, which is why pub/sub re-registers
from a snapshot rather than behind a subscriber's own write lock.
LISTEN_RECONNECT_BACKOFF_SECONDS = 0.2
module-attribute
¶
The pause after a failed reconnect attempt; doubled after each further failure (the first attempt after a drop is immediate).
LISTEN_RECONNECT_MAX_BACKOFF_SECONDS = 5.0
module-attribute
¶
The ceiling the reconnect backoff doubles to.
LISTEN_APPLICATION_NAME = 'popoto_listen'
module-attribute
¶
The application_name the shared session reports in
pg_stat_activity when its DSN names none.
WAKE_SINK_MAX_PAYLOADS = 4096
module-attribute
¶
Distinct stream digests a blocking read remembers between waits
(:class:WakeSink); past it, the next wait wakes at once.
Sink
¶
Something a :class:ListenHub delivers to. Both methods run on the
hub's thread: they must be quick and must not raise.
Source code in src/popoto/backends/postgres/listen.py
dropped = 0
class-attribute
instance-attribute
¶
Payloads this sink discarded unread (:class:QueueSink's caps); the
hub adds every increase to :attr:ListenHub.dropped.
resume(conn, backend_pid)
¶
After a reconnect, before :meth:reconnected: re-establish any
server-side state tied to the session, on the hub's own conn
(autocommit; only this call may use it). Pub/sub re-registers its
subscriptions under the new pid here, so publish's count is
right as soon as the session is back.
Source code in src/popoto/backends/postgres/listen.py
reconnected()
¶
QueueSink
¶
Bases: Sink
Every payload, in arrival order, for one reader (a PubSub).
Bounded by count (Defaults.PG_LISTEN_QUEUE_MAX_MESSAGES) and by
payload size (Defaults.PG_LISTEN_QUEUE_MAX_BYTES, Redis's 32 MiB
pub/sub output-buffer hard limit), both read when the sink is made: a
subscriber that stops reading loses its oldest messages rather than
growing without limit -- the analogue of Redis disconnecting a pub/sub
client past its client-output-buffer-limit. Each drop is counted
(:attr:dropped, :attr:dropped_bytes, and the hub's
:attr:ListenHub.dropped) and logged at WARNING, on the first and then
every 10 000th.
Source code in src/popoto/backends/postgres/listen.py
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 | |
queued_bytes = 0
instance-attribute
¶
UTF-8 bytes of the payloads queued unread now. popoto's own are
ASCII (base64 and hex), but anyone may NOTIFY the channel with
any text, so a payload is measured in bytes, not characters.
take(timeout)
¶
Everything queued, waiting up to timeout (None: until
something arrives) for the first; and whether the session was
reopened since the last take.
Source code in src/popoto/backends/postgres/listen.py
WakeSink
¶
Bases: Sink
The payloads seen since the last wait, for a blocking stream read:
it only needs to know whether one it waits for arrived. Bounded at
:data:WAKE_SINK_MAX_PAYLOADS distinct payloads, past which every wait
answers "read again".
Source code in src/popoto/backends/postgres/listen.py
interrupt()
¶
wait(wanted, timeout)
¶
Wait up to timeout for a wanted payload, a reconnect or an
interrupt: (woken, reconnected). Forgets what it saw.
Source code in src/popoto/backends/postgres/listen.py
ListenHub
¶
The process's one LISTEN session on dsn: see the module
docstring. Get it with :func:hub_for.
Source code in src/popoto/backends/postgres/listen.py
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 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 | |
generation = 0
instance-attribute
¶
Bumped by every successful connect.
dropped = 0
instance-attribute
¶
Messages every sink of this hub has discarded unread (the
:class:QueueSink caps), summed over the hub's life.
attach(channel, sink, *, barrier=True, timeout=None)
¶
Deliver channel's notifications to sink from now on.
Returns once the sink is live (see the module docstring); raises the
connection's error when the session cannot be opened.
Source code in src/popoto/backends/postgres/listen.py
detach(channel, sink)
¶
Stop delivering to sink; UNLISTEN the channel when it was
the last. Never blocks.
Source code in src/popoto/backends/postgres/listen.py
listening()
¶
close()
¶
Drop every sink and close the session (tests, shutdown).
Source code in src/popoto/backends/postgres/listen.py
hub_for(dsn)
¶
This process's hub for dsn (created on first use, and again in a
forked child, which never reuses the parent's).
Source code in src/popoto/backends/postgres/listen.py
close_hubs()
¶
Close every hub this process opened (tests, shutdown). Hubs a forked child inherited are left alone: their sessions are the parent's.