From c4859fa25edb6f4f4d4ad9c05a93ac2460650ba7 Mon Sep 17 00:00:00 2001 From: David Florness Date: Sun, 7 Jun 2020 01:16:09 -0600 Subject: [PATCH] In master, run findPeers in goroutine rather than waiting of closing Doing it this way doesn't require an empty select. --- main.go | 44 +++++++++++++++++++------------------------- 1 file changed, 19 insertions(+), 25 deletions(-) diff --git a/main.go b/main.go index 079e384..9df67b9 100644 --- a/main.go +++ b/main.go @@ -94,36 +94,30 @@ func bootstrap() { ch := make(chan int, 1) closeElection = ch + go findPeers(ch) - go func() { - fmt.Println("press ENTER to solidify group of voters and start voting") - stdReader := bufio.NewReader(os.Stdin) - _, err := stdReader.ReadString('\n') + fmt.Println("press ENTER to solidify group of voters and start voting") + stdReader := bufio.NewReader(os.Stdin) + _, err := stdReader.ReadString('\n') + if err != nil { + panic(err) + } + + logger.Info("ENTER has been pressed; closing election") + n := len(me.otherVoters) + closeElection <- n + close(closeElection) + for _, voter := range me.otherVoters { + stream, err := me.h.NewStream(me.ctx, voter.ID, protocolID) if err != nil { panic(err) } - - logger.Info("ENTER has been pressed; closing election") - n := len(me.otherVoters) - closeElection <- n - close(closeElection) - for _, voter := range me.otherVoters { - stream, err := me.h.NewStream(me.ctx, voter.ID, protocolID) - if err != nil { - panic(err) - } - writer := bufio.NewWriter(stream) - writer.WriteString(fmt.Sprintf("close\n%d", n)) - writer.Flush() - stream.Close() - } - }() - - findPeers(ch) - - select {} // temporary + writer := bufio.NewWriter(stream) + writer.WriteString(fmt.Sprintf("close\n%d", n)) + writer.Flush() + stream.Close() + } } else { // we are a slave - logger.Info("attempting to open stream with master peer...") stream, err := me.h.NewStream(me.ctx, masterID, protocolID) rw := bufio.NewReadWriter(bufio.NewReader(stream), bufio.NewWriter(stream)) -- 2.38.4