Skip to content

Instantly share code, notes, and snippets.

@leizongmin
Created July 14, 2021 14:14
Show Gist options
  • Save leizongmin/be891d83fce6c0bc442db0cd509e3cfc to your computer and use it in GitHub Desktop.
Save leizongmin/be891d83fce6c0bc442db0cd509e3cfc to your computer and use it in GitHub Desktop.
fasthttp ReadBufferSize example
package main
import (
"fmt"
"github.com/valyala/fasthttp"
"log"
"net/http"
"os"
"strings"
"time"
)
func main() {
go test()
s := fasthttp.Server{ReadBufferSize: 1024 * 128}
s.Handler = func(ctx *fasthttp.RequestCtx) {
log.Printf("Got request url size %d", len(ctx.RequestURI()))
ctx.SetStatusCode(200)
ctx.SetBodyString("OK")
}
if err := s.ListenAndServe(":12345"); err != nil {
log.Fatal(err)
}
}
func test() {
time.Sleep(time.Second)
client := http.Client{}
send := func(size int, status int) {
req, err := http.NewRequest("GET", "http://127.0.0.1:12345/?q="+strings.Repeat("a", size), nil)
if err != nil {
log.Fatal(err)
return
}
res, err := client.Do(req)
if err != nil {
log.Fatal(err)
return
}
log.Printf("response %d %+v", res.StatusCode, res.Header)
if status != res.StatusCode {
panic(fmt.Sprintf("expected %d, got %d", status, res.StatusCode))
}
}
send(1024*128, 431)
send(1024*127, 200)
os.Exit(0)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment