Go has channels for synchronization and communication, use them!
Make your connection in a loop and wait for some message to return to the channel.
...
errCh := make(chan error)
for {
lsconn, err = net.Dial("tcp", logStashHost)
go ReadLogStash(lsconn, errCh)
err = <-errCh
if err != nil {
break
}
}
...
func ReadLogStash(conn net.Conn, errCh chan error) {
_, err := io.Copy(os.Stderr, conn)
if err != nil {
fmt.Println(err)
}
errCh <- err
}
If you have more functions in ReadLogStash, you might just use io.Copy inline and forget the whole function, but this template may come in handy for you anyway.
source
share