From a20a7e55acf44bef5f1baa12c892ddffcd1e74fe Mon Sep 17 00:00:00 2001 From: Hongbo Wu Date: Thu, 22 Sep 2022 13:00:58 +0800 Subject: [PATCH 1/3] Close redis connection after execution of cloud function --- packages/text-to-speech/src/index.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/text-to-speech/src/index.ts b/packages/text-to-speech/src/index.ts index a25af17bf..ec8af2550 100644 --- a/packages/text-to-speech/src/index.ts +++ b/packages/text-to-speech/src/index.ts @@ -221,6 +221,9 @@ export const textToSpeechStreamingHandler = Sentry.GCPFunction.wrapHttpFunction( ) console.log('Cache saved') + await redisClient.quit() + console.log('Redis Client Disconnected') + res.send({ idx: utteranceInput.idx, audioData: audioDataString, From b17fb50c3b7b70d11045821110e91f642f6996a7 Mon Sep 17 00:00:00 2001 From: Hongbo Wu Date: Thu, 22 Sep 2022 13:12:36 +0800 Subject: [PATCH 2/3] Close redis connection if got error --- packages/text-to-speech/src/index.ts | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/packages/text-to-speech/src/index.ts b/packages/text-to-speech/src/index.ts index ec8af2550..1bb8ff43e 100644 --- a/packages/text-to-speech/src/index.ts +++ b/packages/text-to-speech/src/index.ts @@ -169,6 +169,12 @@ export const textToSpeechStreamingHandler = Sentry.GCPFunction.wrapHttpFunction( return res.status(401).send({ errorCode: 'UNAUTHENTICATED' }) } + // create redis client + const redisClient = await createRedisClient( + process.env.REDIS_URL, + process.env.REDIS_CERT + ) + try { const utteranceInput = req.body as UtteranceInput const ssmlOptions = { @@ -181,10 +187,6 @@ export const textToSpeechStreamingHandler = Sentry.GCPFunction.wrapHttpFunction( const ssml = `${startSsml(ssmlOptions)}${utteranceInput.text}${endSsml()}` // hash ssml to get the cache key const cacheKey = crypto.createHash('md5').update(ssml).digest('hex') - const redisClient = await createRedisClient( - process.env.REDIS_URL, - process.env.REDIS_CERT - ) // find audio data in cache const cacheResult = await redisClient.get(cacheKey) if (cacheResult) { @@ -221,9 +223,6 @@ export const textToSpeechStreamingHandler = Sentry.GCPFunction.wrapHttpFunction( ) console.log('Cache saved') - await redisClient.quit() - console.log('Redis Client Disconnected') - res.send({ idx: utteranceInput.idx, audioData: audioDataString, @@ -232,6 +231,9 @@ export const textToSpeechStreamingHandler = Sentry.GCPFunction.wrapHttpFunction( } catch (e) { console.error('Text to speech streaming error:', e) return res.status(500).send({ errorCodes: 'SYNTHESIZER_ERROR' }) + } finally { + await redisClient.quit() + console.log('Redis Client Disconnected') } } ) From d3ad5525e37fffa9f1b1c53ba170218bede68bf9 Mon Sep 17 00:00:00 2001 From: Hongbo Wu Date: Thu, 22 Sep 2022 13:30:36 +0800 Subject: [PATCH 3/3] Add 10s connection timeout and retry 10 times max --- packages/text-to-speech/src/redis.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/packages/text-to-speech/src/redis.ts b/packages/text-to-speech/src/redis.ts index 067c78627..350cb09f6 100644 --- a/packages/text-to-speech/src/redis.ts +++ b/packages/text-to-speech/src/redis.ts @@ -7,6 +7,13 @@ export const createRedisClient = async (url?: string, cert?: string) => { tls: url?.startsWith('rediss://'), // rediss:// is the protocol for TLS cert: cert?.replace(/\\n/g, '\n'), // replace \n with new line rejectUnauthorized: false, // for self-signed certs + connectTimeout: 10000, // 10 seconds + reconnectStrategy(retries: number): number | Error { + if (retries > 10) { + return new Error('Retries exhausted') + } + return 1000 + }, }, })