Skip to content

Commit

Permalink
feat: proxy strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
DIYgod committed Jul 12, 2024
1 parent b1a14ea commit 99bf04c
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ export type Config = {
port?: string;
auth?: string;
url_regex: string;
strategy: 'on_retry' | 'all';
};
proxyStrategy: string;
pacUri?: string;
pacScript?: string;
accessKey?: string;
Expand Down Expand Up @@ -399,8 +399,8 @@ const calculateValue = () => {
port: envs.PROXY_PORT,
auth: envs.PROXY_AUTH,
url_regex: envs.PROXY_URL_REGEX || '.*',
strategy: envs.PROXY_STRATEGY || 'all', // all / on_retry
},
proxyStrategy: envs.PROXY_STRATEGY || 'all', // all / on_retry
pacUri: envs.PAC_URI,
pacScript: envs.PAC_SCRIPT,
// access control
Expand Down
2 changes: 1 addition & 1 deletion lib/routes/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ async function handler(ctx) {
if (ctx.req.param('id') === 'httperror') {
await got({
method: 'get',
url: 'https://httpbingo.org/status/404',
url: 'https://httpbingo.org/status/429',
});
}
if (ctx.req.param('id') === 'config-not-found-error') {
Expand Down
9 changes: 9 additions & 0 deletions lib/utils/ofetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ const rofetch = createFetch().create({
retry: config.requestRetry,
retryDelay: 1000,
// timeout: config.requestTimeout,
onResponseError({ request, options }) {
if (options.retry) {
logger.warn(`Request ${request} remaining retry attempts: ${options.retry}`);
if (!options.headers) {
options.headers = {};
}
options.headers['x-retry'] = options.retry;
}
},
onRequestError({ request, error }) {
logger.error(`Request ${request} fail: ${error}`);
},
Expand Down
8 changes: 7 additions & 1 deletion lib/utils/request-rewriter/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,14 @@ const wrappedFetch: typeof undici.fetch = async (input: RequestInfo, init?: Requ
}
}

let isRetry = false;
if (request.headers.get('x-retry')) {
isRetry = true;
request.headers.delete('x-retry');
}

// proxy
if (!options.dispatcher && proxy.dispatcher) {
if (!options.dispatcher && proxy.dispatcher && (proxy.proxyObj.strategy !== 'on_retry' || isRetry)) {
const proxyRegex = new RegExp(proxy.proxyObj.url_regex);
let urlHandler;
try {
Expand Down

0 comments on commit 99bf04c

Please sign in to comment.