diff options
| author | Paul Buetow <paul@buetow.org> | 2023-06-14 09:52:50 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2023-06-14 09:52:50 +0300 |
| commit | 8259f1a764e0c3543c6770dc91433d85fb2e96db (patch) | |
| tree | beba47b6be4531fc6966b694bd4dfd1dcb901719 | |
| parent | 93b32777632eb79393e4f6fe0b752665c8e511ff (diff) | |
make loop interval configurable
| -rw-r--r-- | cmd/gorum/main.go | 3 | ||||
| -rw-r--r-- | internal/config/config.go | 13 | ||||
| -rw-r--r-- | internal/quorum/quorum.go | 10 | ||||
| -rw-r--r-- | internal/run.go | 5 | ||||
| -rw-r--r-- | internal/server/tcpserver.go | 2 |
5 files changed, 26 insertions, 7 deletions
diff --git a/cmd/gorum/main.go b/cmd/gorum/main.go index da6574c..5c4a116 100644 --- a/cmd/gorum/main.go +++ b/cmd/gorum/main.go @@ -16,6 +16,7 @@ const versionStr = "v0.0.0" func main() { configFile := flag.String("cfg", "/etc/gorum.json", "The config file") + loopIntervalS := flag.Int64("loopIntervalOverride", 0, "Overriding the loop interval, if > 0") version := flag.Bool("version", false, "Display version") flag.Parse() @@ -28,7 +29,7 @@ func main() { ctx, cancel := contextWithSignal() defer cancel() - if err := internal.Run(ctx, *configFile); err != nil { + if err := internal.Run(ctx, *configFile, *loopIntervalS); err != nil { log.Fatal(err) } diff --git a/internal/config/config.go b/internal/config/config.go index c404284..90435e5 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -12,9 +12,10 @@ import ( ) type Config struct { - StateDir string - Address string - Nodes []string + StateDir string + Address string + Nodes []string + LoopIntervalS int64 `json:"LoopIntervalS,omitempty"` } func New(configFile string) (Config, error) { @@ -36,6 +37,12 @@ func New(configFile string) (Config, error) { return c, err } + if c.LoopIntervalS == 0 { + c.LoopIntervalS = 10 + } + + log.Println("config: using loop interval of", c.LoopIntervalS) + return c, nil } diff --git a/internal/quorum/quorum.go b/internal/quorum/quorum.go index 52cf6ee..dfeabe6 100644 --- a/internal/quorum/quorum.go +++ b/internal/quorum/quorum.go @@ -36,17 +36,25 @@ func New(conf config.Config) Quorum { func (quo Quorum) Start(ctx context.Context) <-chan vote.Vote { ch := make(chan vote.Vote) + interval := time.Second * time.Duration(quo.conf.LoopIntervalS) + if vote.Expiry <= interval { + log.Fatal("quorum: LoopIntervalS ", quo.conf.LoopIntervalS, + " should be less than the vote expiry of ", vote.Expiry) + } + go func() { defer close(ch) for { select { - case <-time.After(vote.Expiry): + case <-time.After(interval): myVote, _ := quo.makeMyVote() + log.Println("quorum: made my vote:", myVote) ch <- myVote case v := <-quo.voteCh: quo.vote(v) if myVote, changed := quo.makeMyVote(); changed { + log.Println("quorum: changed my vote:", myVote) ch <- myVote } quo.score() diff --git a/internal/run.go b/internal/run.go index 15c661f..961df14 100644 --- a/internal/run.go +++ b/internal/run.go @@ -9,11 +9,14 @@ import ( "codeberg.org/snonux/gorum/internal/server" ) -func Run(ctx context.Context, configFile string) error { +func Run(ctx context.Context, configFile string, loopIntervalS int64) error { conf, err := config.New(configFile) if err != nil { return err } + if loopIntervalS > 0 { + conf.LoopIntervalS = loopIntervalS + } quo := quorum.New(conf) myVoteCh := quo.Start(ctx) diff --git a/internal/server/tcpserver.go b/internal/server/tcpserver.go index 47d47ca..2eb2422 100644 --- a/internal/server/tcpserver.go +++ b/internal/server/tcpserver.go @@ -19,7 +19,7 @@ func tcpServerRun(ctx context.Context, conf config.Config, cb handlerCb) error { } defer listener.Close() - log.Println("server: lisetning on", conf.Address) + log.Println("server: listening on", conf.Address) for { conn, err := listener.Accept() |
