@@ -41,13 +41,13 @@ function createRedisClient() {
4141 // End reconnecting on a specific error and flush all commands with a
4242 // individual error.
4343 if ( options . error . code === "ECONNREFUSED" ) {
44- return new Error ( "The server refused the connection" )
44+ return new Error ( "[Cache] The server refused the connection" )
4545 }
4646 }
4747 // End reconnecting after a specific timeout and flush all commands with a
4848 // individual error.
4949 if ( options . total_retry_time > 1000 * 60 * 60 ) {
50- return new Error ( "Retry time exhausted" )
50+ return new Error ( "[Cache] Retry time exhausted" )
5151 }
5252 // End reconnecting with built in error.
5353 if ( options . attempt > 10 ) {
@@ -62,53 +62,46 @@ function createRedisClient() {
6262 }
6363 client . on ( "error" , error )
6464 VerboseEvents . forEach ( event => {
65- client . on ( event , ( ) => verbose ( `Redis: ${ event } ` ) )
65+ client . on ( event , ( ) => verbose ( `[Cache] ${ event } ` ) )
6666 } )
6767 return client
6868}
6969
7070export const client = isTest ? createMockClient ( ) : createRedisClient ( )
7171
72- // Used to timeout Redis calls
73- // From: https://medium.com/@stockholmux /tick-tock-setting-a-timeout-on-a-redis-call-with-node-js-be63733df98a
74- function timeoutRedisCommand ( command , timeoutValue ) {
75- return function wrappedCommand ( ) {
76- const argsAsArray = Array . prototype . slice . call ( arguments )
77- let callback = argsAsArray . pop ( )
78- const timeoutHandler = setTimeout ( ( ) => {
79- error ( `Cache ${ command } timeout` )
80- callback ( new Error ( `Cache ${ command } timeout` ) )
81- callback = ( ) => { }
82- } , timeoutValue )
83-
84- argsAsArray . push ( ( err , data ) => {
85- clearTimeout ( timeoutHandler )
86- callback ( err , data )
87- } )
88-
89- client [ command ] . apply ( client , argsAsArray )
90- }
91- }
92-
93- const getWithTimeout = timeoutRedisCommand ( "get" , CACHE_RETRIEVAL_TIMEOUT_MS )
94-
9572export default {
9673 get : key => {
9774 return new Promise ( ( resolve , reject ) => {
98- if ( isNull ( client ) ) return reject ( new Error ( "Cache client is `null`" ) )
75+ if ( isNull ( client ) ) return reject ( new Error ( "[Cache] `client` is `null`" ) )
76+
77+ let timeoutId = setTimeout ( ( ) => {
78+ timeoutId = null
79+ const err = new Error ( `[Cache#get] Timeout for key ${ key } ` )
80+ error ( err )
81+ reject ( err )
82+ } , CACHE_RETRIEVAL_TIMEOUT_MS )
83+
9984 const start = Date . now ( )
100- getWithTimeout ( key , ( err , data ) => {
85+ client . get ( key , ( err , data ) => {
10186 const time = Date . now ( ) - start
10287 if ( time > CACHE_QUERY_LOGGING_THRESHOLD_MS ) {
103- error ( `Slow Cache#Get: ${ time } ms key:${ key } ` )
88+ error ( `[Cache#get] Slow read of ${ time } ms for key ${ key } ` )
89+ }
90+
91+ if ( timeoutId ) {
92+ clearTimeout ( timeoutId )
93+ } else {
94+ // timed out and already rejected promise, no need to continue
95+ return
10496 }
97+
10598 if ( err ) {
10699 error ( err )
107100 reject ( err )
108101 } else if ( data ) {
109102 resolve ( JSON . parse ( data ) )
110103 } else {
111- reject ( new Error ( "cache #get did not return `data` " ) )
104+ reject ( new Error ( "[Cache #get] Cache miss " ) )
112105 }
113106 } )
114107 } )
0 commit comments