summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2023-05-28 21:19:56 +0300
committerPaul Buetow <paul@buetow.org>2023-05-28 21:19:56 +0300
commit16fed128d96722f930cc5168c3aaea11ee775159 (patch)
treef432aeda6cb274e5fa4631ee72032b6f1d342356
parent20992b495c5651de53201a9bf8737d1a198bf721 (diff)
fix race condition
-rw-r--r--internal/client/client.go5
-rw-r--r--internal/quorum/quorum.go3
-rw-r--r--internal/quorum/quorum_test.go2
-rw-r--r--internal/server/server.go2
4 files changed, 7 insertions, 5 deletions
diff --git a/internal/client/client.go b/internal/client/client.go
index 76a61f0..8f9e851 100644
--- a/internal/client/client.go
+++ b/internal/client/client.go
@@ -12,9 +12,10 @@ func Start(ctx context.Context, conf config.Config, winnerCh <-chan string) {
go func() {
for {
log.Println("Starting client")
- start(ctx, conf, winnerCh)
+ run(ctx, conf, winnerCh)
select {
+ // Wait a second before restarting the clien
case <-time.After(time.Second):
case <-ctx.Done():
return
@@ -23,7 +24,7 @@ func Start(ctx context.Context, conf config.Config, winnerCh <-chan string) {
}()
}
-func start(ctx context.Context, conf config.Config, winnerCh <-chan string) {
+func run(ctx context.Context, conf config.Config, winnerCh <-chan string) {
fanOut := make([]chan string, len(conf.Participants))
for i := 0; i < len(fanOut); i++ {
diff --git a/internal/quorum/quorum.go b/internal/quorum/quorum.go
index 0e407b6..b31a472 100644
--- a/internal/quorum/quorum.go
+++ b/internal/quorum/quorum.go
@@ -38,7 +38,6 @@ func (quo Quorum) Start(ctx context.Context) chan string {
for {
select {
case <-time.After(vote.Expiry):
- quo.deleteExpiredVotes()
winner, err := quo.winner()
if err != nil {
log.Println(err)
@@ -106,7 +105,7 @@ func (quo Quorum) score() (scores []Score) {
return
}
-func (quo Quorum) deleteExpiredVotes() {
+func (quo Quorum) DeleteExpiredVotes() {
var expired []string
for from, vote := range quo.votes {
diff --git a/internal/quorum/quorum_test.go b/internal/quorum/quorum_test.go
index 02150ca..3e05692 100644
--- a/internal/quorum/quorum_test.go
+++ b/internal/quorum/quorum_test.go
@@ -112,7 +112,7 @@ func TestExpire(t *testing.T) {
t.Errorf("Expected to have two votes before expiry: %v", quo)
}
- quo.deleteExpiredVotes()
+ quo.DeleteExpiredVotes()
if len(quo.votes) != 1 {
t.Errorf("Expected to have only one vote after expiry: %v", quo)
}
diff --git a/internal/server/server.go b/internal/server/server.go
index dc1fd90..a56899a 100644
--- a/internal/server/server.go
+++ b/internal/server/server.go
@@ -38,6 +38,8 @@ func run(ctx context.Context, conf config.Config, quo quorum.Quorum) error {
select {
case vote := <-ch:
quo.Vote(vote)
+ case <-time.After(vote.Expiry):
+ quo.DeleteExpiredVotes()
case <-serverCtx.Done():
return
}