GitHub

Strategy

API reference for Strategy.

class Strategy[A]

How a PubSub handles messages that do not fit and wakes parked takes (Effect's PubSub.Strategy). BackPressureStrategy, DroppingStrategy and SlidingStrategy implement it; so can user code. Subclassing it inherits the two complete_* hooks as the built-ins have them.

Attributes

AttributeTypeDescription
shutdownEffect[None]

Run after the hub shut down and unsubscribed everyone.

Functions

handle_surplus

handle_surplus(pubsub, subscribers, elements, is_shutdown, ended) -> Effect[bool]

A publish's elements did not fit. The publisher may have yielded since publish checked ended(), so check it again before parking or publishing the surplus.

ParameterTypeDescription
pubsubAtomic[A]
subscribersSubscribers[A]
elementsIterable[A]
is_shutdownCallable[[], bool]
endedCallable[[], Option[A]]

Returns Effect[bool]

def handle_surplus(
    self,
    pubsub: Atomic[A],
    subscribers: Subscribers[A],
    elements: Iterable[A],
    is_shutdown: Callable[[], bool],
    ended: Callable[[], Option[A]],
) -> Effect[bool]:
    """A publish's ``elements`` did not fit. The publisher may have
    yielded since ``publish`` checked ``ended()``, so check it again
    before parking or publishing the surplus."""
    ...

on_pubsub_empty_space_unsafe

on_pubsub_empty_space_unsafe(pubsub, subscribers) -> None

A subscriber read, so the store may have room.

ParameterTypeDescription
pubsubAtomic[A]
subscribersSubscribers[A]
def on_pubsub_empty_space_unsafe(self, pubsub: Atomic[A], subscribers: Subscribers[A]) -> None:
    """A subscriber read, so the store may have room."""
    ...

complete_pollers_unsafe

complete_pollers_unsafe(pubsub, subscribers, subscription, pollers) -> None

Give subscription's messages to its parked takes.

ParameterTypeDescription
pubsubAtomic[A]
subscribersSubscribers[A]
subscriptionBackingSubscription[A]
pollersPollers[A]
def complete_pollers_unsafe(
    self,
    pubsub: Atomic[A],
    subscribers: Subscribers[A],
    subscription: BackingSubscription[A],
    pollers: Pollers[A],
) -> None:
    """Give ``subscription``'s messages to its parked takes."""
    while not subscription.is_empty():
        poller = pollers.take()
        if poller is None:
            subscribers.pop(subscription, None)
            return
        polled = subscription.poll()
        if polled is None:
            pollers.prepend(poller)
        else:
            poller.succeed(polled.value)
            self.on_pubsub_empty_space_unsafe(pubsub, subscribers)

complete_subscribers_unsafe

complete_subscribers_unsafe(pubsub, subscribers) -> None

New messages were stored: give them to the parked takes.

ParameterTypeDescription
pubsubAtomic[A]
subscribersSubscribers[A]
def complete_subscribers_unsafe(self, pubsub: Atomic[A], subscribers: Subscribers[A]) -> None:
    """New messages were stored: give them to the parked takes."""
    for subscription in subscribers:
        pollers = subscribers.get(subscription)
        if pollers is not None:
            self.complete_pollers_unsafe(pubsub, subscribers, subscription, pollers)