Pool discovery protocols shared across proxies on the same worker — Closes #55#117
Merged
conradbzura merged 5 commits intowool-labs:mainfrom Mar 25, 2026
Merged
Conversation
c430e3e to
bc950c0
Compare
conradbzura
commented
Mar 24, 2026
conradbzura
commented
Mar 24, 2026
conradbzura
commented
Mar 24, 2026
conradbzura
commented
Mar 24, 2026
conradbzura
commented
Mar 24, 2026
conradbzura
commented
Mar 24, 2026
conradbzura
commented
Mar 24, 2026
conradbzura
commented
Mar 24, 2026
ee94403 to
c1b34cd
Compare
conradbzura
commented
Mar 25, 2026
conradbzura
commented
Mar 25, 2026
conradbzura
commented
Mar 25, 2026
conradbzura
commented
Mar 25, 2026
conradbzura
commented
Mar 25, 2026
| original_init = cls.__init__ # type: ignore[misc] | ||
|
|
||
| def _subscriber_new(cls_arg: type, *args: Any, **kwargs: Any) -> Any: | ||
| key = cls_arg._cache_key(*args, **kwargs) # type: ignore[attr-defined] |
Contributor
Author
There was a problem hiding this comment.
This should be a kwarg to __new__ so that usage can be FooSub(metaclass=SubscriberMeta, key=...).
conradbzura
commented
Mar 25, 2026
874031a to
5c41926
Compare
…ol.get_or_create_sync Infrastructure for pooling discovery protocol subscriptions across proxies sharing the same worker. Adds an async filter class that wraps discovery event streams with transition tracking, a ContextVar for managing subscriber singletons, and a synchronous cache accessor on ResourcePool for use by metaclass construction.
Extracts the demand-driven multicast logic from _SharedSubscription into a reusable Fanout/FanoutConsumer pair in wool.utilities.fanout. A single async iterable source is fanned out to independent consumers on demand with no background task — the first consumer whose queue is empty acquires a lock, pulls one item, and distributes it.
be408de to
781d4c2
Compare
Introduce a SubscriberMeta metaclass that caches discovery subscriber instances as singletons in a global ResourcePool, keyed by protocol configuration. Each subscriber is wrapped in a SharedSubscription that uses Fanout to multicast a single event source to multiple concurrent consumers via a leader/follower pull model — no background task required. Late-joining consumers receive a replay of current workers so they start with a consistent view. All class-level state uses WeakKeyDictionary so entries cascade away when their keys are garbage-collected. Decouple filtering from discovery by using the afilter utility in subscribe() instead of passing predicates into the event stream. Simplify the Zeroconf and shared-memory listeners to emit all events unfiltered.
…nout Add tests for SubscriberMeta singleton caching and lazy pool initialization, SharedSubscription fan-out and late-join replay, afilter transition tracking with independent iteration state, and ResourcePool.get_or_create_sync. Add pool subscription, afilter, and discovery tests covering the new demand-driven multicast model. Add DURABLE_SHARED pool arrangement where two WorkerPool instances share the same LocalDiscovery subscriber, exercising SubscriberMeta caching and SharedSubscription fan-out end-to-end with late-joiner replay. Rewrite pool tests to exercise the new Fanout-backed iteration model instead of the removed queue/lock internals. Add coverage for the pool-absent RuntimeError guard, worker-dropped replay tracking, and concurrent fan-out scenarios. Update lan/local subscriber tests to use DiscoverySubscriberLike isinstance checks. Clear the subscriber pool ContextVar between tests.
781d4c2 to
43c6759
Compare
43c6759 to
3d16d90
Compare
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Rewrite the subscriber pooling layer so that proxies sharing the same discovery protocol reuse a single underlying subscription with demand-driven multicast. Introduce a
Fanoututility that wraps any async generator and distributes each item to multiple independent consumers via a leader/follower pull model — no background task required. Late-joining consumers receive a replay of all currently known workers so they start with a consistent view.All class-level state uses
WeakKeyDictionaryso that entries cascade away automatically when their keys are garbage-collected — no manual cleanup needed beyond closing the async generator on shutdown.Closes #55
Proposed changes
Fanoutmulticast utilityAdd a generic
Fanout[T]container that wraps a singleAsyncGenerator[T]source and multicasts each pulled item to every registeredFanoutConsumer. The first consumer whose queue is empty acquires a sharedasyncio.Lock, pulls one item from the source viaanext, distributes it to all other consumers' queues, and returns. Consumers are tracked in aWeakSet; when a consumer goes out of scope the weak reference expires and it is silently removed from the fan-out set.cleanup()closes the source generator and pushes a sentinel to all remaining consumer queues.Demand-driven
_SharedSubscriptionAdd a
_SharedSubscriptionwrapper that bridges a discovery subscriber and aFanout. Each__aiter__call enters aResourcePoolresource, lazily wraps the raw subscriber in a sharedFanout, and iterates an independentFanoutConsumer. Worker state is tracked as adict[uid, WorkerMetadata]per subscriber — only the latest metadata is stored, and entries are removed onworker-dropped. When a new consumer registers, it receives a replay ofworker-addedevents for all currently known workers.SubscriberMetavia__new__Switch from
__call__to__new__on the metaclass. The metaclass's__new__(called at class definition time) injects a custom__new__onto the subscriber class that caches the raw subscriber in theResourcePooland returns a_SharedSubscriptionwrapper. Since the returned object is not an instance of the subscriber class,type.__call__skips__init__— no double-initialization.Pickle support
Add
__reduce__to_SharedSubscription(delegates to the raw subscriber's__reduce__, so unpickling goes through the metaclass and re-wraps automatically) and toafilter(pickles predicate + inner subscriber).Subscriber simplification
Remove the
_consume()method from bothLanDiscovery.SubscriberandLocalDiscovery.Subscriber.__aiter__now returns_event_stream()directly._shutdownbecomes async and delegates to_SharedSubscription.cleanup.Integration test:
DURABLE_SHAREDpool modeAdd a new pool arrangement where two
WorkerPoolinstances share the sameLocalDiscovery. The second pool discovers existing workers via late-joiner replay. Include pairwise filter and Hypothesis strategy updates so the new mode is covered across all dimension combinations.Test cases