messenger

package module
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2019 License: MIT Imports: 1 Imported by: 9

README

Messenger

Simple broadcasting mechanism using go channels

Build Status codecov GoDoc Go Report Card Donate

What is this

This is a simple broadcasting mechanism where gouroutines can subscribe and unsubscribe to recieve messages from the Messenger instance.

Channels can be buffered to reduce blocking and messages can be dropped if channel's buffer is full. These are configurable options.

Why

Because i needed this for some silly stuff and ofcourse, why not!

Documentation

Overview

Package messenger provides a simple broadcasting mechanism

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Messenger

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

Messenger instance. Must be invoked with New().

func New

func New(buffer uint, drop bool) *Messenger

New creates a new Messenger instance.

Buffer sets the buffer size to client channels, set to 0 if you want unbuffered behaviour.

Drop makes broadcasts to drop if a client's buffer is full. Ignored with 0 buffer size.

Example
package main

import (
	"fmt"
	"log"
	"sync"
	"time"

	"github.com/ugjka/messenger"
)

func main() {
	log.SetFlags(log.Lmicroseconds)
	log.SetPrefix("time ")
	m := messenger.New(0, false)
	wg := &sync.WaitGroup{}
	for i := 1; i <= 5; i++ {
		wg.Add(1)
		go func(i int, m *messenger.Messenger) {
			defer wg.Done()
			time.Sleep(time.Millisecond * time.Duration(i*20))
			client, err := m.Sub()
			if err != nil {
				log.Printf("Client %d: %v\n", i, err)
				return
			}
			log.Printf("Client %d subscribed\n", i)
			timeout := time.After(time.Millisecond * time.Duration(i*100))
			for {
				select {
				case msg := <-client:
					log.Printf("Client %d got message: %s\n", i, msg)
				case <-timeout:
					m.Unsub(client)
					log.Printf("Client %d unsubscribed\n", i)
					return
				}
			}
		}(i, m)
	}
	for i := 0; i < 10; i++ {
		time.Sleep(time.Millisecond * 50)
		m.Broadcast(fmt.Sprintf("nr.%d", i))
	}
	wg.Wait()
}
Output:

func (*Messenger) Broadcast

func (m *Messenger) Broadcast(msg interface{})

Broadcast broadcasts a message to all current clients. If a client is not listening and drop is not set this will block.

func (*Messenger) Kill

func (m *Messenger) Kill()

Kill closes and removes all clients and stops the monitor() goroutine of Messenger, making the Messenger instance unusable. Kill should only be called when all clients have exited.

func (*Messenger) Len added in v1.1.1

func (m *Messenger) Len() int

Len gets the subscriber count

func (*Messenger) Reset

func (m *Messenger) Reset()

Reset closes and removes all clients.

func (*Messenger) Sub

func (m *Messenger) Sub() (client chan interface{}, err error)

Sub returns a new client. Clients can block Broadcast() unless drop is set. Clients should check whether the channel is closed or not. Returns an error if its Messenger instance is Killed. You can ignore the err if you never intend to use Kill()

func (*Messenger) Unsub

func (m *Messenger) Unsub(client chan interface{})

Unsub unsubscribes a client and closes its channel.

Jump to

Keyboard shortcuts

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