Skip to content

Commit

Permalink
fix: add cache for file sizes
Browse files Browse the repository at this point in the history
  • Loading branch information
stephentuso committed Feb 4, 2021
1 parent f2e94d6 commit 463be3b
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 5 deletions.
6 changes: 2 additions & 4 deletions packages/@uppy/companion/src/server/controllers/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ const { retryWithDelay } = require('../helpers/utils')

const workerCount = process.env.COMPANION_WORKER_COUNT ? parseInt(process.env.COMPANION_WORKER_COUNT, 10) : 3

const retryDelays = [5000, 10000, 15000, 30000, 60000, 120000]

const queue = (() => {
const state = {}

Expand Down Expand Up @@ -56,7 +54,7 @@ function get (req, res, next) {
const tenantId = req.body.metadata && req.body.metadata.tenantId

retryWithDelay({
retryDelays,
retryDelays: [5000, 10000],
action: () => new Promise((resolve, reject) => provider.size({ id, token, query: req.query }, (err, size) => {
if (err) {
reject(err)
Expand Down Expand Up @@ -115,7 +113,7 @@ function get (req, res, next) {
uploader.onSocketReady(() => {
if (tenantId) {
queue(tenantId, () => retryWithDelay({
retryDelays,
retryDelays: [5000, 10000, 15000, 30000, 60000, 120000],
action: getPerformDownload(false),
lastAction: getPerformDownload(true)
}))
Expand Down
4 changes: 3 additions & 1 deletion packages/@uppy/companion/src/server/provider/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const { getCredentialsResolver } = require('./credentials')
const Provider = require('./Provider')
// eslint-disable-next-line
const SearchProvider = require('./SearchProvider')
const { withCache } = require('./withCache')

// leave here for now until Purest Providers gets updated with Zoom provider
config.zoom = {
Expand Down Expand Up @@ -61,7 +62,8 @@ module.exports.getProviderMiddleware = (providers, needsProviderCredentials) =>
*/
const middleware = (req, res, next, providerName) => {
if (providers[providerName] && validOptions(req.companion.options)) {
req.companion.provider = new providers[providerName]({ providerName, config })
const provider = new providers[providerName]({ providerName, config })
req.companion.provider = provider instanceof Provider ? withCache(provider) : provider
if (needsProviderCredentials) {
req.companion.getProviderCredentials = getCredentialsResolver(providerName, req.companion.options, req)
}
Expand Down
60 changes: 60 additions & 0 deletions packages/@uppy/companion/src/server/provider/withCache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
const cache = {}

const sizeKey = (providerName, id) => `${providerName}${id}size`

const addToCache = (key, data) => {
cache[key] = data
}

const getFromCache = (key) => cache[key]

/**
*
* @param {*} provider
*/
module.exports.withCache = (provider) => {
const providerName = provider.constructor.authProvider

const wrapList = (list) => (options, cb) => {
list(options, (err, data) => {
if (err) {
cb(err, data)
return
}

for (const item of data.items) {
if (item.size == null || item.id == null) {
continue
}
addToCache(sizeKey(providerName, item.id), item.size)
}

cb(err, data)
})
}

const wrapSize = (size) => (options, cb) => {
const key = sizeKey(providerName, options.id)
const cached = getFromCache(key)
if (cached) {
cb(null, cached)
}

size(options, (err, size) => {
if (err) {
cb(err, size)
return
}

addToCache(key, size)

cb(err, size)
})
}

// modifying is ugly but whatever
provider.list = wrapList(provider.list.bind(provider))
provider.size = wrapSize(provider.size.bind(provider))

return provider
}

0 comments on commit 463be3b

Please sign in to comment.