pubsub

package
v1.67.3 Latest Latest
Warning

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

Go to latest
Published: May 27, 2022 License: BSD-3-Clause Imports: 0 Imported by: 5

Documentation

Overview

Package pubsub implements a simple multi-topic pub-sub library.

Topics must be strings and messages of any type can be published. A topic can have any number of subcribers and all of them receive messages published on the topic.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type PubSub

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

PubSub is a collection of topics.

func NewPubSub

func NewPubSub(capacity int) *PubSub

NewPubSub New creates a new PubSub and starts a goroutine for handling operations. The capacity of the channels created by Sub and SubOnce will be as specified.

func (*PubSub) AddSub

func (ps *PubSub) AddSub(ch chan interface{}, topics ...string)

AddSub adds subscriptions to an existing channel.

func (*PubSub) Close

func (ps *PubSub) Close(topics ...string)

Close closes all channels currently subscribed to the specified topics. If a channel is subscribed to multiple topics, some of which is not specified, it is not closed.

func (*PubSub) FIFOPub

func (ps *PubSub) FIFOPub(msg interface{}, topics ...string)

FIFOPub 发布交易:如果channel满了,那么把最旧的数据删除,并写入msg

func (*PubSub) Pub

func (ps *PubSub) Pub(msg interface{}, topics ...string)

Pub publishes the given message to all subscribers of the specified topics.

func (*PubSub) Shutdown

func (ps *PubSub) Shutdown()

Shutdown closes all subscribed channels and terminates the goroutine.

func (*PubSub) Sub

func (ps *PubSub) Sub(topics ...string) chan interface{}

Sub returns a channel on which messages published on any of the specified topics can be received.

func (*PubSub) SubOnce

func (ps *PubSub) SubOnce(topics ...string) chan interface{}

SubOnce is similar to Sub, but only the first message published, after subscription, on any of the specified topics can be received.

func (*PubSub) TryPub

func (ps *PubSub) TryPub(msg interface{}, topics ...string)

TryPub publishes the given message to all subscribers of the specified topics if the topic has buffer space.

func (*PubSub) Unsub

func (ps *PubSub) Unsub(ch chan interface{}, topics ...string)

Unsub unsubscribes the given channel from the specified topics. If no topic is specified, it is unsubscribed from all topics.

Jump to

Keyboard shortcuts

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