This repository has been archived by the owner on Jun 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 214
Adding timeout to http request (sync and async) #118
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
// limitations under the License. | ||
//---------------------------------------------------------------------- | ||
|
||
using System; | ||
using System.IO; | ||
using System.Net; | ||
using System.Threading.Tasks; | ||
|
@@ -26,6 +27,8 @@ internal class HttpWebRequestWrapper : IHttpWebRequest | |
{ | ||
private readonly HttpWebRequest request; | ||
|
||
private int timeoutInMilliSeconds = 30000; | ||
|
||
public HttpWebRequestWrapper(string uri) | ||
{ | ||
this.request = (HttpWebRequest)WebRequest.Create(uri); | ||
|
@@ -73,6 +76,14 @@ public WebHeaderCollection Headers | |
} | ||
} | ||
|
||
public int TimeoutInMilliSeconds | ||
{ | ||
set | ||
{ | ||
this.timeoutInMilliSeconds = value; | ||
} | ||
} | ||
|
||
public async Task<IHttpWebResponse> GetResponseSyncOrAsync(CallState callState) | ||
{ | ||
if (this.BodyParameters != null) | ||
|
@@ -86,10 +97,42 @@ public async Task<IHttpWebResponse> GetResponseSyncOrAsync(CallState callState) | |
#if ADAL_NET | ||
if (callState != null && callState.CallSync) | ||
{ | ||
this.request.Timeout = this.timeoutInMilliSeconds; | ||
return NetworkPlugin.HttpWebRequestFactory.CreateResponse(this.request.GetResponse()); | ||
} | ||
|
||
Task<WebResponse> getResponseTask = this.request.GetResponseAsync(); | ||
System.Threading.ThreadPool.RegisterWaitForSingleObject( | ||
((IAsyncResult)getResponseTask).AsyncWaitHandle, | ||
delegate (object state, bool timedOut) | ||
{ | ||
if (timedOut) | ||
{ | ||
((HttpWebRequest)state).Abort(); | ||
} | ||
}, | ||
this.request, | ||
this.timeoutInMilliSeconds, | ||
true); | ||
|
||
return NetworkPlugin.HttpWebRequestFactory.CreateResponse(await getResponseTask); | ||
#else | ||
var timer = Windows.System.Threading.ThreadPoolTimer.CreateTimer( | ||
delegate | ||
{ | ||
this.request.Abort(); | ||
}, | ||
TimeSpan.FromMilliseconds(this.timeoutInMilliSeconds)); | ||
|
||
try | ||
{ | ||
return NetworkPlugin.HttpWebRequestFactory.CreateResponse(await this.request.GetResponseAsync()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. HttpWebRequest class has timeout option. You should be able to set timeout for specific request. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It does not work for async calls. That is why I had to use the other approaches. |
||
} | ||
finally | ||
{ | ||
timer.Cancel(); | ||
} | ||
#endif | ||
return NetworkPlugin.HttpWebRequestFactory.CreateResponse(await this.request.GetResponseAsync()); | ||
} | ||
|
||
public async Task<Stream> GetRequestStreamSyncOrAsync(CallState callState) | ||
|
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
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How is this method used? I ask because value could be negative.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is in an internal class and the value may only change by our tests, so no need to worry.