This is what I actually do behind the scene:
- Send a HTTP request to a remote server.
- Do two things with the HTTP response coming from the remote server.
1- Read it, parse it to struct and give it to my client in handler.
2- Dump whole response (uri, headers and body …) into a (because I am auditing all the responses):
a) TXT file, if the application is running in local env.
b) S3 bucket, if the application is not running in local env.
I just didin’t want a and b delay me from responding to my client because both sometimes take long time. That’s why I used defer
.
@Dean_Davidson So I guess what you are saying is that:
- instead of using
defer
, use it likego audit.Log(...)
and manageDATA RACE
which is happening at the moment. - stream it rather than using
io.ReadAll(res.Body)
. I guess you meanvar buf bytes.Buffer
-io.Copy(&buf, resp.Body)
?
By the way if you can think of any better solution, I am happy to listen. Thank you.