summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2023-05-20 01:44:29 +0300
committerPaul Buetow <paul@buetow.org>2023-05-20 01:44:29 +0300
commit6cd559398b5b1183c19ac0ab2b5840b02132489c (patch)
tree85ca4502986dd4910437fe6b85f31e6f6693e4fd
parent16019def84f61d4adc9e25e4bd2d0a6c8bf36c40 (diff)
split up to server
-rw-r--r--internal/run.go28
-rw-r--r--internal/server.go35
2 files changed, 36 insertions, 27 deletions
diff --git a/internal/run.go b/internal/run.go
index 50a5b6f..a8be7ca 100644
--- a/internal/run.go
+++ b/internal/run.go
@@ -2,8 +2,6 @@ package internal
import (
"context"
- "log"
- "time"
)
func Run(ctx context.Context, configFile string) {
@@ -12,29 +10,5 @@ func Run(ctx context.Context, configFile string) {
panic(err)
}
- 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)
- }
+ runServer(ctx, config)
}
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)
+ }
+}