kwpoll

package
v0.8.3 Latest Latest
Warning

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

Go to latest
Published: May 13, 2020 License: MIT Imports: 7 Imported by: 4

README

Poll Watcher

Poll Watcher is a konfig.Watcher that sends events every x time given in the konfig.

Usage

import (
	"fmt"
	"os"
	"time"

	"github.com/lalamove/konfig"
	"github.com/lalamove/konfig/loader/klenv"
	"github.com/lalamove/konfig/watcher/kwpoll"
)

func main() {
	os.Setenv("foo", "bar")

	var l = klenv.New(&klenv.Config{
		Vars: []string{
			"foo",
		},
	})

	var v = konfig.Values{}
	l.Load(v)

	var w = kwpoll.New(&kwpoll.Config{
		Rater:     kwpoll.Time(100 * time.Millisecond),
		Loader:    l,
		Diff:      true,
		Debug:     true,
		InitValue: v,
	})
	w.Start()

	var timer = time.NewTimer(200 * time.Millisecond)
	var watched int

	os.Setenv("foo", "baz") // change the value

main:
	for {
		select {
		case <-timer.C:
			w.Close()
			break main
		case <-w.Watch():
			watched++
		}
	}

	fmt.Println(watched) // will output 1
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (

	// ErrNoLoader is the error returned when no Loader is set and Diff is set to true
	ErrNoLoader = errors.New("You must give a non nil Loader to the poll diff watcher")
	// ErrAlreadyClosed is the error returned when trying to close an already closed PollDiffWatcher
	ErrAlreadyClosed = errors.New("PollDiffWatcher already closed")
	// ErrNoWatcherSupplied is the error returned when Watch in general config is false but a watcher is still being registered
	ErrNoWatcherSupplied = errors.New("watcher has to be supplied when registering a watcher")
)

Functions

This section is empty.

Types

type Config

type Config struct {
	// Rater is the rater the PollWatcher calls to get the duration until the next tick
	Rater Rater
	// Debug sets the debug mode
	Debug bool
	// Logger is the logger used to log debug messages
	Logger nlogger.Provider
	// Diff tells whether we should check for diffs
	// If diff is set, a Getter is required
	Diff bool
	// Loader is a loader to fetch data to check diff
	Loader konfig.Loader
	// InitValue is the initial value to compare with whe Diff is true
	InitValue konfig.Values
}

Config is the config of a PollWatcher

type PollWatcher

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

PollWatcher is a konfig.Watcher that sends events every x time given in the konfig.

func New

func New(cfg *Config) *PollWatcher

New creates a new PollWatcher fromt the given config

func (*PollWatcher) Close

func (t *PollWatcher) Close() error

Close closes the PollWatcher

func (*PollWatcher) Done

func (t *PollWatcher) Done() <-chan struct{}

Done indicates whether the watcher is done or not

func (*PollWatcher) Err

func (t *PollWatcher) Err() error

Err returns the poll watcher error

func (*PollWatcher) Start

func (t *PollWatcher) Start() error

Start starts the ticker watcher

func (*PollWatcher) Watch

func (t *PollWatcher) Watch() <-chan struct{}

Watch returns the channel to which events are written

type Rater

type Rater interface {
	Time() time.Duration
}

Rater is an interface that exposes a single Time method which returns the time until the next tick

type Time

type Time time.Duration

Time is a time.Duration which implements the Rater interface

func (Time) Time

func (t Time) Time() time.Duration

Time returns the time.Duration

Jump to

Keyboard shortcuts

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