I have a files controller that is responsible for handling uploaded files in chunks (1 MB chunks). I took my time testing this in development and everything is as expected. The following is the pseudo code of an action:
logger.Log("Getting InputStream");
var stream = Request.InputStream; // Here is the wait.
logger.Log("Got InputStream");
var bytes = GetByteArray(stream); // This is always less than 1 MB.
await FilesService.AppendToFileAsync(key, fileName, owner, bytes); // Write to the file.
// The GetByteArray method
private byte[] GetByteArray(Stream stream)
{
var bytes = new byte[stream.Length];
stream.Read(bytes, 0, bytes.Length);
return bytes;
}
The specifics aren't terribly important. I'm getting the bytes then I'm appending them to the file on disk.
In production however, things get weird.
This is the request timeline of one chunk being uploaded (in production):
By means of logging I absolutely know that all the waiting time is being spent in the Request.InputStream
prop.
The waiting time is somewhere between 1 min - 3 mins.
My Question
- Does this mean the server is just taking that much time downloading the request? (though I really doubt that)
- Or is it that I'm doing something wrong here?
And a small question: is the Request sent
timeline telling me when the content bytes are done being received on the other side?
I know that perhaps I should write a file upload handler or something, but does that really make that much of a difference?
So, what's going on?
EDIT:
Of course, I looked into a lot of seemingly related issues but nothing hit the mark.
- I firstly suspected that the appdomain was being recycled on every chunk request (because I'm writing to a file) but I confirmed that this not the case from the logs. Also it wouldn't make any sense since the wait is on
Request.InputStream
.
EDIT 2:
I just tested with GetBufferlessInputStream
with manual reading inside the same action and the result is the same. Now I'm sure the wait is while reading the request stream. At this point, I'm getting the feeling that the server is just that slow with getting the request. I contacted my host and they said they don't put a limit on the speed and it shouldn't be slow.
To confirm this, this is one of my logs while a chunk was being uploaded:
2015-09-15 23:23:12.6419|Entered upload chunk // This is the action.
2015-09-15 23:23:12.6419|Got bufferless stream // var s = Request.GetBufferlessStream()
2015-09-15 23:23:12.6419|Stream length: 1048576 (1 MB) // s.Length
2015-09-15 23:23:12.6419|Reading stream... // Here is the wait, this time it was not much (~26 secs) but it's still a lot.
2015-09-15 23:23:38.8211|Read stream of size 1048576 bytes (1 MB)
and this is how I'm reading it:
var b = new byte[s.Length];
var read = 0;
var c = 0;
while ((c = s.Read(b, read, b.Length - read)) != 0)
{
read += c;
}