summaryrefslogtreecommitdiff
path: root/internal
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2023-06-14 09:52:50 +0300
committerPaul Buetow <paul@buetow.org>2023-06-14 09:52:50 +0300
commit8259f1a764e0c3543c6770dc91433d85fb2e96db (patch)
treebeba47b6be4531fc6966b694bd4dfd1dcb901719 /internal
parent93b32777632eb79393e4f6fe0b752665c8e511ff (diff)
make loop interval configurable
Diffstat (limited to 'internal')
-rw-r--r--internal/config/config.go13
-rw-r--r--internal/quorum/quorum.go10
-rw-r--r--internal/run.go5
-rw-r--r--internal/server/tcpserver.go2
4 files changed, 24 insertions, 6 deletions
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()