Skip to content
A simple library to share workloads to a group of workers with one master at any time.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.circleci
examples/simple
.gitignore
LICENSE
README.md
go.mod
go.sum
kettle.go
kettle_test.go
redis.go

README.md

CircleCI

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.

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.

Notes

Check this blog post out for information on how this library is used in production.

You can’t perform that action at this time.