DistributedNonsense/CorrugatedIron — pull request #44 by kppullin (2012-08-12 01:42:19+00:00)
Title: Updated `RiakPbcSocket.Write()` to send the message in 16KB chunks

## Change

Updated `RiakPbcSocket.Write()` to send the message in 16KB chunks
instead of all at once. This addresses issues seen when PUTting large
messages.

## Details

### Problem

Sending a single large buffer via `Socket.Send()` returns immediately.
After the send, the code then proceeds to call `Socket.Read()` in order
to obtain the result of the operation

This works fine for small messages because the Riak server is able to accept
and ACK the TCP packets and send a reply before the `Socket.Receive()` request is issued.

In the case of writing large messages, `Socket.Receive()` is called too soon, before the
bytes have been fully sent and ACK'd, resulting in a SocketException (timeout).

### Fix

`Socket.Send()` will block when called if the bytes from a previously
issued Send() call has yet to be fully sent. By looping through the message
and sending in small chunks, we are able to avoid the problem above.

### Examples

When sending a 3MB buffer all at once, `Socket.Send()` returns
immediatly and a `Socket.Receive()` is issued before the data has completed
transmitting, ultimately resulting in a timeout (default timeout = 2 seconds).

On the other hand, splitting this 3MB message into 16KB chunks,
each call to `Socket.Send()` waits for the previous call to
complete.

From this, when the `Socket.Receive()` command is issued,
we only have to wait until the last 16KB chunk to complete, because
we can safely assume all previous chunks have been transmitted.

Unless the network link is very slow, the final 16KB chunk will complete
and the response sent within the 2 second timeout receive window.