summaryrefslogtreecommitdiff
path: root/internal/server.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/server.go')
-rw-r--r--internal/server.go35
1 files changed, 35 insertions, 0 deletions
diff --git a/internal/server.go b/internal/server.go
new file mode 100644
index 0000000..2024b88
--- /dev/null
+++ b/internal/server.go
@@ -0,0 +1,35 @@
+package internal
+
+import (
+ "context"
+ "log"
+ "time"
+)
+
+func runServer(ctx context.Context, config config) {
+ ch := make(chan vote)
+ quorum := make(quorumMap)
+
+ go func() {
+ for {
+ select {
+ case vote := <-ch:
+ quorum.vote(vote)
+ winner, err := quorum.winner(config)
+ if err != nil {
+ log.Println(err.Error())
+ continue
+ }
+ log.Printf("The current leader node is %s", winner)
+ case <-time.After(voteExpiry):
+ quorum.cleanExpired()
+ case <-ctx.Done():
+ return
+ }
+ }
+ }()
+
+ if err := startTcpServer(ctx, config, ch); err != nil {
+ panic(err)
+ }
+}