watcher

package module
v0.0.0-...-b6fc67c Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2022 License: MIT Imports: 6 Imported by: 0

README

Watcher

简单的监听和加载工具,帮助对依赖复杂的模块进行解耦。

Quick Start

// a.go
// A模块每隔一段时间拉取一次值
updateInterval := time.Minute // 更新间隔
notifier := watcher.NewTickNotifier(updateInterval) // tick触发器
loaderA := watcher.NewLoader(ctx, notifier, watcher.WithTransformer(func(ctx context.Context, v interface{}) interface{} {
    return serviceA.GetSomething() // 执行拉取函数
}))
loaderA.Start(ctx) // 启动加载器

// b.go
// B模块不关心A模块的值什么时候更新,直接使用即可
func AnyWhere() {
    v := loaderA.Get() // 取到A模块的值
    // do something with v
}

// c.go
// C模块监听A模块的值更新
watcher.WatchLoader(loaderA, func(ctx context.Context, v interface{}) {
    fmt.Println(v) // 每次A模块更新值时进行打印
})

Usage

Watcher

每隔一段时间执行一次指定函数:

// 每秒打印一次
notifier := watcher.NewTickNotifier(time.Second)
watcher.Watch(ctx, notifier, func(ctx context.Context, v interface{}) {
    fmt.Println("hello world")
})

// Output:
// hello world

自定义Notifier

ch := make(chan int)
watcher.Watch(ctx, watcher.NewNotifier(ch), func(ctx context.Context, v interface{}) {
    fmt.Println(v)
})
ch <- 1
ch <- 2

// Output:
// 1
// 2

控制启动停止

notifier := watcher.NewTickNotifier(time.Second)
watcher.NewWatcher(notifier, func(ctx context.Context, v interface{}) {
    fmt.Println("hello world")
})

watcher.Start(ctx)
watcher.Stop()
Loader

自动加载器:

ch := make(chan int)
v := watcher.AutoLoad(ctx, watcher.NewNotifier(ch))

fmt.Println(v.Load())

ch <- 1
fmt.Println(v.Load()) // 更新延迟,也许打印出的并不是最新值

// Output:
// nil
// 1

加载器可以添加转换函数:

notifier := watcher.NewTickNotifier(time.Second)
v := watcher.AutoLoad(ctx, notifier, watcher.WithTransformer(func(ctx context.Context, v interface{}) interface{} {
    // TickNotifier 每次触发会送一个 time.Time 过来
    return v.(time.Time).Unix()
}))

time.Sleep(time.Second)
fmt.Println(v.Load())

time.Sleep(time.Second)
fmt.Println(v.Load())

可以监听 Loader 更新:

ch := make(chan int)
loader := watcher.NewLoader(watcher.NewNotifier(ch))
watcher.WatchLoader(loader, func(ctx context.Context, v interface{}) {
    fmt.Println(v)
})

ch <- 1
ch <- 2

// Output:
// 1
// 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AutoLoad

func AutoLoad(ctx context.Context, n Notifier, opts ...LoaderOption) *atomic.Value

AutoLoad updates returned atomic value every time n triggers

func Watch

func Watch(ctx context.Context, n Notifier, f Executer)

Watch executes f every time n triggers

func WatchLoader

func WatchLoader(l *Loader, f Executer)

WatchLoader executes f every time l updates

Types

type Executer

type Executer = func(context.Context, interface{})

type Loader

type Loader struct {
	*Watcher
	// contains filtered or unexported fields
}

func NewLoader

func NewLoader(n Notifier, opts ...LoaderOption) *Loader

func (*Loader) Get

func (l *Loader) Get() interface{}

type LoaderOption

type LoaderOption func(*Loader)

func WithTransformer

func WithTransformer(t Transformer) LoaderOption

type Logger

type Logger interface {
	Info(format string, args ...interface{})
	Error(format string, args ...interface{})
}

type Notifier

type Notifier <-chan interface{}

func NewNotifier

func NewNotifier(ch interface{}) Notifier

NewNotifier takes any readable channel type (chan or <-chan but not chan<-) and exposes it as a Notifier

func NewTickNotifier

func NewTickNotifier(interval time.Duration) Notifier

type Transformer

type Transformer = func(context.Context, interface{}) interface{}

type Watcher

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

func NewWatcher

func NewWatcher(n Notifier, f Executer, opts ...WatcherOption) *Watcher

func (*Watcher) Execute

func (w *Watcher) Execute(ctx context.Context, v interface{})

func (*Watcher) GetNotifier

func (w *Watcher) GetNotifier() Notifier

func (*Watcher) Start

func (w *Watcher) Start(ctx context.Context)

func (*Watcher) Stop

func (w *Watcher) Stop()

type WatcherOption

type WatcherOption func(*Watcher)

func WithLogger

func WithLogger(logger Logger) WatcherOption

Jump to

Keyboard shortcuts

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