mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-21 20:47:08 -05:00
[misc] rewrite: free functions
This commit is contained in:
parent
da8bef821b
commit
4aebc1b0b6
2 changed files with 118 additions and 119 deletions
|
@ -6,7 +6,6 @@
|
|||
* DS207: Consider shorter variations of null checks
|
||||
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
||||
*/
|
||||
let RedisSharelatex
|
||||
const _ = require('underscore')
|
||||
const os = require('os')
|
||||
const crypto = require('crypto')
|
||||
|
@ -17,8 +16,7 @@ const PID = process.pid
|
|||
const RND = crypto.randomBytes(4).toString('hex')
|
||||
let COUNT = 0
|
||||
|
||||
module.exports = RedisSharelatex = {
|
||||
createClient(opts) {
|
||||
function createClient(opts) {
|
||||
let client, standardOpts
|
||||
if (opts == null) {
|
||||
opts = { port: 6379, host: 'localhost' }
|
||||
|
@ -33,32 +31,29 @@ module.exports = RedisSharelatex = {
|
|||
delete standardOpts.cluster
|
||||
delete standardOpts.key_schema
|
||||
client = new Redis.Cluster(opts.cluster, standardOpts)
|
||||
client.healthCheck = RedisSharelatex.clusterHealthCheckBuilder(client)
|
||||
RedisSharelatex._monkeyPatchIoredisExec(client)
|
||||
client.healthCheck = clusterHealthCheckBuilder(client)
|
||||
_monkeyPatchIoredisExec(client)
|
||||
} else {
|
||||
standardOpts = _.clone(opts)
|
||||
const ioredis = require('ioredis')
|
||||
client = new ioredis(standardOpts)
|
||||
RedisSharelatex._monkeyPatchIoredisExec(client)
|
||||
client.healthCheck = RedisSharelatex.singleInstanceHealthCheckBuilder(
|
||||
client
|
||||
)
|
||||
_monkeyPatchIoredisExec(client)
|
||||
client.healthCheck = singleInstanceHealthCheckBuilder(client)
|
||||
}
|
||||
return client
|
||||
},
|
||||
}
|
||||
|
||||
HEARTBEAT_TIMEOUT: 2000,
|
||||
singleInstanceHealthCheckBuilder(client) {
|
||||
const healthCheck = (callback) =>
|
||||
RedisSharelatex._checkClient(client, callback)
|
||||
const HEARTBEAT_TIMEOUT = 2000
|
||||
function singleInstanceHealthCheckBuilder(client) {
|
||||
const healthCheck = (callback) => _checkClient(client, callback)
|
||||
return healthCheck
|
||||
},
|
||||
}
|
||||
|
||||
clusterHealthCheckBuilder(client) {
|
||||
return RedisSharelatex.singleInstanceHealthCheckBuilder(client)
|
||||
},
|
||||
function clusterHealthCheckBuilder(client) {
|
||||
return singleInstanceHealthCheckBuilder(client)
|
||||
}
|
||||
|
||||
_checkClient(client, callback) {
|
||||
function _checkClient(client, callback) {
|
||||
callback = _.once(callback)
|
||||
// check the redis connection by storing and retrieving a unique key/value pair
|
||||
const uniqueToken = `host=${HOST}:pid=${PID}:random=${RND}:time=${Date.now()}:count=${COUNT++}`
|
||||
|
@ -79,7 +74,7 @@ module.exports = RedisSharelatex = {
|
|||
'client timed out'
|
||||
)
|
||||
return callback(error)
|
||||
}, RedisSharelatex.HEARTBEAT_TIMEOUT)
|
||||
}, HEARTBEAT_TIMEOUT)
|
||||
const healthCheckKey = `_redis-wrapper:healthCheckKey:{${uniqueToken}}`
|
||||
const healthCheckValue = `_redis-wrapper:healthCheckValue:{${uniqueToken}}`
|
||||
// set the unique key/value pair
|
||||
|
@ -108,9 +103,9 @@ module.exports = RedisSharelatex = {
|
|||
return callback()
|
||||
})
|
||||
})
|
||||
},
|
||||
}
|
||||
|
||||
_monkeyPatchIoredisExec(client) {
|
||||
function _monkeyPatchIoredisExec(client) {
|
||||
const _multi = client.multi
|
||||
return (client.multi = function (...args) {
|
||||
const multi = _multi.call(client, ...Array.from(args))
|
||||
|
@ -140,7 +135,6 @@ module.exports = RedisSharelatex = {
|
|||
}
|
||||
return multi
|
||||
})
|
||||
},
|
||||
}
|
||||
|
||||
function __guard__(value, transform) {
|
||||
|
@ -148,3 +142,7 @@ function __guard__(value, transform) {
|
|||
? transform(value)
|
||||
: undefined
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
createClient,
|
||||
}
|
||||
|
|
|
@ -126,7 +126,8 @@ describe('index', function () {
|
|||
this.results = []
|
||||
this.multiOrig = { exec: sinon.stub().yields(null, this.results) }
|
||||
this.client = { multi: sinon.stub().returns(this.multiOrig) }
|
||||
this.redis._monkeyPatchIoredisExec(this.client)
|
||||
this.ioredisConstructor.returns(this.client)
|
||||
this.redis.createClient(this.client)
|
||||
return (this.multi = this.client.multi())
|
||||
})
|
||||
|
||||
|
|
Loading…
Reference in a new issue