fix: create only a single redis connection when REDIS_URL is set

This commit is contained in:
Gregor 2019-01-27 10:15:48 -08:00
parent 2a0c1de266
commit 6638331030
2 changed files with 20 additions and 16 deletions

View File

@ -15,6 +15,7 @@ export interface Options {
router?: express.Router
catchErrors?: boolean
githubToken?: string
throttleOptions?: any
}
// Some events can't get an authenticated client (#382):
@ -34,9 +35,9 @@ export class Application {
public cache: Cache
public router: express.Router
public log: LoggerWithTarget
public throttleOptions: any
private githubToken?: string
private throttleOptions: any
constructor (options?: Options) {
const opts = options || {} as any
@ -46,20 +47,7 @@ export class Application {
this.cache = opts.cache
this.router = opts.router || express.Router() // you can do this?
this.githubToken = opts.githubToken
if (process.env.REDIS_URL) {
const Bottleneck = require('bottleneck')
const Redis = require('ioredis')
const client = new Redis(process.env.REDIS_URL)
const connection = new Bottleneck.IORedisConnection({ client })
connection.on('error', this.log.error)
this.throttleOptions = {
Bottleneck,
connection
}
}
this.throttleOptions = opts.throttleOptions
if (opts.catchErrors) {
// Deprecated since 7.2.0

View File

@ -33,6 +33,7 @@ export class Probot {
private apps: Application[]
private app?: OctokitApp
private githubToken?: string
private throttleOptions: any
constructor (options: Options) {
options.webhookPath = options.webhookPath || '/'
@ -64,6 +65,20 @@ export class Probot {
// Log all webhook errors
this.webhook.on('error', this.errorHandler)
if (process.env.REDIS_URL) {
const Bottleneck = require('bottleneck')
const Redis = require('ioredis')
const client = new Redis(process.env.REDIS_URL)
const connection = new Bottleneck.IORedisConnection({ client })
connection.on('error', this.logger.error)
this.throttleOptions = {
Bottleneck,
connection
}
}
}
public errorHandler (err: Error) {
@ -89,7 +104,8 @@ export class Probot {
const app = new Application({
app: this.app as OctokitApp,
cache,
githubToken: this.githubToken
githubToken: this.githubToken,
throttleOptions: this.throttleOptions
})
// Connect the router from the app to the server