Remove returns of promises within functions with callbacks (address DeprecationWarning) (#25603)

* [document-updater] Don't return promises within functions with callbacks

Remove the errors:
DeprecationWarning: Calling promisify on a function that returns a Promise is likely a mistake
https://cloudlogging.app.goo.gl/YHDhoarvLEw2w9rXA

* Remove some more unnecessary returns in functions with callbacks, for consistency

* Add `sendCanaryAppliedOp` to excluded methods for promisification

GitOrigin-RevId: fa6d3e47c4e6561dc29d4c15e57c3289fc1f3dfa
This commit is contained in:
Antoine Clausse
2025-05-15 11:01:38 +02:00
committed by Copybot
parent 0debb4e752
commit b9b3cdc997
2 changed files with 30 additions and 31 deletions

View File

@@ -97,7 +97,8 @@ module.exports = class RedisLocker {
}
/**
* @param {Callback} callback
* @param {string} id
* @param {function(Error, boolean, string): void} callback
*/
tryLock(id, callback) {
if (callback == null) {
@@ -106,7 +107,7 @@ module.exports = class RedisLocker {
const lockValue = this.randomLock()
const key = this.getKey(id)
const startTime = Date.now()
return this.rclient.set(
this.rclient.set(
key,
lockValue,
'EX',
@@ -121,7 +122,7 @@ module.exports = class RedisLocker {
const timeTaken = Date.now() - startTime
if (timeTaken > MAX_REDIS_REQUEST_LENGTH) {
// took too long, so try to free the lock
return this.releaseLock(id, lockValue, function (err, result) {
this.releaseLock(id, lockValue, function (err, result) {
if (err != null) {
return callback(err)
} // error freeing lock
@@ -139,7 +140,8 @@ module.exports = class RedisLocker {
}
/**
* @param {Callback} callback
* @param {string} id
* @param {function(Error, string): void} callback
*/
getLock(id, callback) {
if (callback == null) {
@@ -153,7 +155,7 @@ module.exports = class RedisLocker {
return callback(e)
}
return this.tryLock(id, (error, gotLock, lockValue) => {
this.tryLock(id, (error, gotLock, lockValue) => {
if (error != null) {
return callback(error)
}
@@ -173,14 +175,15 @@ module.exports = class RedisLocker {
}
/**
* @param {Callback} callback
* @param {string} id
* @param {function(Error, boolean): void} callback
*/
checkLock(id, callback) {
if (callback == null) {
callback = function () {}
}
const key = this.getKey(id)
return this.rclient.exists(key, (err, exists) => {
this.rclient.exists(key, (err, exists) => {
if (err != null) {
return callback(err)
}
@@ -196,30 +199,26 @@ module.exports = class RedisLocker {
}
/**
* @param {Callback} callback
* @param {string} id
* @param {string} lockValue
* @param {function(Error, boolean): void} callback
*/
releaseLock(id, lockValue, callback) {
const key = this.getKey(id)
return this.rclient.eval(
UNLOCK_SCRIPT,
1,
key,
lockValue,
(err, result) => {
if (err != null) {
return callback(err)
} else if (result != null && result !== 1) {
// successful unlock should release exactly one key
logger.error(
{ id, key, lockValue, redis_err: err, redis_result: result },
'unlocking error'
)
metrics.inc(this.metricsPrefix + '-unlock-error')
return callback(new Error('tried to release timed out lock'))
} else {
return callback(null, result)
}
this.rclient.eval(UNLOCK_SCRIPT, 1, key, lockValue, (err, result) => {
if (err != null) {
return callback(err)
} else if (result != null && result !== 1) {
// successful unlock should release exactly one key
logger.error(
{ id, key, lockValue, redis_err: err, redis_result: result },
'unlocking error'
)
metrics.inc(this.metricsPrefix + '-unlock-error')
return callback(new Error('tried to release timed out lock'))
} else {
return callback(null, result)
}
)
})
}
}

View File

@@ -49,7 +49,7 @@ const RealTimeRedisManager = {
MAX_OPS_PER_ITERATION,
-1
)
return multi.exec(function (error, replys) {
multi.exec(function (error, replys) {
if (error != null) {
return callback(error)
}
@@ -80,7 +80,7 @@ const RealTimeRedisManager = {
},
getUpdatesLength(docId, callback) {
return rclient.llen(Keys.pendingUpdates({ doc_id: docId }), callback)
rclient.llen(Keys.pendingUpdates({ doc_id: docId }), callback)
},
sendCanaryAppliedOp({ projectId, docId, op }) {
@@ -132,5 +132,5 @@ const RealTimeRedisManager = {
module.exports = RealTimeRedisManager
module.exports.promises = promisifyAll(RealTimeRedisManager, {
without: ['sendData'],
without: ['sendCanaryAppliedOp', 'sendData'],
})