Closed davidleitw closed 1 year ago
To be honest I haven't tried it, but you could try proxyClient.Do(ctx.Request, ctx.Response)
. In theory that should also work and won't require any copies.
@erikdubbelboer After examining the APIs provided by fasthttp in detail, I tried using SetBodyRaw()
to avoid copying. The detailed code is as follows. If my understanding of the API is correct, this should also avoid copying.
func proxyHandler(proxyClient *fasthttp.HostClient, ctx *fasthttp.RequestCtx, appId, method string) {
req, res := fasthttp.AcquireRequest(), fasthttp.AcquireResponse()
defer fasthttp.ReleaseRequest(req)
defer fasthttp.ReleaseResponse(res)
req.Header.SetMethod(method)
req.Header.Set("dapr-app-id", appId)
req.Header.SetContentType("application/json")
req.SetRequestURI("http://localhost:3500")
req.SetBodyRaw(ctx.PostBody())
if err := proxyClient.Do(req, res); err != nil {
ctx.Error(fmt.Sprintf("proxy handler get error: %s\n", err.Error()), http.StatusBadGateway)
return
}
ctx.Response.SetBodyRaw(res.Body())
ctx.SetStatusCode(res.StatusCode())
}
What I want to ask is whether the SetBodyRaw
API can avoid copying []byte as I understand it. I'm afraid my understanding is incorrect, but thank you for taking the time to answer my question. Thank you!
Yes that's probably an even better solution 👍
Recently, I have been trying to use fasthttp to write microservices based on dapr. The following is a simple example for practice. First, the client initiates a request to the Verify service, and then the Verify service calls the Addition service after verifying that the JSON is correct. After Addition completes processing, it returns to the Verify service and then to the client.
PS: Since the request needs to be forwarded to the dapr sidecar, the request will always be forwarded to localhost:3500
My question is as follows:
fasthttp.AcquireRequest()
andfasthttp.AcquireResponse()
when forwarding requests in the above code? Are there better ways to forward requests in this scenario?req.SetBody(ctx.PostBody())
when creating a new request, and also executedctx.SetBody(res.Body())
when preparing to return the result. I think this resulted in two unnecessary copies. Is there a better way to avoid copying?My questions are basically the above two points. I want to optimize the speed of forwarding requests as much as possible. Thank you for your answers!