From 6b6687f9e4da0d656307f5b90ede3a68b2f58e4b Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Wed, 13 Sep 2017 15:56:55 -0700 Subject: [PATCH] reset on error in echo --- examples/echo/main.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/examples/echo/main.go b/examples/echo/main.go index 7297e6b1..cc37453d 100644 --- a/examples/echo/main.go +++ b/examples/echo/main.go @@ -134,8 +134,12 @@ func main() { // a user-defined protocol name. ha.SetStreamHandler("/echo/1.0.0", func(s net.Stream) { log.Println("Got a new stream!") - defer s.Close() - doEcho(s) + if err := doEcho(s); err != nil { + log.Println(err) + s.Reset() + } else { + s.Close() + } }) if *target == "" { @@ -194,18 +198,14 @@ func main() { } // doEcho reads a line of data a stream and writes it back -func doEcho(s net.Stream) { +func doEcho(s net.Stream) error { buf := bufio.NewReader(s) str, err := buf.ReadString('\n') if err != nil { - log.Println(err) - return + return err } log.Printf("read: %s\n", str) _, err = s.Write([]byte(str)) - if err != nil { - log.Println(err) - return - } + return err }