onBeforeRequest 报错时支持走重试逻辑#499
Open
bao-io wants to merge 1 commit into
Open
Conversation
Adds functionality to retry requests when encountering errors during the before-request phase. Introduces methods to determine if a retry should occur based on custom conditions and hooks. Enhances the existing retry mechanism to handle temporary errors effectively, ensuring robust request handling.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
背景
当前
OnBeforeRequest中间件一旦返回 error,Request.do()会直接 return,不会走RetryCondition/RetryHook。重试逻辑只在请求发出之后才会触发。但有些逻辑需要在请求发出前执行(例如获取 token、预检查),失败时同样希望能按
RetryCondition判断是否重试。改动
shouldRetry、prepareRetry、tryRetry,复用请求后的重试逻辑udBeforeRequest报错时调用tryRetry,满足条件则continue retryRetryCondition前先构造resp(含Request和Err),保证 hook 里能拿到resp.Request,即使请求还没发到网络行为说明
udBeforeRequest,内置beforeRequest(如parseRequestURL)报错仍直接返回RetryCondition返回false、MaxRetries耗尽、context.Canceled等行为与原来一致测试
TestRetryOnBeforeRequestErrorTestRetryConditionHasRequestOnBeforeRequestErrorTestNoRetryOnBeforeRequestErrorWhenConditionFalse