aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs
diff options
context:
space:
mode:
authorLuke Pulverenti <luke.pulverenti@gmail.com>2014-10-22 00:42:08 -0400
committerLuke Pulverenti <luke.pulverenti@gmail.com>2014-10-22 00:42:08 -0400
commit1e7ac871db62d749725226fe05d61ab2629e79e9 (patch)
treeb39e47ede447922270fa7f62f6302975de34aa50 /MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs
parent931bf29c3e64870dc4187064b91ec58461d6ac38 (diff)
add http client timeout
Diffstat (limited to 'MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs')
-rw-r--r--MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs48
1 files changed, 45 insertions, 3 deletions
diff --git a/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs b/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs
index 4a8597b7f..093b904f1 100644
--- a/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs
+++ b/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs
@@ -123,7 +123,7 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager
}
request.Method = method;
- request.Timeout = 20000;
+ request.Timeout = options.TimeoutMs;
if (!string.IsNullOrEmpty(options.Host))
{
@@ -390,7 +390,7 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager
if (!options.BufferContent)
{
- var response = await httpWebRequest.GetResponseAsync().ConfigureAwait(false);
+ var response = await GetResponseAsync(httpWebRequest, TimeSpan.FromMilliseconds(options.TimeoutMs)).ConfigureAwait(false);
var httpResponse = (HttpWebResponse)response;
@@ -401,7 +401,7 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager
return GetResponseInfo(httpResponse, httpResponse.GetResponseStream(), GetContentLength(httpResponse), httpResponse);
}
- using (var response = await httpWebRequest.GetResponseAsync().ConfigureAwait(false))
+ using (var response = await GetResponseAsync(httpWebRequest, TimeSpan.FromMilliseconds(options.TimeoutMs)).ConfigureAwait(false))
{
var httpResponse = (HttpWebResponse)response;
@@ -843,5 +843,47 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager
{
return Post(url, postData, null, cancellationToken);
}
+
+ private Task<WebResponse> GetResponseAsync(WebRequest request, TimeSpan timeout)
+ {
+ var taskCompletion = new TaskCompletionSource<WebResponse>();
+
+ Task<WebResponse> asyncTask = Task.Factory.FromAsync<WebResponse>(request.BeginGetResponse, request.EndGetResponse, null);
+
+ ThreadPool.RegisterWaitForSingleObject((asyncTask as IAsyncResult).AsyncWaitHandle, TimeoutCallback, request, timeout, true);
+ asyncTask.ContinueWith(task =>
+ {
+ taskCompletion.TrySetResult(task.Result);
+
+ }, TaskContinuationOptions.NotOnFaulted);
+
+ // Handle errors
+ asyncTask.ContinueWith(task =>
+ {
+ if (task.Exception != null)
+ {
+ taskCompletion.TrySetException(task.Exception);
+ }
+ else
+ {
+ taskCompletion.TrySetException(new List<Exception>());
+ }
+
+ }, TaskContinuationOptions.OnlyOnFaulted);
+
+ return taskCompletion.Task;
+ }
+
+ private static void TimeoutCallback(object state, bool timedOut)
+ {
+ if (timedOut)
+ {
+ WebRequest request = (WebRequest)state;
+ if (state != null)
+ {
+ request.Abort();
+ }
+ }
+ }
}
}