in walkthroughs/howto-timeout-policy/src/colorgateway/main.go [171:205]
func (h *tcpEchoHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
endpoint, err := getTCPEchoEndpoint()
if err != nil {
writer.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(writer, "tcpecho endpoint is not set")
return
}
log.Printf("Dialing tcp endpoint %s", endpoint)
conn, err := net.Dial("tcp", endpoint)
if err != nil {
writer.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(writer, "Dial failed, err:%s", err.Error())
return
}
defer conn.Close()
strEcho := "Hello from gateway"
log.Printf("Writing '%s'", strEcho)
_, err = fmt.Fprintf(conn, strEcho)
if err != nil {
writer.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(writer, "Write to server failed, err:%s", err.Error())
return
}
reply, err := bufio.NewReader(conn).ReadString('\n')
if err != nil {
writer.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(writer, "Read from server failed, err:%s", err.Error())
return
}
fmt.Fprintf(writer, "Response from tcpecho server: %s", reply)
}