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

Pool Manager: limit, pause and cancel requests #96

Closed
wants to merge 28 commits into from

Conversation

vixez
Copy link
Contributor

@vixez vixez commented Oct 28, 2021

This is a PR related to #92

The goal is to limit the amount of simultaneous requests, and pause requests when a token is being updated.
It is also possible to cancel active requests.

PoolManager can handle both of these.
I have added an explanation to README.md and updated CHANGELOG.md as well.

Pool Manager

Using a PoolManager allows you to set the maximum amount of simultaneous requests, and pause
requests during token updates.

Create a PoolManager and assign it to an InterceptedClient:

final PoolManager poolManager = PoolManager(    
  maxActiveConnections: 8, 
);  
  
InterceptedClient.build(    
  poolManager: poolManager, 
);  

In the above example only 8 requests can be active at once. By default this is 32.

Skip the pool

It is possible to skip a pool that is active, on hold, or blocked by a token update.
This can be useful to, for example, update a token in a RetryPolicy while the pool is on hold.
If the InterceptedClient.kSkipPoolHeader header is found in the request the pool will be skipped, so the request is executed immediately.

To your headers, add for example:

headers[InterceptedClient.kSkipPoolHeader] = 'true';

The value of the header does not matter and the InterceptedClient.kSkipPoolHeader is filtered out of the request before being sent to the server.

Refreshing a token

If you use a RetryPolicy you can use the PoolManager to put new requests on hold until
the RetryPolicy has completed. Use requestUpdateToken to let the PoolManager know you want to
update the token and put new requests on hold. Once you have updated the token,
use releaseUpdateToken to resume requests.

Be sure to add logic (through an interceptor) to update queued requests with the new token you got
in the RetryPolicy.

poolManager in the example should be a reference to where you stored the PoolManager you
assigned to InterceptedClient.

class TokenRetryPolicy extends RetryPolicy {  
  @override Future<bool> shouldAttemptRetryOnResponse(ResponseData response) async { 
    if (response.statusCode == 401) { 
     // Need to get a new token 
     await poolManager.requestUpdateToken();  
     
     /*  --- YOUR TOKEN REFRESH LOGIC --- */  
     
     poolManager.releaseUpdateToken(); return true; } 
     return false; 
   }
 }  

Cancel active requests

You can cancel all requests that have not yet returned a response. They will return with RequestCancelledException.
This is done through the PoolManager with cancelRequests (optionally releasing token updates with the forceReleaseUpdateToken) parameter.

@vixez vixez mentioned this pull request Oct 28, 2021
@CodingAleCR
Copy link
Owner

Hey, this looks amazing, I will continue testing it out with the new 2.0.0 APIs, thank you for all the hard work you have put into this!

@vixez
Copy link
Contributor Author

vixez commented Nov 15, 2021

You're welcome. Let me know if there is anything I need to change/can do.

vixez added 11 commits November 30, 2021 15:43
# Conflicts:
#	lib/extensions/multipart_request.dart
#	lib/extensions/request.dart
#	lib/extensions/streamed_request.dart
#	lib/http/intercepted_client.dart
#	lib/http/intercepted_http.dart
#	lib/http_interceptor.dart
#	pubspec.yaml
#	test/extensions/base_reponse_test.dart
#	test/extensions/base_request_test.dart
#	test/extensions/request_test.dart
#	test/extensions/response_test.dart
#	test/models/retry_policy_test.dart
@vixez
Copy link
Contributor Author

vixez commented Jan 10, 2022

I have updated this to work with the 2.0.0 beta.

@fnicastri
Copy link

Any update on this @CodingAleCR ? Looking forward to use this on my app(almost ready to be released) app!

Thanks for this awesome lib!

@vixez vixez changed the title Pool Manager: limit and pause requests Pool Manager: limit, pause and cancel requests Feb 7, 2022
@vixez vixez mentioned this pull request Feb 7, 2022
# Conflicts:
#	lib/http/intercepted_client.dart
#	pubspec.yaml
@vixez
Copy link
Contributor Author

vixez commented Feb 7, 2022

Added the ability to cancel all requests that have not yet returned a response

@CodingAleCR
Copy link
Owner

I really like this, but not entirely sure about the implementation. Lately, I've been thinking about adding a special PooledInterceptor or something that you can implement in order to opt-in for the functionality. What would you think about something like this?

1 similar comment
@CodingAleCR
Copy link
Owner

I really like this, but not entirely sure about the implementation. Lately, I've been thinking about adding a special PooledInterceptor or something that you can implement in order to opt-in for the functionality. What would you think about something like this?

@vixez
Copy link
Contributor Author

vixez commented Mar 21, 2022

That could be an option as well, yes 🤔

@LucPham
Copy link

LucPham commented May 13, 2022

waiting a release!!

@stale stale bot added the wontfix This will not be worked on label Jul 12, 2022
@stale stale bot closed this Jul 30, 2022
@fnicastri
Copy link

🤷‍♂️

Repository owner deleted a comment from stale bot Aug 3, 2022
@CodingAleCR CodingAleCR removed the wontfix This will not be worked on label Aug 3, 2022
@CodingAleCR CodingAleCR reopened this Aug 4, 2022
Copy link
Owner

@CodingAleCR CodingAleCR left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've explored other options but none that I like as alternatives, this has the most potential and stability. Would you be willing to update the PR with the latest changes?

@@ -1,6 +1,7 @@
library http_interceptor;

export 'package:http/http.dart';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's best if we expose http package along with this package so we can avoid having to install manually http when using http_interceptor.

@stale stale bot added the wontfix This will not be worked on label Oct 12, 2022
@stale stale bot closed this Oct 20, 2022
@shinayser
Copy link

shinayser commented Jan 27, 2023

Is this feature still will be implemented or its abbandoned?

Repository owner deleted a comment from stale bot Jan 31, 2023
@CodingAleCR
Copy link
Owner

I asked for a couple of updates. When done it should be good to go

@CodingAleCR CodingAleCR reopened this Jan 31, 2023
@stale stale bot removed the wontfix This will not be worked on label Jan 31, 2023
@shinayser
Copy link

@vixez Have you quit this implementation or got any update on this?

@vixez
Copy link
Contributor Author

vixez commented Mar 9, 2023

@shinayser I have rewritten major parts of our API code for the app, making this no longer needed.

@stale
Copy link

stale bot commented May 9, 2023

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix This will not be worked on label May 9, 2023
@stale stale bot closed this May 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request wontfix This will not be worked on
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants