http_interceptor icon indicating copy to clipboard operation
http_interceptor copied to clipboard

Pool Manager: limit, pause and cancel requests

Open vixez opened this issue 3 years ago • 12 comments

This is a PR related to https://github.com/CodingAleCR/http_interceptor/issues/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 avatar Oct 28 '21 09:10 vixez

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!

CodingAleCR avatar Nov 14 '21 03:11 CodingAleCR

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

vixez avatar Nov 15 '21 12:11 vixez

I have updated this to work with the 2.0.0 beta.

vixez avatar Jan 10 '22 13:01 vixez

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

Thanks for this awesome lib!

fnicastri avatar Jan 21 '22 14:01 fnicastri

Not just yet, I have been trying to avoid adding significant new features while beta testing the new API that adds support for different types of requests and responses.

However, I do appreciate the patience and the hard work been put in by the different contributors and interested supporters 🎉

CodingAleCR avatar Jan 26 '22 18:01 CodingAleCR

@CodingAleCR @vixez Thanks!

fnicastri avatar Jan 27 '22 09:01 fnicastri

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

vixez avatar Feb 07 '22 11:02 vixez

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?

CodingAleCR avatar Mar 17 '22 16:03 CodingAleCR

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?

CodingAleCR avatar Mar 17 '22 16:03 CodingAleCR

That could be an option as well, yes 🤔

vixez avatar Mar 21 '22 12:03 vixez

waiting a release!!

LucPham avatar May 13 '22 03:05 LucPham

🤷‍♂️

fnicastri avatar Aug 01 '22 18:08 fnicastri

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[bot] avatar Oct 12 '22 08:10 stale[bot]

Is this feature still will be implemented or its abbandoned?

shinayser avatar Jan 27 '23 15:01 shinayser

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

CodingAleCR avatar Jan 31 '23 15:01 CodingAleCR

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

shinayser avatar Jan 31 '23 16:01 shinayser

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

vixez avatar Mar 09 '23 10:03 vixez

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[bot] avatar May 09 '23 01:05 stale[bot]