github.com/choria-io/go-choria@v0.28.1-0.20240416190746-b3bf9c7d5a45/providers/election/streams/options.go (about) 1 // Copyright (c) 2021-2022, R.I. Pienaar and the Choria Project contributors 2 // 3 // SPDX-License-Identifier: Apache-2.0 4 5 package election 6 7 import ( 8 "time" 9 10 "github.com/nats-io/nats.go" 11 ) 12 13 // Option configures the election system 14 type Option func(o *options) 15 16 type options struct { 17 name string 18 key string 19 bucket nats.KeyValue 20 ttl time.Duration 21 cInterval time.Duration 22 wonCb func() 23 lostCb func() 24 campaignCb func(s State) 25 bo Backoff 26 debug func(format string, a ...any) 27 } 28 29 // WithBackoff will use the provided Backoff timer source to decrease campaign intervals over time 30 func WithBackoff(bo Backoff) Option { 31 return func(o *options) { o.bo = bo } 32 } 33 34 // OnWon is a callback called when winning an election 35 func OnWon(cb func()) Option { 36 return func(o *options) { o.wonCb = cb } 37 } 38 39 // OnLost is a callback called when losing an election 40 func OnLost(cb func()) Option { 41 return func(o *options) { o.lostCb = cb } 42 } 43 44 // OnCampaign is called each time a campaign is done by the leader or a candidate 45 func OnCampaign(cb func(s State)) Option { 46 return func(o *options) { o.campaignCb = cb } 47 } 48 49 // WithDebug sets a function to do debug logging with 50 func WithDebug(cb func(format string, a ...any)) Option { 51 return func(o *options) { o.debug = cb } 52 }