kettle

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2023 License: MIT Imports: 11 Imported by: 0

README

Go Go Reference

Overview

kettle is a simple library that abstracts the use of distributed locking to elect a master among group of workers at a specified time interval. The elected master will then call the "master" function. This library uses Redis as the default distributed locker.

How it works

All workers that share the same name will attempt to grab a Redis lock to become the master. A provided master function will be executed by the node that successfully grabbed the lock. A single node works as well, in which case, that node will run both as master and a worker.

The main changes in v2.x.x is the use of context for termination and an optional 'done' channel for notification. It looks something like this:

name := "kettle-example"
k, _ := kettle.New(kettle.WithName(name), kettle.WithVerbose(true))
in := kettle.StartInput{
    // Our master callback function.
    Master: func(v interface{}) error {
        kt := v.(*kettle.Kettle)
        log.Println("from master, name:", kt.Name())
        return nil
    },
    MasterCtx: k, // arbitrary data that is passed to master function
}

ctx, cancel := context.WithCancel(context.TODO())
done := make(chan error, 1)
err = k.Start(ctx, &in, done)
_ = err

// Simulate work
time.Sleep(time.Second * 5)
cancel() // terminate
<-done   // wait

For version 0.x.x, it looks something like this:

name := "kettle-example"
k, _ := kettle.New(kettle.WithName(name), kettle.WithVerbose(true))
in := kettle.StartInput{
    // Our master callback function.
    Master: func(v interface{}) error {
        kt := v.(*kettle.Kettle)
        log.Println("from master, name:", kt.Name())
        return nil
    },
    MasterCtx: k, // arbitrary data that is passed to master function
    Quit:      make(chan error),
    Done:      make(chan error),
}

err = k.Start(&in)
_ = err

// Simulate work
time.Sleep(time.Second * 5)
in.Quit <- nil // terminate
<-in.Done      // wait

Environment variables

# Required
REDIS_HOST=1.2.3.4:6379

# Optional
REDIS_PASSWORD=***
REDIS_TIMEOUT_SECONDS=5

Example

A simple example is provided here for reference. Try running it simultaneously on multiple nodes. For the version 0.x.x example, check it out here.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewRedisPool

func NewRedisPool() (*redis.Pool, error)

Types

type DistLocker

type DistLocker interface {
	Lock() error
	Unlock() (bool, error)
}

DistLocker abstracts a distributed locker.

type Kettle

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

Kettle provides functions that abstract the master election of a group of workers at a given interval time.

func New

func New(opts ...KettleOption) (*Kettle, error)

New returns an instance of Kettle.

func (Kettle) IsMaster added in v0.0.5

func (k Kettle) IsMaster() bool

IsMaster returns master status.

func (Kettle) IsVerbose

func (k Kettle) IsVerbose() bool

IsVerbose returns the verbosity setting.

func (Kettle) Name

func (k Kettle) Name() string

Name returns the instance's name.

func (Kettle) Pool

func (k Kettle) Pool() *redis.Pool

Pool returns the configured Redis connection pool.

func (*Kettle) Start

func (k *Kettle) Start(in *StartInput) error

Start starts Kettle's main function.

type KettleOption

type KettleOption interface {
	Apply(*Kettle)
}

KettleOption configures Kettle.

func WithDistLocker

func WithDistLocker(v DistLocker) KettleOption

WithDistLocker configures a Kettle instance's DistLocker.

func WithName

func WithName(v string) KettleOption

WithName configures Kettle instance's name.

func WithTickTime

func WithTickTime(v int64) KettleOption

WithTickTime configures a Kettle instance's tick timer in seconds.

func WithVerbose

func WithVerbose(v bool) KettleOption

WithVerbose configures a Kettle instance's log verbosity.

type StartInput

type StartInput struct {
	Master    func(ctx interface{}) error // function to call every time we are master
	MasterCtx interface{}                 // callback function parameter
	Quit      chan error                  // signal for us to terminate
	Done      chan error                  // report that we are done
}

StartInput configures the Start function.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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