fasthttp icon indicating copy to clipboard operation
fasthttp copied to clipboard

Reverse Proxy : fasthttp.HostClient crash

Open linuxpham opened this issue 1 year ago • 1 comments

import (
    "github.com/valyala/fasthttp"
)

var proxyClient = &fasthttp.HostClient{
    Addr: "upstream.host:port",
    // set other options here if required - most notably timeouts.
}

func ReverseProxyHandler(ctx *fasthttp.RequestCtx) {
    req := &ctx.Request
    resp := &ctx.Response
    prepareRequest(req)
    if err := proxyClient.Do(req, resp); err != nil {
        ctx.Logger().Printf("error when proxying the request: %s", err)
    }
    postprocessResponse(resp)
}

func prepareRequest(req *fasthttp.Request) {
    // do not proxy "Connection" header.
    req.Header.Del("Connection")
    // strip other unneeded headers.

    // alter other request params before sending them to upstream host
    // req.SetHost(upstream.host)
}

func postprocessResponse(resp *fasthttp.Response) {
    // do not proxy "Connection" header
    resp.Header.Del("Connection")

    // strip other unneeded headers

    // alter other response data if needed
}

func main() {
    if err := fasthttp.ListenAndServe(":8080", reverseProxyHandler); err != nil {
        log.Fatalf("error in fasthttp server: %s", err)
    }
}

fasthttp.HostClient Do (function): Crash when forwarding big file (> 2GB)

linuxpham avatar Jul 03 '23 05:07 linuxpham

Can we get a stack trace or an error message?

erikdubbelboer avatar Jul 03 '23 07:07 erikdubbelboer