valyala / fasthttp

Fast HTTP package for Go. Tuned for high performance. Zero memory allocations in hot paths. Up to 10x faster than net/http
MIT License
21.87k stars 1.76k forks source link

slow browser initial connection compared with net/http #1610

Closed aggroot closed 1 year ago

aggroot commented 1 year ago

Hello, I m trying to build a web server using fiber and the initial connection between browser and server (the tcp handshake) seems slow compared with net/http and other frameworks: Here is some simple code that I m using to test and compare:


import (
    "fmt"
    "html"
    "log"
    "net/http"

    "github.com/gofiber/fiber/v2"
    "github.com/valyala/fasthttp"
)

func main() {

    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path))
    })

    http.HandleFunc("/hi", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "Hi")
    })

    go func() {
        log.Fatal(http.ListenAndServe(":8080", nil))
    }()

    app := fiber.New()

    app.Get("/", func(c *fiber.Ctx) error {
        return c.SendString("Hello world")
    })

    go func() {
        app.Listen(":8081")
    }()

    fasthttp.ListenAndServe(":8082", func(ctx *fasthttp.RequestCtx) {
        fmt.Fprintf(ctx, "Hello, %q", ctx.Path())
    })

}

Below are the results (also verified with fasthttp):

fiber: fiber

fasthttp: fasthttp

net/http net-html

erikdubbelboer commented 1 year ago

I can't reproduce this. Which version of Go are you using? Which OS are you on? How do you start/compile the programs?

If you are on Windows and you have a virus scanner it's probably causing this.

aggroot commented 1 year ago

go version: go version go1.20.4 linux/amd64 OS: WSL2 on windows with the browser opened on windows and server running in WSL start command: a simple go run

It could be my setup, but i'm thinking that my setup should affect also other frameworks or net/http, but it doesn't. I tried with gin also which uses a different router than net/http... and the results are similar with net/http

erikdubbelboer commented 1 year ago

Do you have any virus scanner? My guess is that your virus scanner recognizes net/http early and allows it quickly while fasthttp takes a bit longer. Try disabling your virus scanner. fasthttp just uses the normal Go networking library, just like net/http does. So I'm afraid there is nothing we could do about this.