Skip to content

Commit 7712e6a

Browse files
perf: path的拦截器匹配结果缓存起来,避免每次重复解析
1 parent c1292b1 commit 7712e6a

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

packages/mitmproxy/src/options.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
const fs = require('node:fs')
22
const path = require('node:path')
33
const lodash = require('lodash')
4+
const { LRUCache } = require('lru-cache')
45
const dnsUtil = require('./lib/dns')
56
const interceptorImpls = require('./lib/interceptor')
67
const scriptInterceptor = require('./lib/interceptor/impl/res/script')
78
const { getTmpPacFilePath, downloadPacAsync, createOverwallMiddleware } = require('./lib/proxy/middleware/overwall')
89
const log = require('./utils/util.log.server')
910
const matchUtil = require('./utils/util.match')
1011

12+
// 每个域名的路径级拦截器缓存的最大条数。
13+
// 对于使用 .* 路径模式的域名(如 api.github.com),每个唯一 URL(含不同 query string)都会生成独立的缓存条目。
14+
// 设置上限,超出后清空最久未使用的缓存,防止长期运行时因 API 分页/唯一 token 等导致内存无界增长。
15+
const PATH_CACHE_MAX_SIZE = 512
16+
1117
// 处理拦截配置
1218
function buildIntercepts (intercepts) {
1319
// 自动生成script拦截器所需的辅助配置,降低使用`script拦截器`配置绝对地址和相对地址时的门槛
@@ -153,6 +159,24 @@ module.exports = (serverConfig) => {
153159
return
154160
}
155161

162+
// 获取缓存:同一 hostname+path 的拦截器列表是固定的,不必每次重新构建
163+
// 注:缓存 key 使用完整路径(含 query string),以保证正则捕获组(matched)的正确性
164+
// 注:采用 LRU 淘汰策略,上限为 PATH_CACHE_MAX_SIZE 条;利用 Map 按插入顺序迭代的特性,命中时删除后重新插入以更新位置
165+
if (!interceptOpts._pathCache) {
166+
const cache = new LRUCache({
167+
maxSize: PATH_CACHE_MAX_SIZE,
168+
sizeCalculation: () => {
169+
return 1
170+
},
171+
})
172+
Object.defineProperty(interceptOpts, '_pathCache', { value: cache, enumerable: false, configurable: true })
173+
} else {
174+
const cached = interceptOpts._pathCache.get(rOptions.path)
175+
if (cached) {
176+
return cached
177+
}
178+
}
179+
156180
const matchIntercepts = []
157181
const matchInterceptsOpts = {}
158182
for (const regexp in interceptOpts) { // 遍历拦截配置
@@ -244,6 +268,9 @@ module.exports = (serverConfig) => {
244268
// log.info('interceptor:', interceptor.name, 'priority:', interceptor.priority)
245269
// }
246270

271+
// 设置缓存
272+
interceptOpts._pathCache.set(rOptions.path, matchIntercepts)
273+
247274
return matchIntercepts
248275
},
249276
}

0 commit comments

Comments
 (0)