【发布时间】:2015-11-22 02:50:44
【问题描述】:
我正在构建一个多租户节点应用程序,其中租户是通过查询字符串标识的。
我还有一个 API 客户端,它根据站点发出经过身份验证的 (oauth) 请求。
我的问题是我是否可以将 API 客户端作为一个单例对象保留,并且只更新其中的会话 - 以及我是否会因为另一个请求同时进入而遇到竞争条件,而我' m 与客户端进行异步操作。
const session = new Session({ apiKey: 'xxx', secret: 'xxx' })
const client = new Client({ session })
app.use((req, res, next)=> {
res.locals.client = client;
next()
})
app.use((req, res, next)=> {
let { client, db } = res.locals;
if (req.query.tenant) {
return db.Tenant.findOne({ tenant: req.query.tenant })
.then((tenant)=> {
client.updateSession({
access_token: tenant ? tenant.access_token : null
})
next()
})
}
next();
})
app.get('/test/api', (req, res)=> {
let { client } = res.locals;
client.get('products').then((products)=> {
// What if another request from another tenant comes in right here?
// Is it possible for the session to be swapped out underneath me?
return products.get(2).someAsyncFunc().then((product)=> {
return res.json(product)
})
})
})
【问题讨论】:
-
嗯...我确实看到
client.updateSession可能存在问题。我不知道它是做什么的,所以我不能确定。绝对看起来这将是一个问题。一种解决方案是为每个请求提供它自己的client。 -
是的,updateSession 只是在客户端实例上设置了一个道具。听起来我每个请求都需要一个新实例。
标签: node.js asynchronous express