BUG/MEDIUM: channel: fix miscalculation of available buffer space.

The function channel_recv_limit() relies on channel_reserved() which
itself relies on channel_in_transit(). Individually they're OK but
combined they're doing the wrong thing.

The problem is that we refrain from filling buffers while to_forward
is even much larger than the buffer because of a semantic issue along
the call chain. This is particularly visible when offloading SSL on
moderately large files (1 MB), though it is also visible on clear text.
Twice the number of recv() calls are made compared to what is needed,
and the typical performance drops by 15-20% in SSL in 1.6 and later,
and no directly measurable drop in 1.5 except when using strace.

There's no need for all these intermediate functions, so let's get
rid of them and reimplement channel_recv_limit() from scratch in a
safer way.

This fix needs to be backported to 1.6 and 1.5 (at least). Note that in
1.5 the function is called buffer_max_len() and it may differ a bit.
(cherry picked from commit 999f643ed2dcf72779e8c18f300171d87177c04b)
(cherry picked from commit fed3c4efe837d2201c1bb6be7ad532227451b8ed)

[1.5: it's buffer_reserved() which was removed, channel_reserved() is a
 boolean used for a different purpose there. There's no empty buffer in
 1.5. The logic remains the same]
1 file changed