Proxying connections between traefik and haProxy

Hello traefikers,

first let's explain what I am trying to do. I am running my own proxy application between traefik and a haProxy instance. This proxy accepts tcp requests from haProxy that contain a SNI and does some preprocessing based on that.
After processing the request, I am building a tunnel between traefik and haProxy and forward the data in both directions. Traefik is then used to terminate ssl and forward to a kubernetes service that serves some static pages. This setup works quite well, but I am facing one problem:

The connection to traefik is created for every incoming request. I was not able to determine when stop reading the connection to traefik. My proxy application gets stuck while reading, because I do not know when to close the connection.
Is there some kind of stop signal (EOF) when the page is fully served to the connection?
Currently, I am working with a timeout and close the connection when that timeout is reached. This is a messy approach, because the bidirectional pipe gets reused and there is a chance to timeout while reading.

Here is an example how I do the bidirectional copy.


I commented the section where it keeps blocking until the previously defined timeouts are reached.
My understanding is that if I should read, until the upstream connections writes a certain signal or returns a certain error.
I also tried a much simpler way by just using io.Copy() function twice.

I am also thinking about not opening the connection to traefik for every client connection and let the read operation block if no data is sent. Maybe it's a better solution to get rid of all timeouts and just reuse the connection to traefik forever.

I would love to get some help and exchange thoughts about this. I'm really stuck at this point and would be rly happy about any suggestions / improvements. This also might not be a traefik specific problem, but a more general lack of my understanding of connections in golang.

Thank you