-
Notifications
You must be signed in to change notification settings - Fork 25.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(http): http requests to implement xhr timeout #36681
Conversation
Would anyone be able to review this PR? |
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.
Thx for updating the docs too 😍
A minor comments - otherwise lgtm ✨
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.
One suggestion. Otherwise lgtm 👍
BTW, please squash the commit into one (or create fixup commits).
@@ -74,6 +84,10 @@ export class ConfigService { | |||
if (error.error instanceof ErrorEvent) { | |||
// A client-side or network error occurred. Handle it accordingly. | |||
console.error('An error occurred:', error.error.message); | |||
} else if (error.status === 0 && error.statusText === 'Request timeout') { |
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.
It is no longer certain that error.statusText === 'Request timeout'
. How about error.error instanceof ProgressEvent && error.status === 0
?
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.
Should there also be documentation on how to fake a request timeout for testing?
|
||
* The server backend might reject the request, returning an HTTP response with a status code such as 404 or 500. These are error _responses_. | ||
|
||
* Something could go wrong on the client-side such as a network error that prevents the request from completing successfully or an exception thrown in an RxJS operator. These errors produce JavaScript `ErrorEvent` objects. | ||
|
||
`HttpClient` captures both kinds of errors in its `HttpErrorResponse`. You can inspect that response to identify the error's cause. | ||
* The server took too long to respond, but only when a timeout value has been passed in as an optional parameter with the request object or the `HttpClient`'s request methods. |
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.
Perhaps more concise:
* The server took too long to respond, but only when a timeout value has been passed in as an optional parameter with the request object or the `HttpClient`'s request methods. | |
* The server took too long to respond. This can occur only when the request specifies an explicit `timeout`. |
@@ -134,20 +140,23 @@ export class HttpRequest<T> { | |||
params?: HttpParams, | |||
responseType?: 'arraybuffer'|'blob'|'json'|'text', | |||
withCredentials?: boolean, | |||
timeout?: number, |
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.
@gkalpak perhaps out of scope for this PR, but would it make sense to extract this options object into its own type? The repetition seems unfortunate.
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.
👍
@@ -196,6 +207,10 @@ export class HttpRequest<T> { | |||
this.headers = options.headers; | |||
} | |||
|
|||
if (!!options.timeout) { |
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.
Using !!
is unnecessary in conditionals since it will always evaluate to the same thing as normal truthiness.
@@ -93,6 +93,11 @@ export class HttpXhrBackend implements HttpBackend { | |||
xhr.withCredentials = true; | |||
} | |||
|
|||
// Set the timeout if set in request object | |||
if (req.timeout) { |
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.
What happens if xhr.timeout
is set to null
, undefined
, or zero?
@@ -234,6 +239,19 @@ export class HttpXhrBackend implements HttpBackend { | |||
} | |||
}; | |||
|
|||
// The onTimeout callback is called when the request has timed out. This timeout is optional | |||
// and can be set by the user. | |||
const onTimeout = (error: ProgressEvent) => { |
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.
@gkalpak I'm wondering if creating this function for every request will allocate an additional amount of memory that we care about. Would it make a meaningful difference to only define onTimeout
when req.timeout
is set?
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.
I doubt it will matter in the grand scheme of things, but it definitely wouldn't hurt to only define onTimeout
if needed.
Same goes for onDownProgress
and onUpProgress
.
Howdy, it's been a while indeed. Will look into resolving the conflicts and answering aforementioned questions at my earliest convenience. Probably 1 Feb. Thanks for the message. |
Hi @daftfox - did you manage to find some time to update this PR? |
Http client allows an optional timeout option to be passed. An error will be emitted when no response is received within the allotted interval. This reflects XMLHttpRequest behaviour that is standard in all modern browsers. https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/timeout This is behaviour that has been available for 'regular' js xhr requests, but has not yet been implemented in Angular. Alternatives such as interceptors, RxJS or custom client implementation don't offer the same behaviour in a neat package. fixes angular#34421
Update docs and apply changes requested by @gkalpak
Hey @petebacondarwin. Sorry, it has been a while. |
Looks like there is a legitimate compilation error:
|
@daftfox - are you able to clean up the failures and finish dealing with the comments on this PR? |
(Closing due to no response) |
I think I might be willing to pick this up if that's OK, not sure if there are any issues with that (it looks like the CLA has been signed). I can see what bandwidth I have to pick this up and open a new PR, but I would like to know if that would be OK. I've been watching this PR for a bit but was also waiting for a response and then was busy coming upon the end of the year. |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
@terencehonles Feel free to pick this up and create a new PR with the fixes addressed. We'd love to land it. If you could mention the related issue #34421 in the PR, that'd be great. |
Http client allows an optional timeout option to be passed.
An error will be emitted when no response is received within the allotted interval.
This reflects XMLHttpRequest behaviour that is standard in all modern browsers.
https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/timeout
This is behaviour that has been available for 'regular' js xhr requests, but has
not yet been implemented in Angular. Alternatives such as interceptors, RxJS
or custom client implementation don't offer the same behaviour in a neat package.
fixes #34421
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
The HttpClient does not allow developers to pass in a timeout option.
Issue Number: #34421
What is the new behavior?
A timeout option can be passed when creating an Http request. When no response has been received before the time runs out, the observer's error method is called.
Does this PR introduce a breaking change?
Other information
Docs and tests have been added or updated where applicable. In addition, the payload-size limit for the aio main-es2015.js has been increased by 150 bytes to allow for the slight increase in size that happened due to the newly added feature.