1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| package main
import ( "context" "flag" "fmt" "os" "time"
"github.com/CodeNinja917/leaderelection" "github.com/google/uuid" "github.com/redis/go-redis/v9" )
func main() { var ( redisAddr string lockName string id string ) flag.StringVar(&redisAddr, "redis-addr", "localhost:6379", "redis addr") flag.StringVar(&lockName, "lock-name", "", "the lease lock resource name") flag.StringVar(&id, "id", uuid.New().String(), "the holder identify name") flag.Parse()
cfg := leaderelection.LeaderElectionConfig{ RedisConfig: redis.Options{Addr: redisAddr}, Callbacks: leaderelection.LeaderCallbacks{ OnStartedLeading: func(ctx context.Context) { for { select { case <-ctx.Done(): return default: time.Sleep(10 * time.Second) fmt.Println("Controller loop...") } } }, OnStoppedLeading: func(ctx context.Context) { fmt.Printf("leader lost: %s\n", id) os.Exit(0) }, OnNewLeader: func(identity string) { if identity == id { return } fmt.Printf("new leader elected: %s\n", identity) }, }, ReleaseOnCancel: true, Identity: id, Key: lockName, } ctx := context.Background() le, err := leaderelection.NewLeaderElector(ctx, cfg) if err != nil { fmt.Printf("ERROR: %v\n", err) return } le.Run(ctx) }
|