@@ -4,7 +4,7 @@ import { Arrays, Objects } from 'cafe-utility'
44import { IncomingMessage , ServerResponse , createServer } from 'http'
55import fetch from 'node-fetch'
66import { metrics } from './metrics'
7- import { Target , getHealthyTarget } from './target'
7+ import { Target , getHealthyTarget , markTargetAsUnhealthy } from './target'
88import { RequestContext , ResponseContext } from './types'
99import { fetchWithTimeout , respondWithFetchPromise } from './utility'
1010
@@ -21,10 +21,11 @@ function main() {
2121 const port = Arrays . getNumberArgument ( process . argv , 'port' , process . env , PORT_ENV ) || DEFAULT_PORT
2222 const target = Arrays . getArgument ( process . argv , 'target' , process . env , TARGET_ENV ) || DEFAULT_TARGET
2323 const expiry = Arrays . getNumberArgument ( process . argv , 'expiry' , process . env , EXPIRY_ENV ) || DEFAULT_EXPIRY
24- const fastIndex = Objects . createFastIndex ( )
24+ const cache = new Map < string , { promise : Promise < ResponseContext | null > , expiry : number } > ( )
2525 const targets : Target [ ] = target . split ( ',' ) . map ( x => ( {
2626 url : x ,
27- lastErrorAt : 0
27+ lastErrorAt : 0 ,
28+ lastUsedAt : 0
2829 } ) )
2930 const server = createServer ( async ( request : IncomingMessage , response : ServerResponse ) => {
3031 request . on ( 'error' , error => {
@@ -37,12 +38,12 @@ function main() {
3738 for ( let i = 0 ; i < targets . length ; i ++ ) {
3839 const target = getHealthyTarget ( targets )
3940 try {
40- await fetch ( target . url , { timeout : 10_000 } )
41+ await fetch ( target . url )
4142 response . statusCode = 200
4243 response . end ( `200 OK - ${ metrics . requests } requests served` )
4344 return
4445 } catch ( error ) {
45- target . lastErrorAt = Date . now ( )
46+ markTargetAsUnhealthy ( targets , target . url )
4647 console . error ( error )
4748 }
4849 }
@@ -69,17 +70,15 @@ function main() {
6970 delete parsedBody . id
7071 metrics . requests ++
7172 const key = `${ target . url } _${ JSON . stringify ( parsedBody ) } `
72- const cachedPromise = Objects . getFromFastIndexWithExpiracy (
73- fastIndex ,
74- key
75- ) as Promise < ResponseContext >
73+ const cached = cache . get ( key )
74+ const cachedPromise = cached && cached . expiry > Date . now ( ) ? cached . promise : null
7675 if ( cachedPromise ) {
7776 process . stdout . write ( `Cache hit: ${ key } \n` )
7877 const successful = await respondWithFetchPromise ( id , response , cachedPromise )
7978 if ( successful ) {
8079 return
8180 } else {
82- target . lastErrorAt = Date . now ( )
81+ markTargetAsUnhealthy ( targets , target . url )
8382 continue
8483 }
8584 }
@@ -92,16 +91,16 @@ function main() {
9291 headers : context . headers ,
9392 body : context . body
9493 } )
95- Objects . pushToFastIndexWithExpiracy ( fastIndex as any , key , responsePromise , expiry )
94+ cache . set ( key , { promise : responsePromise , expiry : Date . now ( ) + expiry } )
9695 const successful = await respondWithFetchPromise ( id , response , responsePromise )
9796 if ( successful ) {
9897 return
9998 } else {
100- target . lastErrorAt = Date . now ( )
99+ markTargetAsUnhealthy ( targets , target . url )
101100 continue
102101 }
103102 } catch ( error ) {
104- target . lastErrorAt = Date . now ( )
103+ markTargetAsUnhealthy ( targets , target . url )
105104 console . error ( error )
106105 }
107106 }
0 commit comments