inbox

package
v0.18.0-rc.9 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2024 License: Apache-2.0 Imports: 3 Imported by: 0

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

func New[M any]() *Inbox[M]

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:

  1. Calling [Pop], which will return one message at a time.
  2. 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

func (i *Inbox[M]) BlockingPop() (item M, ok bool, closed error)

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]) Close

func (i *Inbox[M]) Close()

func (*Inbox[M]) Enqueue

func (i *Inbox[M]) Enqueue(msg M) bool

Add a message to the end of the k

func (*Inbox[M]) Iter

func (i *Inbox[M]) Iter() iter.Seq2[M, bool]

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.

func (*Inbox[M]) Pop

func (i *Inbox[M]) Pop() (item M, ok bool, closed error)

get and remove a value from the inbox. This is safe to call from multiple go routines. if there was no item returned, [ok] returns false if the inbox is closed and will never return a value, [closed] will be not nil

func (*Inbox[M]) Size

func (i *Inbox[M]) Size() int

Return the number of items in the Inbox

func (*Inbox[M]) Sync

func (i *Inbox[M]) Sync() *sync.Cond

Jump to

Keyboard shortcuts

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