Skip to content

Commit fccbc3a

Browse files
committed
1.3.9.04
【优化】 - 移除部分死代码
1 parent 53d4a1d commit fccbc3a

6 files changed

Lines changed: 63 additions & 207 deletions

File tree

src/services/github/RequestBatcher.ts

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { logger, retry } from '@/utils';
2-
import type { RetryOptions } from '@/utils';
1+
import { logger } from '@/utils';
32
import { createTimeWheel } from '@/utils/data-structures/TimeWheel';
43
import type { TimeWheel } from '@/utils/data-structures/TimeWheel';
54

@@ -22,6 +21,17 @@ interface FingerprintData {
2221
hitCount: number;
2322
}
2423

24+
/**
25+
* 重试选项
26+
*/
27+
interface RetryOptions {
28+
maxRetries: number;
29+
backoff: (attempt: number) => number;
30+
shouldRetry?: (error: unknown) => boolean;
31+
onRetry?: (attempt: number, error: unknown) => void;
32+
silent?: boolean;
33+
}
34+
2535
/**
2636
* 请求批处理器类
2737
*
@@ -60,6 +70,55 @@ export class RequestBatcher {
6070
return `${method}:${key}:${headerStr}`;
6171
}
6272

73+
/**
74+
* 重试选项
75+
*/
76+
private withRetry = async <T>(
77+
fn: () => Promise<T>,
78+
options: RetryOptions
79+
): Promise<T> => {
80+
const {
81+
maxRetries,
82+
backoff,
83+
shouldRetry = () => true,
84+
onRetry,
85+
silent = false
86+
} = options;
87+
88+
let lastError: unknown = null;
89+
90+
for (let attempt = 0; attempt <= maxRetries; attempt++) {
91+
try {
92+
const result = await fn();
93+
if (attempt > 0) {
94+
logger.debug(`操作在第 ${(attempt + 1).toString()} 次尝试后成功`);
95+
}
96+
return result;
97+
} catch (error: unknown) {
98+
lastError = error;
99+
100+
if (attempt < maxRetries && shouldRetry(error)) {
101+
const delay = backoff(attempt);
102+
103+
if (onRetry !== undefined) {
104+
onRetry(attempt, error);
105+
}
106+
107+
if (!silent) {
108+
const errorMessage = error instanceof Error ? error.message : String(error);
109+
logger.debug(`重试操作 (尝试 ${(attempt + 1).toString()}/${(maxRetries + 1).toString()}),延迟 ${delay.toString()}ms: ${errorMessage}`);
110+
}
111+
112+
await new Promise(resolve => setTimeout(resolve, delay));
113+
} else {
114+
break;
115+
}
116+
}
117+
}
118+
119+
throw lastError;
120+
};
121+
63122
/**
64123
* 将请求加入批处理队列
65124
*
@@ -191,7 +250,7 @@ export class RequestBatcher {
191250

192251
try {
193252
// 使用通用重试逻辑执行请求
194-
const result = await retry.withRetry(executeRequest, retryOptions);
253+
const result = await this.withRetry(executeRequest, retryOptions);
195254

196255
// 缓存成功的请求结果
197256
if (!skipDeduplication) {

src/services/github/core/Auth.ts

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
11
import { GitHubTokenManager } from '../TokenManager';
2-
import {
3-
markProxyServiceFailed as proxyMarkServiceFailed,
4-
getCurrentProxyService as proxyGetCurrentService,
5-
resetFailedProxyServices as proxyResetFailedServices,
6-
transformImageUrl as proxyTransformImageUrl
7-
} from '../proxy';
82
import { ErrorManager } from '@/utils/error';
93
import type { GitHubError } from '@/types/errors';
104
import { shouldUseServerAPI } from '../config';
@@ -147,46 +141,6 @@ export function handleApiError(error: Response, endpoint: string, method = 'GET'
147141
* @param proxyUrl - 失败的代理服务URL
148142
* @returns void
149143
*/
150-
export function markProxyServiceFailed(proxyUrl: string): void {
151-
proxyMarkServiceFailed(proxyUrl);
152-
}
153-
154-
/**
155-
* 获取当前使用的代理服务
156-
*
157-
* @returns 当前活跃的代理服务URL
158-
*/
159-
export function getCurrentProxyService(): string {
160-
return proxyGetCurrentService();
161-
}
162-
163-
/**
164-
* 重置失败的代理服务记录
165-
*
166-
* 清除所有代理服务的失败标记,允许重新尝试使用。
167-
*
168-
* @returns void
169-
*/
170-
export function resetFailedProxyServices(): void {
171-
proxyResetFailedServices();
172-
}
173-
174-
/**
175-
* 转换相对图片URL为绝对URL
176-
*
177-
* 将Markdown文件中的相对路径图片URL转换为可访问的绝对URL,
178-
* 根据useTokenMode决定是否使用代理服务。
179-
*
180-
* @param src - 原始图片URL(可能是相对路径)
181-
* @param markdownFilePath - Markdown文件的路径
182-
* @param useTokenMode - 是否使用Token模式
183-
* @param branch - 分支名称(可选)
184-
* @returns 转换后的绝对URL,如果输入为undefined则返回undefined
185-
*/
186-
export function transformImageUrl(src: string | undefined, markdownFilePath: string, useTokenMode: boolean, branch?: string): string | undefined {
187-
return proxyTransformImageUrl(src, markdownFilePath, useTokenMode, branch);
188-
}
189-
190144
/**
191145
* 获取 Token 管理器实例
192146
*

src/services/github/core/StatsService.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { CacheManager } from '../cache';
2-
import { getProxyHealthStats } from '../proxy';
3-
import { resetFailedProxyServices } from './Auth';
2+
import { getProxyHealthStats, resetFailedProxyServices } from '../proxy';
43

54
// GitHub统计服务,使用模块导出而非类
65

@@ -46,4 +45,3 @@ export async function getNetworkStats(): Promise<{
4645
cache: getCacheStats()
4746
};
4847
}
49-

src/utils/data-structures/TimeWheel.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -281,15 +281,6 @@ export class TimeWheel<T> {
281281
};
282282
}
283283

284-
/**
285-
* 销毁时间轮
286-
*
287-
* 停止时钟并清空所有数据。
288-
*/
289-
destroy(): void {
290-
this.stop();
291-
this.clear();
292-
}
293284
}
294285

295286
/**
@@ -307,4 +298,3 @@ export function createTimeWheel<T>(options?: {
307298
wheel.start();
308299
return wheel;
309300
}
310-

src/utils/index.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@ export const pdf = {
3939
};
4040

4141

42-
// 重试工具
43-
import * as retryUtils from './retry/retryUtils';
44-
export const retry = retryUtils;
4542

4643
// 请求管理工具
4744
import { requestManager as requestManagerInstance } from './request/requestManager';
@@ -101,7 +98,6 @@ export const performance = {
10198

10299
};
103100

104-
export type { RetryOptions } from './retry/retryUtils';
105101
export type { SmartCacheOptions } from './cache/SmartCache';
106102
export type { RequestOptions } from './request/requestManager';
107103
export type { ErrorHandlerOptions } from './error/errorHandler';

src/utils/retry/retryUtils.ts

Lines changed: 0 additions & 141 deletions
This file was deleted.

0 commit comments

Comments
 (0)