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

Is there a way to send early hints from fasthttp server? #1636

Closed pablolagos closed 1 year ago

pablolagos commented 1 year ago

In net/http the early hints are solved this way:

// main.go
package main

import (
    "io"
    "log"
    "net/http"
)

func main() {
    helloHandler := func(w http.ResponseWriter, req *http.Request) {
        w.Header().Add("Link", "</style.css>; rel=preload; as=style")
        w.Header().Add("Link", "</script.js>; rel=preload; as=script")

        w.WriteHeader(103)

        // do your heavy tasks such as DB or remote APIs calls here

        w.WriteHeader(200)

        io.WriteString(w, "<!doctype html>\n[... rest of the response body is omitted from the example ...]")
    }

    http.HandleFunc("/hello", helloHandler)
    log.Fatal(http.ListenAndServeTLS(":443", "cert.pem", "key.pem", nil))
}

How can we perform the same action with fasthttp server?

erikdubbelboer commented 1 year ago

No this is not possible. Fasthttp was not designed for this. Fasthttp always takes the whole response (headers and body) into memory and only sends it to the client when your hander returns.