go-elect

module
v0.0.0-...-2ff72f1 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2024 License: Apache-2.0

README

go-elect

go-elect is a library for making the election process easier. It provides a simple interface for managing your distributed exclusive job execution, and it supports multiple backends for storing the election state:

And more to come...

How to use

Redis

package main

import (
    "github.com/redis/go-redis/v9"
    "github.com/git-hulk/go-elect/elector/engine/store"
    "github.com/git-hulk/go-elect/elector"
)

type CountRunner struct {
    count atomic.Int32
}

func (r *CountRunner) Run(_ context.Context) error {
    r.count.Inc()
    time.Sleep(100 * time.Millisecond)
    return nil
}

func main() {
    ctx := context.Background()
    redisClient := redis.NewClient(&redis.Options{Addr: "localhost:6379"})
    redisStore := store.NewRedisStore(redisClient)
	
    elector, err := elector.New(redisStore, "test-elector1-key", 3 * time.Second,  &CountRunner{})
    if err := elector.Run(ctx);  err != nil {
        // handle error
    }
    elector.Wait()
    // use elector.Release() to release the election
}

License

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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