the throttle handler reimplementation has one main strenght - it can use
sendfile. Other benifits is that it removes a dependancy. I would also
argue it's lighter runtime(don't quote me).
In order to use this we use the now new ThrottledWriter which records
how much bytes it has throttled at a given speed and tries to write as
much as it can in order to get to the speed that is requested.
The reason why it's done in the timeoutConn instead of in the handler
given that it requires the whole getting the correct connection for each
request is:
http.ResponseWriter.ReadFrom does os.Stat on each io.LimitedReader it
gets. This adds up pretty quickly to hundred of thousands of os.Stat, which
both slow down the whole application, and make a lot of objects.
Given the fact that io.LimitedReader is the only way to limit the amount
of data the sendfile sends at a time and there is no way to go around
http.ResponseWriter.ReadFrom without Hijacking the connection, the
current solution emerged.
Additional benifit is that other handlers can now throttle as well.
the throttle handler reimplementation has one main strenght - it can use sendfile. Other benifits is that it removes a dependancy. I would also argue it's lighter runtime(don't quote me).
In order to use this we use the now new ThrottledWriter which records how much bytes it has throttled at a given speed and tries to write as much as it can in order to get to the speed that is requested.
The reason why it's done in the timeoutConn instead of in the handler given that it requires the whole getting the correct connection for each request is: http.ResponseWriter.ReadFrom does os.Stat on each io.LimitedReader it gets. This adds up pretty quickly to hundred of thousands of os.Stat, which both slow down the whole application, and make a lot of objects.
Given the fact that io.LimitedReader is the only way to limit the amount of data the sendfile sends at a time and there is no way to go around http.ResponseWriter.ReadFrom without Hijacking the connection, the current solution emerged.
Additional benifit is that other handlers can now throttle as well.