Skip to content

Commit

Permalink
fix timeout logic
Browse files Browse the repository at this point in the history
  • Loading branch information
bsideup committed Jan 16, 2018
1 parent 52e34c4 commit 5e9cb6f
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ func main() {
log.Printf("Starting on port %d...", *port)

cli, err := client.NewEnvClient()
if err == nil {
_, err = cli.Ping(context.Background())
}

if err != nil {
panic(err)
}
Expand All @@ -37,6 +41,7 @@ func main() {
for {
conn, _ := ln.Accept()
connected <- true
log.Println("Connected")
reader := bufio.NewReader(conn)
for {
message, err := reader.ReadString('\n')
Expand Down Expand Up @@ -77,19 +82,22 @@ func main() {
}
}
disconnected <- true
log.Println("Disconnected")
conn.Close()
}
}()

select {
case <-time.After(1 * time.Minute):
panic("Timed out waiting for the initial connection")
case <-connected:
}

TimeoutLoop:
for {
select {
case <-time.After(1 * time.Minute):
panic("Timed out waiting for the initial connection")
case <-connected:
log.Println("Connected")
case <-disconnected:
log.Println("Disconnected")
select {
case <-connected:
case <-time.After(10 * time.Second):
Expand Down

0 comments on commit 5e9cb6f

Please sign in to comment.