blob: e9d62f0e41dc68640b78e57ef54ed1c2024d9dfc (
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
|
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, quo quorum.Quorum) {
go func() {
for {
log.Println("server: starting")
if err := runServer(ctx, conf, quo); err != nil {
log.Println("server:", err)
}
select {
case <-time.After(time.Second):
case <-ctx.Done():
return
}
}
}()
}
func runServer(ctx context.Context, conf config.Config, quo quorum.Quorum) error {
serverCtx, cancel := context.WithCancel(ctx)
defer cancel()
ch := make(chan vote.Vote)
go func() {
for {
select {
case vote := <-ch:
quo.Vote(vote)
case <-serverCtx.Done():
return
}
}
}()
return tcpServerRun(serverCtx, conf, func(message string) string {
ch <- vote.New(conf, message)
return "ok - I received your message, dear client"
})
}
|