1
0
Fork 0
mirror of synced 2024-06-28 11:00:55 +12:00

Switching from scan for app locks to mget - which is a fast O(N) operation that only retrieves what we need.

This commit is contained in:
mike12345567 2022-10-07 16:05:01 +01:00
parent 0b47da374d
commit dc589f7fb5
3 changed files with 35 additions and 15 deletions

View file

@ -214,6 +214,31 @@ export = class RedisWrapper {
} }
} }
async bulkGet(keys: string[]) {
const db = this._db
const prefixedKeys = keys.map(key => addDbPrefix(db, key))
let response = await this.getClient().mget(prefixedKeys)
if (Array.isArray(response)) {
let final: any = {}
let count = 0
for (let result of response) {
if (result) {
let parsed
try {
parsed = JSON.parse(result)
} catch (err) {
parsed = result
}
final[keys[count]] = parsed
}
count++
}
return final
} else {
throw new Error(`Invalid response: ${response}`)
}
}
async store(key: string, value: any, expirySeconds: number | null = null) { async store(key: string, value: any, expirySeconds: number | null = null) {
const db = this._db const db = this._db
if (typeof value === "object") { if (typeof value === "object") {

View file

@ -32,7 +32,7 @@ const {
import { USERS_TABLE_SCHEMA } from "../../constants" import { USERS_TABLE_SCHEMA } from "../../constants"
import { removeAppFromUserRoles } from "../../utilities/workerRequests" import { removeAppFromUserRoles } from "../../utilities/workerRequests"
import { clientLibraryPath, stringToReadStream } from "../../utilities" import { clientLibraryPath, stringToReadStream } from "../../utilities"
import { getAllLocks } from "../../utilities/redis" import { getLocksById } from "../../utilities/redis"
import { import {
updateClientLibrary, updateClientLibrary,
backupClientLibrary, backupClientLibrary,
@ -45,11 +45,10 @@ import { cleanupAutomations } from "../../automations/utils"
import { context } from "@budibase/backend-core" import { context } from "@budibase/backend-core"
import { checkAppMetadata } from "../../automations/logging" import { checkAppMetadata } from "../../automations/logging"
import { getUniqueRows } from "../../utilities/usageQuota/rows" import { getUniqueRows } from "../../utilities/usageQuota/rows"
import { quotas } from "@budibase/pro" import { quotas, groups } from "@budibase/pro"
import { errors, events, migrations } from "@budibase/backend-core" import { errors, events, migrations } from "@budibase/backend-core"
import { App, Layout, Screen, MigrationType } from "@budibase/types" import { App, Layout, Screen, MigrationType } from "@budibase/types"
import { BASE_LAYOUT_PROP_IDS } from "../../constants/layouts" import { BASE_LAYOUT_PROP_IDS } from "../../constants/layouts"
import { groups } from "@budibase/pro"
import { enrichPluginURLs } from "../../utilities/plugins" import { enrichPluginURLs } from "../../utilities/plugins"
const URL_REGEX_SLASH = /\/|\\/g const URL_REGEX_SLASH = /\/|\\/g
@ -172,16 +171,16 @@ export const fetch = async (ctx: any) => {
const all = ctx.query && ctx.query.status === AppStatus.ALL const all = ctx.query && ctx.query.status === AppStatus.ALL
const apps = await getAllApps({ dev, all }) const apps = await getAllApps({ dev, all })
const appIds = apps
.filter((app: any) => app.status === "development")
.map((app: any) => app.appId)
// get the locks for all the dev apps // get the locks for all the dev apps
if (dev || all) { if (dev || all) {
const locks = await getAllLocks() const locks = await getLocksById(appIds)
for (let app of apps) { for (let app of apps) {
if (app.status !== "development") { const lock = locks[app.appId]
continue
}
const lock = locks.find((lock: any) => lock.appId === app.appId)
if (lock) { if (lock) {
app.lockedBy = lock.user app.lockedBy = lock
} else { } else {
// make sure its definitely not present // make sure its definitely not present
delete app.lockedBy delete app.lockedBy

View file

@ -34,12 +34,8 @@ exports.doesUserHaveLock = async (devAppId, user) => {
return expected === userId return expected === userId
} }
exports.getAllLocks = async () => { exports.getLocksById = async appIds => {
const locks = await devAppClient.scan() return await devAppClient.bulkGet(appIds)
return locks.map(lock => ({
appId: lock.key,
user: lock.value,
}))
} }
exports.updateLock = async (devAppId, user) => { exports.updateLock = async (devAppId, user) => {