@@ -19,6 +19,7 @@ const CONFIG = {
19
19
CONNECTION_POOL_SIZE : 10 ,
20
20
HEALTH_CHECK_TIMEOUT : 5000 , // 5 seconds for health check
21
21
MAX_HEALTH_CHECK_FAILURES : 3 , // Max consecutive failures before re-resolution
22
+ GRAPHQL_TIMEOUT : 10000 , // 10 seconds for GraphQL requests before considering endpoint unhealthy
22
23
} as const ;
23
24
24
25
const STORE_META_ENVELOPE = `
@@ -389,6 +390,35 @@ export class EVaultClient {
389
390
console . log ( `Removed cached client for ${ w3id } ` ) ;
390
391
}
391
392
393
+ /**
394
+ * Wrapper for GraphQL requests with timeout handling
395
+ */
396
+ private async withTimeout < T > (
397
+ w3id : string ,
398
+ operation : ( ) => Promise < T >
399
+ ) : Promise < T > {
400
+ const controller = new AbortController ( ) ;
401
+ const timeoutId = setTimeout ( ( ) => {
402
+ controller . abort ( ) ;
403
+ console . log ( `GraphQL request timeout for ${ w3id } , marking endpoint as unhealthy` ) ;
404
+ this . removeCachedClient ( w3id ) ;
405
+ } , CONFIG . GRAPHQL_TIMEOUT ) ;
406
+
407
+ try {
408
+ const result = await operation ( ) ;
409
+ clearTimeout ( timeoutId ) ;
410
+ return result ;
411
+ } catch ( error ) {
412
+ clearTimeout ( timeoutId ) ;
413
+
414
+ if ( error instanceof Error && error . name === 'AbortError' ) {
415
+ throw new Error ( `Request timeout after ${ CONFIG . GRAPHQL_TIMEOUT } ms` ) ;
416
+ }
417
+
418
+ throw error ;
419
+ }
420
+ }
421
+
392
422
/**
393
423
* Manually trigger a health check for a specific w3id
394
424
* Useful for testing or forcing re-resolution
@@ -466,15 +496,15 @@ export class EVaultClient {
466
496
console . log ( "sending to eVault: " , envelope . w3id )
467
497
console . log ( "sending payload" , envelope ) ;
468
498
469
- const response = await client
470
- . request < StoreMetaEnvelopeResponse > ( STORE_META_ENVELOPE , {
499
+ const response = await this . withTimeout ( envelope . w3id , ( ) =>
500
+ client . request < StoreMetaEnvelopeResponse > ( STORE_META_ENVELOPE , {
471
501
input : {
472
502
ontology : envelope . schemaId ,
473
503
payload : envelope . data ,
474
504
acl : [ "*" ] ,
475
505
} ,
476
506
} )
477
- . catch ( ( ) => null ) ;
507
+ ) . catch ( ( ) => null ) ;
478
508
479
509
if ( ! response ) return v4 ( ) ;
480
510
return response . storeMetaEnvelope . metaEnvelope . id ;
0 commit comments