queue

package module
v0.0.0-...-090010c Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2022 License: MIT Imports: 2 Imported by: 0

README

queue

A basic event queue (and publisher/subscriber) in go, now supporting Go v1.18+ Generics in github.com/jimjibone/queue/v2.

See the v2 readme for a slightly faster and type-safe option.

Installation

go get github.com/jimjibone/queue

Queue Usage

Queue is a channel-based FIFO queue. Similar to a Go channel, items can be pushed to the back of the Queue and then popped off the front by listening on the Pop channel. This structure differs from channels in that its buffer is effectively endless.

q := queue.New()
defer q.Close()

q.Push("item")
out := <-q.Pop()
fmt.Printf("received: %v\n", out)

Pub-Sub Usage

Pub is a channel-based broadcast FIFO queue. Built on top of Queue, the Pub sends published messages to all active Subs created via the NewSub method.

pub := queue.NewPub()
defer pub.Close()

sub1 := pub.NewSub()
sub2 := pub.NewSub()
defer sub1.Close()
defer sub2.Close()

pub.Pub("item")
out1 := <-sub1.Sub()
fmt.Printf("sub1 received: %v\n", out1)

out2 := <-sub2.Sub()
fmt.Printf("sub2 received: %v\n", out2)

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Pub

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

Pub is a channel-based broadcast FIFO queue. Built on top of Queue, the Pub sends published messages to all active Subs created via the NewSub method.

Example
package main

import (
	"fmt"

	"github.com/jimjibone/queue"
)

func main() {
	pub := queue.NewPub()
	defer pub.Close()

	sub1 := pub.NewSub()
	sub2 := pub.NewSub()
	defer sub1.Close()
	defer sub2.Close()

	pub.Pub("item")
	out1 := <-sub1.Sub()
	fmt.Printf("sub1 received: %v\n", out1)
	out2 := <-sub2.Sub()
	fmt.Printf("sub2 received: %v\n", out2)

}
Output:

sub1 received: item
sub2 received: item

func NewPub

func NewPub() *Pub

NewPub returns a new Pub. Remember to call Close on the Pub once you're finished with it.

func (*Pub) Close

func (p *Pub) Close()

Close the Pub.

func (*Pub) NewSub

func (p *Pub) NewSub() *Sub

NewSub adds a new Sub to the Pub and returns it. Remember to call Close on the Sub when finished.

func (*Pub) Pub

func (p *Pub) Pub(item interface{})

Pub publishes the new item to subscribers.

type Queue

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

Queue is a channel-based FIFO queue. Similar to a Go channel, items can be pushed to the back of the Queue and then popped off the front by listening on the Pop channel. This structure differs from channels in that its buffer is effectively endless.

Example
package main

import (
	"fmt"

	"github.com/jimjibone/queue"
)

func main() {
	q := queue.New()
	defer q.Close()

	q.Push("item")
	out := <-q.Pop()
	fmt.Printf("received: %v\n", out)

}
Output:

received: item

func New

func New() *Queue

New returns a new, running, Queue. Remember to call Close on the Queue once you're finished with it.

func (*Queue) Close

func (q *Queue) Close()

Close the Queue.

func (*Queue) Flush

func (q *Queue) Flush()

Flush empties the Queue.

func (*Queue) Pop

func (q *Queue) Pop() <-chan interface{}

Pop an item from the front of the Queue.

func (*Queue) Push

func (q *Queue) Push(item interface{})

Push an item onto the back of the Queue.

type Sub

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

Sub is a channel-based FIFO queue, receiving messages from its parent Pub.

func (*Sub) Close

func (s *Sub) Close()

Close the Sub.

func (*Sub) Sub

func (s *Sub) Sub() <-chan interface{}

Sub receives the next published item.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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