-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Add QueuedInterceptor instead of interceptors.lock #1308
Comments
It works for me! 💯 |
Why do you deprecate locks? Could you provide example of token refreshment? In that case you need lock only for one part of the interceptor. (In the case where you repeat request) |
Could you provide migration guide for |
|
@RumoneAnderson So by simply removing and replacing |
Being a guarantee that it will work for you, not sure. However, I have tested it and it works as expected. |
How to clear a QueuedInterceptor's queue for cases when auth token unable to be refreshed? |
We use the locks to deal with expired tokens and I'm not too comfortable changing this without good documentation. Could someone please provide a concrete migration example for this? |
this case cannot be handled currently with QueuedInterceptor. before with could clear unlock QueuedInterceptor holds ques internally no way to reset. after token refresh failed interceptor becames inactive no error or result and we getting loading with no result. |
with this new changes. what is the solution for refreshing tokens ? |
A example for refreshing tokens with QueuedInterceptor : https://github.com/flutterchina/dio/blob/develop/example/lib/queued_interceptor_crsftoken.dart |
How do I lock/unlock the requests? |
It looks not so good to me, in my scenario. I am using Since the token could be expired at any time, I have to check every response of the first But it isn't so easy to me when using a |
I'd like to know this as well. My code contains multiple dio instances, some of them are set up differently. At the moment I can decide which ones to lock/unlock etc. |
Is sample auth interceptor https://gist.github.com/TimurMukhortov/a1c9819e3779015e54bc3964b7d2308a for dio. its work for me. |
There is a problem with this implementation code, if an error is reported when |
I don't understand the need to deprecate the For e.g. There might be repeated duplicate requests to the same endpoint, while the token is being refreshed. All of them get inserted in the queue, ready to run after the token is refreshed. However, after it is done, I might only want to execute the original request once, with the new token, and remove all the other duplicate requests. How is it possible to achieve this without clearing the queue? |
using with legacy it work properly but with QueuedInterceptorsWrapper not. the problem is at line 428 if (!taskQueue.processing) { return true but taskQueue.queue is empty it get stucks. |
@fullfash I have create a PR(#1457 ) to fix the queued_interceptor_crsftoken bug. The key point to note about using |
@ipcjs but same situation can be happen on second dio instance also. |
Don't add a |
yes but wondering if there can be any leaking case. 🤔 anyway thanks . we will use tokenDio for repeat request also |
|
@fullflash you are right. maybe we need add a |
@ipcjs this is a good workaround. |
@ipcjs i came we this patch: to keep active processing que and force complete when same request path and data repeats.
this bypasses your test. |
To modify the |
How to clear the queue? I got refreshing token happening 5 times to refresh the token. Is there a way to clear the queue? |
Thank @ipcjs for your suggestion. About this sample https://github.com/flutterchina/dio/blob/dfe102d5bed4da72e8f77f8f67395ba2b7d47b40/example/lib/queued_interceptor_crsftoken.dart If you use another Dio instance to repeat the request (line 107 and line 83), everything should be working. For this new Dio instance, don't forget to add your QueuedInterceptorsWrapper interceptor too. Dio library does not seem to be maintained anymore ... does it? |
unfortunately it looks abandoned. Zead Is Dead Baby |
Can an explanation be provided for why locks have become deprecated? I'm all for adding new features users want, but why remove the other one? As I see it, those are vastly different alternatives with pros and cons of their own. So the user should have the freedom of choice. I've recently started implementing the token refresh logic in my application, and was glad to see the locking mechanism. But then, I was saddened to see it became deprecated, without an explanation on why. While I agree that the logic is more complex using the locking mechanism than using the queue mechanism, but I think it should be kept for the users who want to use it. |
What is the alternative of requestLock |
same here, any solution? create new instance is not a good practice! |
Using this interceptor prevents you from being able to make parralel calls (because it serialises them)? How is this better than the lock/unlock technique which blocks the queue ONLY when a token refresh is in progress (for example)? |
Only Very simplified it looks something like the following, only when bool isTokenValid = true;
QueuedInterceptorsWrapper(
onRequest: (
RequestOptions requestOptions,
RequestInterceptorHandler handler,
) {
if (!isTokenValid) {
await refreshToken();
isTokenValid = true;
}
handler.next(requestOptions);
},
); |
@kuhnroyal Interesting, thank you. I was wrong then. |
Thanks, Queued Interceptors solve my problems, now I can handle my refresh and access token very efficiently. The best part of Queued Interceptors is this, it retries automatically the failed requests. |
The perfect solution with example, thanks a lot, you saved my day !! |
does anyone have working example? I cant get the refreshToken to run only once
}` |
same issue here, the refreshing runs more than once... |
Same issue, I still use mutex to solve this case for refresh token and still use Interceptor instead of QueuedInterceptor. |
What is
QueuedInterceptor
?As we all know, we can intercept requests/responses/errors before they are handled by
then
orcatchError
withdio
Interceptor
。In previous versions, interceptors could be executed concurrently, that is, all of the requests enter the interceptor at once, rather than executing sequentially. However, in some cases we expect that requests enter the interceptor sequentially like #590 。 Therefore, we need to provide a mechanism for sequential access(one by one) to interceptors andQueuedInterceptor
is proposed to solve this problem.Examples
We make 3 concurrent requests, all of which enter the interceptor at the same time when using
Interceptor
; But when usingQueuedInterceptor
, requests enter the interceptor sequentially.Of course, you can implement your own queued interceptor directly by inheriting from
QueuedInterceptor
.Delete
Lock
s of interceptorsLocks of interceptors were originally designed to synchronize interceptor execution, but locks have a problem that once it becomes unlocked all of the requests run at once, rather than executing sequentially. Now
QueuedInterceptor
can do it better.To delete
Lock
another reason is thatLock
is shared between the interceptors, so Lock can be called by any interceptors at any place, it will bring potential risks, assuming a interceptor invokeslock
, but because the code defects, lead to can't normal callunLock
, this will affect the rest of the interceptor to normal work (may never be executed to).How to try
QueuedInterceptor
.Use
4.0.2-beta1
or change todevelop
branch.The text was updated successfully, but these errors were encountered: