event

package
v0.0.0-...-e1aa8c7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 1, 2020 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AbsTime

type AbsTime time.Duration

AbsTime represents absolute monotonic time.

func Now

func Now() AbsTime

Now returns the current absolute monotonic time.

func (AbsTime) Add

func (t AbsTime) Add(d time.Duration) AbsTime

Add returns t + d as absolute time.

func (AbsTime) Sub

func (t AbsTime) Sub(t2 AbsTime) time.Duration

Sub returns t - t2 as a duration.

type ChanTimer

type ChanTimer interface {
	Timer

	// The channel returned by C receives a value when the timer expires.
	C() <-chan AbsTime
	// Reset reschedules the timer with a new timeout.
	// It should be invoked only on stopped or expired timers with drained channels.
	Reset(time.Duration)
}

ChanTimer is a cancellable event created by NewTimer.

type Clock

type Clock interface {
	Now() AbsTime
	Sleep(time.Duration)
	NewTimer(time.Duration) ChanTimer
	After(time.Duration) <-chan AbsTime
	AfterFunc(d time.Duration, f func()) Timer
}

The Clock interface makes it possible to replace the monotonic system clock with a simulated clock.

type Feed

type Feed struct {
	// contains filtered or unexported fields
}

Feed implements one-to-many subscriptions where the carrier of events is a channel. Values sent to a Feed are delivered to all subscribed channels simultaneously.

Feeds can only be used with a single type. The type is determined by the first Send or Subscribe operation. Subsequent calls to these methods panic if the type does not match.

The zero value is ready to use.

func (*Feed) Send

func (f *Feed) Send(value interface{}) (nsent int)

Send delivers to all subscribed channels simultaneously. It returns the number of subscribers that the value was sent to.

func (*Feed) Subscribe

func (f *Feed) Subscribe(channel interface{}) Subscription

Subscribe adds a channel to the feed. Future sends will be delivered on the channel until the subscription is canceled. All channels added must have the same element type.

The channel should have ample buffer space to avoid blocking other subscribers. Slow subscribers are not dropped.

type ResubscribeFunc

type ResubscribeFunc func(context.Context) (Subscription, error)

A ResubscribeFunc attempts to establish a subscription.

type Subscription

type Subscription interface {
	Err() <-chan error // returns the error channel
	Unsubscribe()      // cancels sending of events, closing the error channel
}

Subscription represents a stream of events. The carrier of the events is typically a channel, but isn't part of the interface.

Subscriptions can fail while established. Failures are reported through an error channel. It receives a value if there is an issue with the subscription (e.g. the network connection delivering the events has been closed). Only one value will ever be sent.

The error channel is closed when the subscription ends successfully (i.e. when the source of events is closed). It is also closed when Unsubscribe is called.

The Unsubscribe method cancels the sending of events. You must call Unsubscribe in all cases to ensure that resources related to the subscription are released. It can be called any number of times.

func NewSubscription

func NewSubscription(producer func(<-chan struct{}) error) Subscription

NewSubscription runs a producer function as a subscription in a new goroutine. The channel given to the producer is closed when Unsubscribe is called. If fn returns an error, it is sent on the subscription's error channel.

func Resubscribe

func Resubscribe(backoffMax time.Duration, fn ResubscribeFunc) Subscription

Resubscribe calls fn repeatedly to keep a subscription established. When the subscription is established, Resubscribe waits for it to fail and calls fn again. This process repeats until Unsubscribe is called or the active subscription ends successfully.

Resubscribe applies backoff between calls to fn. The time between calls is adapted based on the error rate, but will never exceed backoffMax.

type SubscriptionScope

type SubscriptionScope struct {
	// contains filtered or unexported fields
}

SubscriptionScope provides a facility to unsubscribe multiple subscriptions at once.

For code that handle more than one subscription, a scope can be used to conveniently unsubscribe all of them with a single call. The example demonstrates a typical use in a larger program.

The zero value is ready to use.

func (*SubscriptionScope) Close

func (sc *SubscriptionScope) Close()

Close calls Unsubscribe on all tracked subscriptions and prevents further additions to the tracked set. Calls to Track after Close return nil.

func (*SubscriptionScope) Count

func (sc *SubscriptionScope) Count() int

Count returns the number of tracked subscriptions. It is meant to be used for debugging.

func (*SubscriptionScope) Track

Track starts tracking a subscription. If the scope is closed, Track returns nil. The returned subscription is a wrapper. Unsubscribing the wrapper removes it from the scope.

type System

type System struct{}

System implements Clock using the system clock.

func (System) After

func (c System) After(d time.Duration) <-chan AbsTime

After returns a channel which receives the current time after d has elapsed.

func (System) AfterFunc

func (c System) AfterFunc(d time.Duration, f func()) Timer

AfterFunc runs f on a new goroutine after the duration has elapsed.

func (System) NewTimer

func (c System) NewTimer(d time.Duration) ChanTimer

NewTimer creates a timer which can be rescheduled.

func (System) Now

func (c System) Now() AbsTime

Now returns the current monotonic time.

func (System) Sleep

func (c System) Sleep(d time.Duration)

Sleep blocks for the given duration.

type Timer

type Timer interface {
	// Stop cancels the timer. It returns false if the timer has already
	// expired or been stopped.
	Stop() bool
}

Timer is a cancellable event created by AfterFunc.

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL