Read request body before redirecting it

Hi All,
I am creating a proxy (or if I use ReverseProxy), I get the request and need to compute a hash function on it’s body. I want to calculate this hash in the same time as I redirect this message to its destination (assume body is large), I want it to start stream to the destination and meanwhile I calculate my hash function. So I cannot read all the body and start sending it only then.
But the body is io.ReadCloser and it cannot read it twice.
What can I do to still be able to resend the request and calculate the hash with minimal latency?

Thank you in ahead!

Maybe you can use a TeeReader to split the stream into the outgoing request and the hash function?

1 Like

Thanks for the tip, I will try TeeReader. I am afraid it will reduce the performance of my “main” process. I need to redirect the message as fast as possible to the server, and now I will add writes to this critical path. Do you have maybe another idea that will not add latency to the first read (I can pay this latency in the second one if this possible).

Thank you,

I suggest trying the simplest solution first, then measuring if it impacts your latency enough to matter.

1 Like

Hi, I did some basic performance tests and it seems to work fine.
Thank you

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.