Skip to content
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

Support aborting intercepted requests #520

Open
imewish opened this issue Mar 13, 2024 · 6 comments
Open

Support aborting intercepted requests #520

imewish opened this issue Mar 13, 2024 · 6 comments
Labels
DX enhancement New feature or request

Comments

@imewish
Copy link

imewish commented Mar 13, 2024

Hello,

Can a timeout be added to the requests on the interceptor?

The goal is to add a timeout for any intercepted request and modify the request.

@kettanaito
Copy link
Member

Hi, @imewish. Can you give me an example of what you'd use that timeout for? To cancel a request, I presume? Would love to learn more.

@imewish
Copy link
Author

imewish commented Mar 17, 2024

Hey @kettanaito , yes I would like to cancel a request if it's taking more than X Milliseconds. Similar to the timeout specified in AXIOS.

@kettanaito kettanaito changed the title Set Time Out on Requests Support aborting requests after a timeout Mar 17, 2024
@kettanaito
Copy link
Member

This is something I'd love to add too. This is the API I was thinking about, what's your opinion on it?

interceptor.on('request', ({ request, abortController }) => {
  // Abort this intercepted request if it's taking more than 1s.
  setTimeout(() => abortController.abort(), 1000)
})

The problem with this requirement is this: what defines the "taking more than X"? Is it X amount of time as the request is being sent? Before the first byte of the response is received? Before the entire response is received (cannot really cancel afterward)?

Let me know how you'd expect this to behave.

@kettanaito kettanaito added enhancement New feature or request DX labels Mar 17, 2024
@kettanaito kettanaito changed the title Support aborting requests after a timeout Support aborting intercepted requests Mar 17, 2024
@imewish
Copy link
Author

imewish commented Mar 18, 2024

Hey @kettanaito, this looks good. So ideally one should be able to cancel any requests in both cases you mentioned.
for eg, if the client was not able to make the network connection to the target domain, we should be able to cancel it instead of hanging forever. Also if the connection was made successfully and the response is taking too much of time we should be able to cancel it as well.
Im not sure if this is handled differently in the case of fetch when a user specifies the timeout params.

@kettanaito
Copy link
Member

eg, if the client was not able to make the network connection to the target domain

If you don't respond with a mock, the request will happen as-is. The connection shouldn't hang in either case.

Also if the connection was made successfully and the response is taking too much of time we should be able to cancel it as well.

My main question is: how do you describe this in the listener? You don't know which stage of the resolution the request is. The listener is called as soon as the request headers are sent. So you can abort while the request body is streaming, once it's done streaming, once it's waiting for the response. The problem is, you cannot differentiate between any of those in the listener because there's no way of knowing that.

Do you see my concern here? For you, the user of the library, it's a bad experience.

@imewish
Copy link
Author

imewish commented Mar 19, 2024

Yeah, I got your concern. So if we cannot differentiate b/w request and response timeout, a generic implementation like you have mentioned here #520 (comment) should be ok.

So the main use case for me here is we want to abort all the HTTP calls that takes more than X ms, that are used in the code behind our APIs, the API calls we make using axios or fetch, we can set our own timeout config with axios/fetch instances. But, the calls are made behind the scenes of other libraries/SDKs we don't have control. When we were experiencing high latencies in our APIs, and had no clue what was causing the delay, using this library I was able to trace most of the calls made by these libraries. So this thought came in, if we can trace/log these calls, adding timeout/abort config to it will be very useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
DX enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants