Documentation ¶
Index ¶
- Constants
- type AsyncTaskNotifier
- func (n *AsyncTaskNotifier[T]) BlockAndGetResult() T
- func (n *AsyncTaskNotifier[T]) BlockUntilFinish()
- func (n *AsyncTaskNotifier[T]) Cancel()
- func (n *AsyncTaskNotifier[T]) Context() context.Context
- func (n *AsyncTaskNotifier[T]) Finish(result T)
- func (n *AsyncTaskNotifier[T]) FinishChan() <-chan struct{}
- type ContextCond
- type Future
- type VersionedListener
- type VersionedNotifier
Constants ¶
const ( VersionedListenAtEarliest versionedListenAt = -1 VersionedListenAtLatest versionedListenAt = -2 )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AsyncTaskNotifier ¶
type AsyncTaskNotifier[T any] struct { // contains filtered or unexported fields }
AsyncTaskNotifier is a notifier for async task.
func NewAsyncTaskNotifier ¶
func NewAsyncTaskNotifier[T any]() *AsyncTaskNotifier[T]
NewAsyncTaskNotifier creates a new async task notifier.
func (*AsyncTaskNotifier[T]) BlockAndGetResult ¶
func (n *AsyncTaskNotifier[T]) BlockAndGetResult() T
BlockAndGetResult returns the result of the async task.
func (*AsyncTaskNotifier[T]) BlockUntilFinish ¶
func (n *AsyncTaskNotifier[T]) BlockUntilFinish()
BlockUntilFinish blocks until the async task is finished.
func (*AsyncTaskNotifier[T]) Cancel ¶
func (n *AsyncTaskNotifier[T]) Cancel()
Cancel cancels the async task, the async task can receive the cancel signal from Context.
func (*AsyncTaskNotifier[T]) Context ¶
func (n *AsyncTaskNotifier[T]) Context() context.Context
Context returns the context of the async task.
func (*AsyncTaskNotifier[T]) Finish ¶
func (n *AsyncTaskNotifier[T]) Finish(result T)
Finish finishes the async task with a result.
func (*AsyncTaskNotifier[T]) FinishChan ¶
func (n *AsyncTaskNotifier[T]) FinishChan() <-chan struct{}
FinishChan returns a channel that will be closed when the async task is finished.
type ContextCond ¶
ContextCond is a condition variable implementation that can be used with context.
func NewContextCond ¶
func NewContextCond(l sync.Locker) *ContextCond
NewContextCond creates a new condition variable that can be used with context. Broadcast is implemented using a channel, so the performance may not be as good as sync.Cond.
func (*ContextCond) LockAndBroadcast ¶
func (cv *ContextCond) LockAndBroadcast()
LockAndBroadcast locks the underlying locker and performs a broadcast. It notifies all goroutines waiting on the condition variable.
c.LockAndBroadcast() ... make some change ... c.L.Unlock()
func (*ContextCond) Wait ¶
func (cv *ContextCond) Wait(ctx context.Context) error
Wait waits for a broadcast or context timeout. It blocks until either a broadcast is received or the context is canceled or times out. Returns an error if the context is canceled or times out.
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() c.L.Lock() for !condition() { if err := c.Wait(ctx); err != nil { return err } } ... make use of condition ... c.L.Unlock()
func (*ContextCond) WaitChan ¶
func (cv *ContextCond) WaitChan() <-chan struct{}
WaitChan returns a channel that can be used to wait for a broadcast. Should be called after Lock. The channel is closed when a broadcast is received.
type Future ¶
type Future[T any] struct { // contains filtered or unexported fields }
Future is a future value that can be set and retrieved.
func (*Future[T]) Done ¶
func (f *Future[T]) Done() <-chan struct{}
Done returns a channel that is closed when the future is set.
type VersionedListener ¶
type VersionedListener struct {
// contains filtered or unexported fields
}
VersionedListener is a listener with version.
func (*VersionedListener) Sync ¶
func (vl *VersionedListener) Sync()
Sync syncs the listener to the latest version.
func (*VersionedListener) Wait ¶
func (vl *VersionedListener) Wait(ctx context.Context) error
Wait waits for the next notification. If the context is canceled, it returns the error. Otherwise it will block until the next notification.
func (*VersionedListener) WaitChan ¶
func (vl *VersionedListener) WaitChan() <-chan struct{}
WaitChan returns a channel that will be closed when the next notification comes. Use Sync to sync the listener to the latest version to avoid redundant notify.
ch := vl.WaitChan() <-ch vl.Sync() ... make use of the notification ...
type VersionedNotifier ¶
type VersionedNotifier struct {
// contains filtered or unexported fields
}
VersionedNotifier is a notifier with version. A version-based notifier, any change of version could be seen by all listeners without lost.
func NewVersionedNotifier ¶
func NewVersionedNotifier() *VersionedNotifier
NewVersionedNotifier creates a new VersionedNotifier.
func (*VersionedNotifier) Listen ¶
func (vn *VersionedNotifier) Listen(at versionedListenAt) *VersionedListener
Listen creates a listener at given position.
func (*VersionedNotifier) NotifyAll ¶
func (vn *VersionedNotifier) NotifyAll()
NotifyAll notifies all listeners.