Documentation ¶
Overview ¶
Package grpcsync implements additional synchronization primitives built upon the sync package.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type CallbackSerializer ¶ added in v1.55.0
type CallbackSerializer struct {
// contains filtered or unexported fields
}
CallbackSerializer provides a mechanism to schedule callbacks in a synchronized manner. It provides a FIFO guarantee on the order of execution of scheduled callbacks. New callbacks can be scheduled by invoking the Schedule() method.
This type is safe for concurrent access.
func NewCallbackSerializer ¶ added in v1.55.0
func NewCallbackSerializer(ctx context.Context) *CallbackSerializer
NewCallbackSerializer returns a new CallbackSerializer instance. The provided context will be passed to the scheduled callbacks. Users should cancel the provided context to shutdown the CallbackSerializer. It is guaranteed that no callbacks will be added once this context is canceled, and any pending un-run callbacks will be executed before the serializer is shut down.
func (*CallbackSerializer) Done ¶ added in v1.56.0
func (cs *CallbackSerializer) Done() <-chan struct{}
Done returns a channel that is closed after the context passed to NewCallbackSerializer is canceled and all callbacks have been executed.
func (*CallbackSerializer) Schedule ¶ added in v1.55.0
func (cs *CallbackSerializer) Schedule(f func(ctx context.Context)) bool
Schedule adds a callback to be scheduled after existing callbacks are run.
Callbacks are expected to honor the context when performing any blocking operations, and should return early when the context is canceled.
Return value indicates if the callback was successfully added to the list of callbacks to be executed by the serializer. It is not possible to add callbacks once the context passed to NewCallbackSerializer is cancelled.
type Event ¶
type Event struct {
// contains filtered or unexported fields
}
Event represents a one-time event that may occur in the future.
func (*Event) Done ¶
func (e *Event) Done() <-chan struct{}
Done returns a channel that will be closed when Fire is called.
type PubSub ¶ added in v1.57.0
type PubSub struct {
// contains filtered or unexported fields
}
PubSub is a simple one-to-many publish-subscribe system that supports messages of arbitrary type. It guarantees that messages are delivered in the same order in which they were published.
Publisher invokes the Publish() method to publish new messages, while subscribers interested in receiving these messages register a callback via the Subscribe() method.
Once a PubSub is stopped, no more messages can be published, but any pending published messages will be delivered to the subscribers. Done may be used to determine when all published messages have been delivered.
func NewPubSub ¶ added in v1.57.0
NewPubSub returns a new PubSub instance. Users should cancel the provided context to shutdown the PubSub.
func (*PubSub) Done ¶ added in v1.58.0
func (ps *PubSub) Done() <-chan struct{}
Done returns a channel that is closed after the context passed to NewPubSub is canceled and all updates have been sent to subscribers.
func (*PubSub) Publish ¶ added in v1.57.0
Publish publishes the provided message to the PubSub, and invokes callbacks registered by subscribers asynchronously.
func (*PubSub) Subscribe ¶ added in v1.57.0
func (ps *PubSub) Subscribe(sub Subscriber) (cancel func())
Subscribe registers the provided Subscriber to the PubSub.
If the PubSub contains a previously published message, the Subscriber's OnMessage() callback will be invoked asynchronously with the existing message to begin with, and subsequently for every newly published message.
The caller is responsible for invoking the returned cancel function to unsubscribe itself from the PubSub.
type Subscriber ¶ added in v1.57.0
type Subscriber interface { // OnMessage is invoked when a new message is published. Implementations // must not block in this method. OnMessage(msg any) }
Subscriber represents an entity that is subscribed to messages published on a PubSub. It wraps the callback to be invoked by the PubSub when a new message is published.