Merge pull request #1226 from omnivore-app/fix/close-redis-conn

fix/close redis conn
This commit is contained in:
Jackson Harper
2022-09-22 15:01:03 +08:00
committed by GitHub
2 changed files with 16 additions and 4 deletions

View File

@ -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) {
@ -229,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')
}
}
)

View File

@ -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
},
},
})