~edwargix/tallyard

c4859fa25edb6f4f4d4ad9c05a93ac2460650ba7 — David Florness 5 years ago 70372a4
In master, run findPeers in goroutine rather than waiting of closing

Doing it this way doesn't require an empty select.
1 files changed, 19 insertions(+), 25 deletions(-)

M main.go
M main.go => main.go +19 -25
@@ 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))