blob: d388e01365682be1de0516d00abf2ac6217a7c8a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
package server
import (
"context"
"log"
"time"
"codeberg.org/snonux/gorum/internal/config"
"codeberg.org/snonux/gorum/internal/quorum"
"codeberg.org/snonux/gorum/internal/vote"
)
func Start(ctx context.Context, conf config.Config) {
go func() {
for {
log.Println("Starting server")
start(ctx, conf)
select {
case <-time.After(time.Second):
case <-ctx.Done():
return
}
}
}()
}
func start(ctx context.Context, conf config.Config) {
ch := make(chan vote.Vote)
quo := make(quorum.Quorum)
go func() {
for {
select {
case vote := <-ch:
quo.Vote(vote)
winner, err := quo.Winner(conf)
if err != nil {
log.Println(err.Error())
continue
}
log.Printf("The current leader node is %s", winner)
case <-time.After(vote.Expiry):
quo.CleanExpired()
case <-ctx.Done():
return
}
}
}()
if err := tcpServerStart(ctx, conf, ch); err != nil {
panic(err)
}
}
|