Skip to content

Commit fb8b49f

Browse files
committed
Avoid recomputing definition when the coordinates are queued multiple times within one batch
1 parent b31e6e1 commit fb8b49f

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

providers/upgrade/process.js

+24-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
const { get } = require('lodash')
55
const EntityCoordinates = require('../../lib/entityCoordinates')
66
const { factory } = require('./defVersionCheck')
7+
const Cache = require('../caching/memory')
78

89
class QueueHandler {
910
constructor(queue, logger, messageHandler = { processMessage: async () => {} }) {
@@ -33,18 +34,39 @@ class QueueHandler {
3334
}
3435

3536
class DefinitionUpgrader {
36-
constructor(definitionService, logger, defVersionChecker) {
37+
static defaultTtlSeconds = 60 * 5 /* 5 mins */
38+
static delayInMSeconds = 500
39+
40+
constructor(
41+
definitionService,
42+
logger,
43+
defVersionChecker,
44+
cache = Cache({ defaultTtlSeconds: DefinitionUpgrader.defaultTtlSeconds })
45+
) {
3746
this.logger = logger
3847
this._definitionService = definitionService
3948
this._defVersionChecker = defVersionChecker
4049
this._defVersionChecker.currentSchema = definitionService.currentSchema
50+
this._upgradeLock = cache
4151
}
4252

4353
async processMessage(message) {
4454
let coordinates = get(message, 'data.coordinates')
4555
if (!coordinates) return
46-
4756
coordinates = EntityCoordinates.fromObject(coordinates)
57+
58+
while (this._upgradeLock.get(coordinates.toString())) {
59+
await new Promise(resolve => setTimeout(resolve, DefinitionUpgrader.delayInMSeconds))
60+
}
61+
try {
62+
this._upgradeLock.set(coordinates.toString(), true)
63+
await this._upgradeIfNecessary(coordinates)
64+
} finally {
65+
this._upgradeLock.delete(coordinates.toString())
66+
}
67+
}
68+
69+
async _upgradeIfNecessary(coordinates) {
4870
const existing = await this._definitionService.getStored(coordinates)
4971
let result = await this._defVersionChecker.validate(existing)
5072
if (!result) {

0 commit comments

Comments
 (0)