From 6cd559398b5b1183c19ac0ab2b5840b02132489c Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Sat, 20 May 2023 01:44:29 +0300 Subject: split up to server --- internal/run.go | 28 +--------------------------- internal/server.go | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 27 deletions(-) create mode 100644 internal/server.go 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) + } +} -- cgit v1.2.3