Skip to content

Instantly share code, notes, and snippets.

@komuw
Last active January 4, 2019 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save komuw/395192ff36058b77cc8766f787b7cedb to your computer and use it in GitHub Desktop.
Save komuw/395192ff36058b77cc8766f787b7cedb to your computer and use it in GitHub Desktop.
client and server odd behaviour
package main
import (
"bytes"
"io"
"log"
"net"
"time"
)
/*
usage:
go run -race client.go
*/
func main() {
start := time.Now()
Backend := "localhost:3000"
b := `POST /post HTTP/1.1
Host: localhost:3000
User-Agent: curl/7.54.0
accept: application/json
Content-Type: application/json
Content-Length: 15
{"name":"komu"}`
requestBytes := []byte(b)
backendConn, err := net.Dial("tcp", Backend)
if err != nil {
log.Fatalf("dial failed %+v", err)
}
defer backendConn.Close()
err = backendConn.SetDeadline(time.Now().Add(15 * time.Second))
if err != nil {
log.Fatalf("unable to set backendConn deadline %+v", err)
}
log.Printf("frontend connected to backend %v", Backend)
io.Copy(backendConn, bytes.NewReader(requestBytes))
log.Printf("send request to backend %v took %v seconds", Backend, time.Since(start).Seconds())
responseBuf := new(bytes.Buffer)
io.Copy(responseBuf, backendConn)
log.Printf("read response from backend %v took %v seconds", Backend, time.Since(start).Seconds())
log.Println(string(responseBuf.Bytes()))
}
package main
import (
"io/ioutil"
"log"
"net/http"
"time"
)
/*
usage:
go run -race server.go
*/
func main() {
http.HandleFunc("/post", echoHandler)
log.Println("listening at localhost:3000")
err := http.ListenAndServe(":3000", nil)
if err != nil {
log.Fatal("unable to listen and serve on port 3000", err)
}
}
func echoHandler(w http.ResponseWriter, r *http.Request) {
start := time.Now()
reqBytes, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Printf("unable to read request body %+v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
time.Sleep(1 * time.Second)
w.Header().Set("Content-Type", "application/json")
w.Write(reqBytes)
log.Printf("served response in %v seconds", time.Since(start).Seconds())
}
@komuw
Copy link
Author

komuw commented Jan 4, 2019

Run the server in one terminal;
go run -race server.go
and the client in another;
go run -race client.go

The odd thing is;
Even though the server only takes 1second to reply,
the client always waits 15seconds(ie the time specified in backendConn.SetDeadline) before exiting.

2019/01/04 17:11:48 read response from backend localhost:3000 took 15.006758846 seconds

However, if we changed the backend to something else, eg;

Backend := "google.com:80"

the client does not wait the full 15seconds before exiting

2019/01/04 17:05:52 read response from backend google.com:80 took 0.582892931 seconds

I would expect the behaviour of the client in the second case to be the right one always

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment