Documentation
¶
Overview ¶
Package caster implements a dead simple and performant message broadcaster (pubsub) library
Example ¶
/* * @Author: guiguan * @Date: 2019-09-19T23:52:54+10:00 * @Last modified by: guiguan * @Last modified time: 2019-09-20T10:26:20+10:00 */ package main import ( "fmt" "sync" "github.com/guiguan/caster" ) func main() { wg := new(sync.WaitGroup) c := caster.New(nil) // register subscribers for i := 0; i < 5; i++ { ch, _ := c.Sub(nil, 1) go receiveFromChannel(ch, wg) } // broadcast for i := 0; i < 100; i++ { c.Pub(i) } // close caster and all subscriber channels c.Close() wg.Wait() } func receiveFromChannel(ch chan interface{}, wg *sync.WaitGroup) { wg.Add(1) counter := 0 for range ch { counter++ } fmt.Printf("received %v messages\n", counter) wg.Done() }
Output: received 100 messages received 100 messages received 100 messages received 100 messages received 100 messages
Index ¶
- type Caster
- func (c *Caster) Close() (ok bool)
- func (c *Caster) Done() <-chan struct{}
- func (c *Caster) Pub(msg interface{}) (ok bool)
- func (c *Caster) Sub(ctx context.Context, capacity uint) (sCh chan interface{}, ok bool)
- func (c *Caster) TryPub(msg interface{}) (ok bool)
- func (c *Caster) Unsub(subCh chan interface{}) (ok bool)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Caster ¶
type Caster struct {
// contains filtered or unexported fields
}
Caster represents a message broadcaster
func (*Caster) Close ¶
Close closes current caster and all subscriber channels. Ok value indicates whether the operation is performed or not. When current caster is closed, it stops receiving further operations and the operation won't be performed.
func (*Caster) Done ¶
func (c *Caster) Done() <-chan struct{}
Done returns a done channel that is closed when current caster is closed
func (*Caster) Pub ¶
Pub publishes the given message to current caster, so the caster in turn broadcasts the message to all subscriber channels. Ok value indicates whether the operation is performed or not. When current caster is closed, it stops receiving further operations and the operation won't be performed.
func (*Caster) Sub ¶
Sub subscribes to current caster and returns a new channel with the given buffer for the subscriber to receive the broadcasting message. When the given ctx is canceled, current caster will unsubscribe the subscriber channel and close it. Ok value indicates whether the operation is performed or not. When current caster is closed, it stops receiving further operations and the operation won't be performed. A closed receiver channel will be returned if ok is false.
func (*Caster) TryPub ¶
TryPub publishes the given message to current caster, so the caster in turn broadcasts the message to all subscriber channels without blocking on waiting for channels to be ready for receiving. Ok value indicates whether the operation is performed or not. When current caster is closed, it stops receiving further operations and the operation won't be performed.