Skip to content

Commit

Permalink
refactor(cu): each request is independent and no longer processes at …
Browse files Browse the repository at this point in the history
…batch
  • Loading branch information
TillaTheHun0 committed Dec 22, 2023
1 parent 17d384e commit cd6c2b9
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 50 deletions.
59 changes: 26 additions & 33 deletions servers/cu/src/routes/middleware/withInMemoryCache.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import Dataloader from 'dataloader'
import { LRUCache } from 'lru-cache'

import { logger as _logger } from '../../logger.js'
Expand Down Expand Up @@ -37,37 +36,31 @@ export const withInMemoryCache = ({

const logger = _logger.child('InMemoryCache')

const dataloader = new Dataloader(loader, {
cacheKeyFn: ({ req }) => keyer(req),
cacheMap: {
get: (key) => {
/**
* See https://www.npmjs.com/package/lru-cache#status-tracking
*
* This allows us to log when our InMemoryCache has a HIT or MISS
*/
const status = {}
const res = cache.get(key, { status })
logger(`"%s" for key ${key}`, status.get.toUpperCase())
return res
},
set: cache.set.bind(cache),
clear: cache.clear.bind(cache),
delete: (key) => {
logger('Removing "%s"', key)
return cache.delete(key)
}
}
})
return async (req, res) => {
const key = keyer(req)

/**
* See https://www.npmjs.com/package/lru-cache#status-tracking
*
* This allows us to log when our InMemoryCache has a HIT or MISS
*/
const status = {}
const cached = cache.get(key, { status })
logger(`"%s" for key ${key}`, status.get.toUpperCase())

if (cached) return res.send(cached)

return (req, res) => dataloader.load({ req, res })
.then(result => res.send(result))
.catch(err => {
if (evict(req, err)) dataloader.clear(keyer(req))
/**
* After optionally clearing the cache on err,
* continue bubbling
*/
throw err
})
return loader({ req, res })
.then((result) => {
cache.set(key, result)
res.send(result)
})
.catch((err) => {
if (evict(req, err)) {
logger('Removing "%s"', key)
cache.delete(key)
}
throw err
})
}
}
26 changes: 9 additions & 17 deletions servers/cu/src/routes/result.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,17 @@ export const withResultRoutes = app => {
const { params: { messageTxId } } = req
return messageTxId
},
loader: async (requests) => {
return requests.map(async ({ req }) => {
const {
params: { messageTxId },
query: { 'process-id': processId },
domain: { apis: { readResult } }
} = req
loader: async ({ req }) => {
const {
params: { messageTxId },
query: { 'process-id': processId },
domain: { apis: { readResult } }
} = req

const input = inputSchema.parse({ messageTxId, processId })
const input = inputSchema.parse({ messageTxId, processId })

return readResult(input)
.toPromise()
/**
* Will bubble up to the individual load call
* on the dataloader, where it can be handled
* individually
*/
.catch(err => err)
})
return readResult(input)
.toPromise()
}
})
)()
Expand Down

0 comments on commit cd6c2b9

Please sign in to comment.