Skip to content

Instantly share code, notes, and snippets.

@erikdubbelboer
Created September 18, 2021 20:15
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 erikdubbelboer/b18aff34c1627dd57b10be13716f27a6 to your computer and use it in GitHub Desktop.
Save erikdubbelboer/b18aff34c1627dd57b10be13716f27a6 to your computer and use it in GitHub Desktop.
fasthttp client timeout test
module test
go 1.17
require github.com/valyala/fasthttp v1.30.0
require (
github.com/andybalholm/brotli v1.0.2 // indirect
github.com/klauspost/compress v1.13.4 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
)
package main
import (
"time"
"github.com/valyala/fasthttp"
)
func main() {
go func() {
s := &fasthttp.Server{
Handler: func(ctx *fasthttp.RequestCtx) {
time.Sleep(time.Second * 11)
},
}
if err := s.ListenAndServe(":8080"); err != nil {
panic(err)
}
}()
client := &fasthttp.Client{
MaxConnsPerHost: 16,
MaxIdleConnDuration: time.Second * 50,
ReadBufferSize: 10 * 1024 * 1024,
WriteBufferSize: 1 * 1024,
ReadTimeout: time.Second * 10,
WriteTimeout: time.Second * 10,
}
for {
time.Sleep(time.Second)
req := fasthttp.AcquireRequest()
resp := fasthttp.AcquireResponse()
req.Header.SetContentType("application/json")
req.Header.SetMethod("POST")
req.SetRequestURI("http://localhost:8080/")
start := time.Now()
if err := client.Do(req, resp); err != nil {
println(err.Error())
} else {
println("ok")
}
println(time.Since(start).String())
fasthttp.ReleaseRequest(req)
fasthttp.ReleaseResponse(resp)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment