@@ -78,10 +78,11 @@ export function request<T>(redisClient, channel: string, body: any, requestValid
7878}
7979
8080export async function server < T , S > ( redisClient , channel : string , callback : serverCallback < T , S > , serverValidityPeriod : number ) : Promise < void > {
81+ const REDIS_RESPONSE_TTL_IN_SECONDS = 1
8182 const clientClone = redisClient . duplicate ( )
8283 clients . push ( clientClone )
8384
84- function brpop ( ) {
85+ function brpop ( ) {
8586 clientClone . brpop ( channel , 0 , async function ( _null , popInfo ) {
8687 if ( _null ) {
8788 console . error ( 'BRPOP ERROR' , _null )
@@ -100,35 +101,35 @@ export async function server<T, S>(redisClient, channel: string, callback: serve
100101 const req : EpicurusRequest = JSON . parse ( rawRequest )
101102 req . body . channel = channel
102103
103- if ( req . ttl > Date . now ( ) - serverValidityPeriod ) {
104- callback ( req . body , async function ( error , result ) {
105- const errorRef = error
106- ? { name : error . name , message : error . message , stack : error . stack , severity : error . severity || 1 , status : error . status }
107- : null
104+ callback ( req . body , async function ( error , result ) {
105+ const errorRef = error
106+ ? { name : error . name , message : error . message , stack : error . stack , severity : error . severity || 1 , status : error . status }
107+ : null
108108
109- let redisResponse : EpicurusResponse = {
110- error : errorRef ,
111- result : result
112- }
109+ let redisResponse : EpicurusResponse = {
110+ error : errorRef ,
111+ result : result
112+ }
113+
114+ redisClient . lpush ( req . reqId , JSON . stringify ( redisResponse ) )
115+ redisClient . expire ( req . reqId , REDIS_RESPONSE_TTL_IN_SECONDS )
116+ } )
113117
114- redisClient . lpush ( req . reqId , JSON . stringify ( redisResponse ) )
115- } )
116- }
117118 } )
118119 }
119120
120121 brpop ( )
121122}
122123
123- export function disableServers ( ) {
124+ export function disableServers ( ) {
124125 serversEnabled = false
125126 closeAllClients ( )
126127}
127128
128- export function enableServers ( ) {
129+ export function enableServers ( ) {
129130 serversEnabled = true
130131}
131132
132- export function closeAllClients ( ) {
133+ export function closeAllClients ( ) {
133134 clients . forEach ( c => c . end ( false ) )
134135}
0 commit comments