Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Inbox ¶
type Inbox[M any] struct { // contains filtered or unexported fields }
func New ¶
Create an Inbox that will store messages of type [M]. An Inbox is written to using the `Enqueue` method, which will append itself to the end of a message queue. To read these messages, there are two methods:
- Calling [Pop], which will return one message at a time.
- Call [Receive], which will return a channel that will receive a message one at a time. This method is useful if you expect to call [Pop] in a loop or if you want to have multiple consumers of the inbox and order is not important (a version of the competeing consumers pattern).
func (*Inbox[M]) BlockingPop ¶
Similar to [Pop], but this call will block until it has a value to retrieve. This is safe to call from multiple go routines, but keep in mind that [item] may be empty in that case, so always ensure that you actually got a value by checking [ok].
If the inbox is closed, [closed] will be non-nil and the caller can expect no more messages. Under the hood, this is using sync.Cond to sleep callers until there are messages.
func (*Inbox[M]) Iter ¶
this is an Iterator function that can be used with a range loop like so:
for item, ok := range ibox.Iter() { if !ok { log.Println("someone beat us to it, try again") continue } log.Printf("got value: %d", item) sig <- item }
The loop will exit when the inbox is closed. Note that you need to check [ok] still to ensure that you actually got a value, since multiple go routines may be reading off this same machine.