watcher

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2021 License: MIT Imports: 4 Imported by: 0

README

watcher

Watcher means to receive changing signals and delivery to observers those want to get changing notify of their subscribed topics. It looks like a fan-out pattern, receive one signal then broadcast it to one more client.

type ChangeNotify struct {
	CheckSum string
	Topic    string 
	Data     []byte
}

type Watcher interface {
	// tell watcher there is a client want to get notified while these topic changed. 
	Subscribe(ch chan<- ChangeNotify, topics ...string)

	// any changes would be send to Watcher.ChangeNotifyCh.
	ChangeNotifyCh() <-chan ChangeNotify
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ChangeType

type ChangeType uint8
const (
	ChangeType_KV ChangeType = iota + 1
	ChangeType_DIR
)

type IChange

type IChange interface {
	Topic() string
	Type() ChangeType
}

type IObserver

type IObserver interface {
	// Identity must keep unique in the server.
	Identity() string

	// Topics describes all topics those IObserver want to subscribe.
	Topics() []string

	// Inbound will receive all changes about topics.
	Inbound() chan<- IChange

	// Outbound will receive all changes about topics.
	Outbound() <-chan IChange

	// Close release observer resources, only be called IWatcher, importantly close channel.
	Close()
}

IObserver describes all actions those the IWatcher's client should have.

type IWatcher

type IWatcher interface {
	// Subscribe tell watcher there is a client want to get notified while topics are changed.
	Subscribe(obs ...IObserver)

	// Unsubscribe how to unsubscribe safely?
	Unsubscribe(obs IObserver)

	// ChangeNotify any changes would be sent to Watcher.Inbound.
	ChangeNotify(notify IChange)
}

IWatcher provides Subscribe(obs ...IObserver) and Unsubscribe(obs IObserver) for observers, and ChangeNotify(notify IChange) for producer.

func NewChannelWatcher

func NewChannelWatcher(bufferSize int) IWatcher

NewChannelWatcher construct a IWatcher at first call, if there is a IWatcher instance already, that instance would be returned at once.

Jump to

Keyboard shortcuts

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