-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
moberacker
committed
Feb 21, 2019
1 parent
11c39d4
commit 61f50db
Showing
2 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using System; | ||
using System.Net.Http; | ||
|
||
namespace Log4ALA | ||
{ | ||
public static class HttpRequestExtensions | ||
{ | ||
private static string TimeoutPropertyKey = "RequestTimeout"; | ||
|
||
public static void SetTimeout(this HttpRequestMessage request, TimeSpan? timeout) | ||
{ | ||
if (request == null) throw new ArgumentNullException(nameof(request)); | ||
request.Properties[TimeoutPropertyKey] = timeout; | ||
} | ||
|
||
public static TimeSpan? GetTimeout(this HttpRequestMessage request) | ||
{ | ||
if (request == null) throw new ArgumentNullException(nameof(request)); | ||
if (request.Properties.TryGetValue(TimeoutPropertyKey, out var value) && value is TimeSpan timeout) | ||
return timeout; | ||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using System; | ||
using System.Net.Http; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Log4ALA | ||
{ | ||
public class TimeoutHandler : DelegatingHandler | ||
{ | ||
public TimeSpan DefaultTimeout { get; set; } = TimeSpan.FromSeconds(100); | ||
|
||
protected async override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) | ||
{ | ||
using (var cts = GetCancellationTokenSource(request, cancellationToken)) | ||
{ | ||
try | ||
{ | ||
return await base.SendAsync(request, cts?.Token ?? cancellationToken); | ||
} | ||
catch (OperationCanceledException) when (!cancellationToken.IsCancellationRequested) | ||
{ | ||
throw new TimeoutException(); | ||
} | ||
} | ||
} | ||
|
||
private CancellationTokenSource GetCancellationTokenSource(HttpRequestMessage request, CancellationToken cancellationToken) | ||
{ | ||
var timeout = request.GetTimeout() ?? DefaultTimeout; | ||
if (timeout == Timeout.InfiniteTimeSpan) | ||
{ | ||
// No need to create a CTS if there's no timeout | ||
return null; | ||
} | ||
else | ||
{ | ||
var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); | ||
cts.CancelAfter(timeout); | ||
return cts; | ||
} | ||
} | ||
} | ||
} |