Last active
January 4, 2019 14:13
-
-
Save komuw/395192ff36058b77cc8766f787b7cedb to your computer and use it in GitHub Desktop.
client and server odd behaviour
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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;
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