Merge pull request #3761 from overleaf/bg-redis-and-request-cleanups

redis and request cleanups

GitOrigin-RevId: d2bf133ef823c96b06eb09b4ef2c83ba7c54cd12
This commit is contained in:
Brian Gough 2021-03-18 08:39:39 +00:00 committed by Copybot
parent 83a38a9980
commit be95516185
3 changed files with 22 additions and 35 deletions

View file

@ -14,7 +14,7 @@
let rclient_secondary
const OError = require('@overleaf/o-error')
const Settings = require('settings-sharelatex')
const request = require('request')
const request = require('request').defaults({ timeout: 30 * 1000 })
const RedisWrapper = require('../../infrastructure/RedisWrapper')
const rclient = RedisWrapper.client('clsi_cookie')
if (Settings.redis.clsi_cookie_secondary != null) {
@ -114,10 +114,12 @@ module.exports = function(backendGroup) {
if (callback == null) {
callback = function(err) {}
}
const multi = rclient.multi()
multi.set(this.buildKey(project_id), serverId)
multi.expire(this.buildKey(project_id), Settings.clsiCookie.ttl)
return multi.exec(callback)
rclient.setex(
this.buildKey(project_id),
Settings.clsiCookie.ttl,
serverId,
callback
)
},
clearServerId(project_id, callback) {

View file

@ -1,4 +1,4 @@
const request = require('request').defaults()
const request = require('request').defaults({ timeout: 30 * 100 })
const OError = require('@overleaf/o-error')
const settings = require('settings-sharelatex')
const _ = require('underscore')

View file

@ -23,24 +23,19 @@ const realRequst = require('request')
describe('ClsiCookieManager', function() {
beforeEach(function() {
const self = this
this.redisMulti = {
set: sinon.stub(),
get: sinon.stub(),
expire: sinon.stub(),
exec: sinon.stub()
}
this.redis = {
auth() {},
get: sinon.stub(),
multi() {
return self.redisMulti
}
setex: sinon.stub().callsArg(3)
}
this.project_id = '123423431321'
this.request = {
get: sinon.stub(),
cookie: realRequst.cookie,
jar: realRequst.jar
jar: realRequst.jar,
defaults: () => {
return this.request
}
}
this.settings = {
redis: {
@ -163,7 +158,6 @@ describe('ClsiCookieManager', function() {
this.ClsiCookieManager._parseServerIdFromResponse = sinon
.stub()
.returns('clsi-8')
return this.redisMulti.exec.callsArgWith(0)
})
it('should set the server id with a ttl', function(done) {
@ -171,13 +165,11 @@ describe('ClsiCookieManager', function() {
this.project_id,
this.response,
err => {
this.redisMulti.set
.calledWith(`clsiserver:${this.project_id}`, 'clsi-8')
.should.equal(true)
this.redisMulti.expire
this.redis.setex
.calledWith(
`clsiserver:${this.project_id}`,
this.settings.clsiCookie.ttl
this.settings.clsiCookie.ttl,
'clsi-8'
)
.should.equal(true)
return done()
@ -208,7 +200,7 @@ describe('ClsiCookieManager', function() {
this.project_id,
this.response,
(err, serverId) => {
this.redisMulti.exec.called.should.equal(false)
this.redis.setex.called.should.equal(false)
return done()
}
)
@ -222,19 +214,14 @@ describe('ClsiCookieManager', function() {
this.project_id,
this.response,
(err, serverId) => {
this.redisMulti.exec.called.should.equal(false)
this.redis.setex.called.should.equal(false)
return done()
}
)
})
it('should also set in the secondary if secondary redis is enabled', function(done) {
this.redisSecondaryMulti = {
set: sinon.stub(),
expire: sinon.stub(),
exec: sinon.stub()
}
this.redis_secondary = { multi: () => this.redisSecondaryMulti }
this.redis_secondary = { setex: sinon.stub().callsArg(3) }
this.settings.redis.clsi_cookie_secondary = {}
this.RedisWrapper.client = sinon.stub()
this.RedisWrapper.client.withArgs('clsi_cookie').returns(this.redis)
@ -254,13 +241,11 @@ describe('ClsiCookieManager', function() {
this.project_id,
this.response,
(err, serverId) => {
this.redisSecondaryMulti.set
.calledWith(`clsiserver:${this.project_id}`, 'clsi-8')
.should.equal(true)
this.redisSecondaryMulti.expire
this.redis_secondary.setex
.calledWith(
`clsiserver:${this.project_id}`,
this.settings.clsiCookie.ttl
this.settings.clsiCookie.ttl,
'clsi-8'
)
.should.equal(true)
return done()