@@ -18,6 +18,31 @@ const ENGINES = {
1818 catvod,
1919} ;
2020
21+ // 创建带超时的Promise包装函数
22+ function withTimeout ( promise , timeoutMs = null , operation = 'API操作' , invokeMethod = null ) {
23+ let defaultTimeout ;
24+
25+ // 根据invokeMethod确定超时时间
26+ if ( invokeMethod === 'action' ) {
27+ // action接口使用专用超时时间,默认60秒
28+ defaultTimeout = parseInt ( process . env . API_ACTION_TIMEOUT || '60' ) * 1000 ;
29+ } else {
30+ // 其他接口使用默认超时时间,默认20秒
31+ defaultTimeout = parseInt ( process . env . API_TIMEOUT || '20' ) * 1000 ;
32+ }
33+
34+ const actualTimeout = timeoutMs || defaultTimeout ;
35+
36+ return Promise . race ( [
37+ promise ,
38+ new Promise ( ( _ , reject ) => {
39+ setTimeout ( ( ) => {
40+ reject ( new Error ( `${ operation } 超时 (${ actualTimeout } ms)` ) ) ;
41+ } , actualTimeout ) ;
42+ } )
43+ ] ) ;
44+ }
45+
2146export default ( fastify , options , done ) => {
2247 // 启动JSON监听
2348 startJsonWatcher ( ENGINES , options . jsonDir ) ;
@@ -54,7 +79,7 @@ export default (fastify, options, done) => {
5479
5580 // console.log(`proxyUrl:${proxyUrl}`);
5681 function getEnv ( moduleName ) {
57- const proxyUrl = `${ protocol } ://${ hostname } /proxy/${ moduleName } /?do=${ query . do || 'ds' } &extend=${ encodeURIComponent ( moduleExt ) } ` ;
82+ const proxyUrl = `${ protocol } ://${ hostname } /proxy/${ moduleName } /?do=${ query . do || 'ds' } &extend=${ encodeURIComponent ( moduleExt ) } ` ;
5883 const getProxyUrl = function ( ) {
5984 return proxyUrl
6085 } ;
@@ -79,7 +104,11 @@ export default (fastify, options, done) => {
79104 return null ;
80105 }
81106 const _env = getEnv ( _moduleName ) ;
82- const RULE = await apiEngine . getRule ( _modulePath , _env ) ;
107+ const RULE = await withTimeout (
108+ apiEngine . getRule ( _modulePath , _env ) ,
109+ null ,
110+ `获取规则[${ _moduleName } ]`
111+ ) ;
83112 RULE . callRuleFn = async function ( _method , _args ) {
84113 let invokeMethod = null ;
85114 switch ( _method ) {
@@ -112,10 +141,19 @@ export default (fastify, options, done) => {
112141 if ( typeof RULE [ _method ] !== 'function' ) {
113142 return null
114143 } else {
115- return await RULE [ _method ]
144+ return await withTimeout (
145+ RULE [ _method ] ,
146+ null ,
147+ `规则方法[${ _method } ]`
148+ )
116149 }
117150 }
118- return await apiEngine [ invokeMethod ] ( _modulePath , _env , ..._args )
151+ return await withTimeout (
152+ apiEngine [ invokeMethod ] ( _modulePath , _env , ..._args ) ,
153+ null ,
154+ `规则调用[${ _method } ]` ,
155+ invokeMethod
156+ )
119157 } ;
120158 return RULE
121159 } ;
@@ -125,7 +163,11 @@ export default (fastify, options, done) => {
125163 if ( 'play' in query ) {
126164 // 处理播放逻辑
127165 // console.log('play query:', query);
128- const result = await apiEngine . play ( modulePath , env , query . flag , query . play ) ;
166+ const result = await withTimeout (
167+ apiEngine . play ( modulePath , env , query . flag , query . play ) ,
168+ null ,
169+ `播放接口[${ moduleName } ]`
170+ ) ;
129171 return reply . send ( result ) ;
130172 }
131173
@@ -141,7 +183,11 @@ export default (fastify, options, done) => {
141183 }
142184 }
143185 // 分类逻辑
144- const result = await apiEngine . category ( modulePath , env , query . t , pg , 1 , extend ) ;
186+ const result = await withTimeout (
187+ apiEngine . category ( modulePath , env , query . t , pg , 1 , extend ) ,
188+ null ,
189+ `分类接口[${ moduleName } ]`
190+ ) ;
145191 return reply . send ( result ) ;
146192 }
147193
@@ -150,36 +196,61 @@ export default (fastify, options, done) => {
150196 fastify . log . info ( `[${ moduleName } ] 二级已接收post数据: ${ query . ids } ` ) ;
151197 }
152198 // 详情逻辑
153- const result = await apiEngine . detail ( modulePath , env , query . ids . split ( ',' ) ) ;
199+ const result = await withTimeout (
200+ apiEngine . detail ( modulePath , env , query . ids . split ( ',' ) ) ,
201+ null ,
202+ `详情接口[${ moduleName } ]`
203+ ) ;
154204 return reply . send ( result ) ;
155205 }
156206
157207 if ( 'ac' in query && 'action' in query ) {
158208 // 处理动作逻辑
159- const result = await apiEngine . action ( modulePath , env , query . action , query . value ) ;
209+ const result = await withTimeout (
210+ apiEngine . action ( modulePath , env , query . action , query . value ) ,
211+ null ,
212+ `动作接口[${ moduleName } ]` ,
213+ 'action'
214+ ) ;
160215 return reply . send ( result ) ;
161216 }
162217
163218
164219 if ( 'wd' in query ) {
165220 // 搜索逻辑
166221 const quick = 'quick' in query ? query . quick : 0 ;
167- const result = await apiEngine . search ( modulePath , env , query . wd , quick , pg ) ;
222+ const result = await withTimeout (
223+ apiEngine . search ( modulePath , env , query . wd , quick , pg ) ,
224+ null ,
225+ `搜索接口[${ moduleName } ]`
226+ ) ;
168227 return reply . send ( result ) ;
169228 }
170229
171230 if ( 'refresh' in query ) {
172231 // 强制刷新初始化逻辑
173- const refreshedObject = await apiEngine . init ( modulePath , env , true ) ;
232+ const refreshedObject = await withTimeout (
233+ apiEngine . init ( modulePath , env , true ) ,
234+ null ,
235+ `初始化接口[${ moduleName } ]`
236+ ) ;
174237 return reply . send ( refreshedObject ) ;
175238 }
176239 if ( ! ( 'filter' in query ) ) {
177240 query . filter = 1
178241 }
179242 // 默认逻辑,返回 home + homeVod 接口
180243 const filter = 'filter' in query ? query . filter : 1 ;
181- const resultHome = await apiEngine . home ( modulePath , env , filter ) ;
182- const resultHomeVod = await apiEngine . homeVod ( modulePath , env ) ;
244+ const resultHome = await withTimeout (
245+ apiEngine . home ( modulePath , env , filter ) ,
246+ null ,
247+ `首页接口[${ moduleName } ]`
248+ ) ;
249+ const resultHomeVod = await withTimeout (
250+ apiEngine . homeVod ( modulePath , env ) ,
251+ null ,
252+ `推荐接口[${ moduleName } ]`
253+ ) ;
183254 let result = {
184255 ...resultHome ,
185256 // list: resultHomeVod,
@@ -227,7 +298,7 @@ export default (fastify, options, done) => {
227298 const fServer = fastify . server ;
228299
229300 function getEnv ( moduleName ) {
230- const proxyUrl = `${ protocol } ://${ hostname } /proxy/${ moduleName } /?do=${ query . do || 'ds' } &extend=${ encodeURIComponent ( moduleExt ) } ` ;
301+ const proxyUrl = `${ protocol } ://${ hostname } /proxy/${ moduleName } /?do=${ query . do || 'ds' } &extend=${ encodeURIComponent ( moduleExt ) } ` ;
231302 const getProxyUrl = function ( ) {
232303 return proxyUrl
233304 } ;
@@ -248,7 +319,11 @@ export default (fastify, options, done) => {
248319
249320 const env = getEnv ( moduleName ) ;
250321 try {
251- const backRespList = await apiEngine . proxy ( modulePath , env , query ) ;
322+ const backRespList = await withTimeout (
323+ apiEngine . proxy ( modulePath , env , query ) ,
324+ null ,
325+ `代理接口[${ moduleName } ]`
326+ ) ;
252327 const statusCode = backRespList [ 0 ] ;
253328 const mediaType = backRespList [ 1 ] || 'application/octet-stream' ;
254329 let content = backRespList [ 2 ] || '' ;
@@ -332,7 +407,7 @@ export default (fastify, options, done) => {
332407 const fServer = fastify . server ;
333408
334409 function getEnv ( moduleName ) {
335- const proxyUrl = `${ protocol } ://${ hostname } ${ request . url } ` . split ( '?' ) [ 0 ] . replace ( '/parse/' , '/proxy/' ) + `/?do=${ query . do || "ds" } &extend=${ encodeURIComponent ( moduleExt ) } ` ;
410+ const proxyUrl = `${ protocol } ://${ hostname } ${ request . url } ` . split ( '?' ) [ 0 ] . replace ( '/parse/' , '/proxy/' ) + `/?do=${ query . do || "ds" } &extend=${ encodeURIComponent ( moduleExt ) } ` ;
336411 const getProxyUrl = function ( ) {
337412 return proxyUrl
338413 } ;
@@ -352,7 +427,11 @@ export default (fastify, options, done) => {
352427
353428 const env = getEnv ( '' ) ;
354429 try {
355- const backResp = await drpyS . jx ( jxPath , env , query ) ;
430+ const backResp = await withTimeout (
431+ drpyS . jx ( jxPath , env , query ) ,
432+ null ,
433+ `解析接口[${ jxName } ]`
434+ ) ;
356435 const statusCode = 200 ;
357436 const mediaType = 'application/json; charset=utf-8' ;
358437 if ( typeof backResp === 'object' ) {
0 commit comments