diff options
Diffstat (limited to 'internal/quorum/quorum.go')
| -rw-r--r-- | internal/quorum/quorum.go | 12 |
1 files changed, 11 insertions, 1 deletions
diff --git a/internal/quorum/quorum.go b/internal/quorum/quorum.go index b31a472..6c4bcb6 100644 --- a/internal/quorum/quorum.go +++ b/internal/quorum/quorum.go @@ -14,6 +14,7 @@ import ( type Quorum struct { conf config.Config votes map[string]vote.Vote + inCh chan vote.Vote } type Score struct { @@ -25,6 +26,7 @@ func New(conf config.Config) Quorum { return Quorum{ conf: conf, votes: make(map[string]vote.Vote), + inCh: make(chan vote.Vote), } } @@ -38,6 +40,9 @@ func (quo Quorum) Start(ctx context.Context) chan string { for { select { case <-time.After(vote.Expiry): + quo.deleteExpiredVotes() + case vote := <-quo.inCh: + quo.vote(vote) winner, err := quo.winner() if err != nil { log.Println(err) @@ -65,6 +70,11 @@ func (quo Quorum) Start(ctx context.Context) chan string { } func (quo Quorum) Vote(v vote.Vote) { + log.Printf("Queing vote %v", v) + quo.inCh <- v +} + +func (quo Quorum) vote(v vote.Vote) { log.Printf("Adding vote %v", v) quo.votes[v.From] = v } @@ -105,7 +115,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 { |
