admin管理员组文章数量:1418861
I'm exploring server side JavaScript with NodeJS and writing REST APIs with ExpressJS. My REST API is for configuring a device, much like configuring your home router. I would like to prevent parallel REST calls from attempting to configure the client without preventing multiple non-config requests.
Example API resources:
/api/config/network
/api/config/filesystem
In my example API, conceptually I'd like one client to be able to configure the network resource while another client configures the file system resource, while allowing other clients to query those resources.
I'm not sure how to acplish this with JavaScripts asynchronous programming model. I e from a non-asynchronous programming background (multi-threaded C++). I am having trouble wrapping my head around this problem and wondering what ways one can solve this problem by hand, what if I had to write the plugin/module?.
I'm exploring server side JavaScript with NodeJS and writing REST APIs with ExpressJS. My REST API is for configuring a device, much like configuring your home router. I would like to prevent parallel REST calls from attempting to configure the client without preventing multiple non-config requests.
Example API resources:
/api/config/network
/api/config/filesystem
In my example API, conceptually I'd like one client to be able to configure the network resource while another client configures the file system resource, while allowing other clients to query those resources.
I'm not sure how to acplish this with JavaScripts asynchronous programming model. I e from a non-asynchronous programming background (multi-threaded C++). I am having trouble wrapping my head around this problem and wondering what ways one can solve this problem by hand, what if I had to write the plugin/module?.
Share Improve this question edited Mar 4, 2013 at 19:35 snowballhg asked Mar 4, 2013 at 19:10 snowballhgsnowballhg 3782 silver badges10 bronze badges 8- 1 There are libraries to help you handle that, such as github./caolan/async – bfavaretto Commented Mar 4, 2013 at 19:12
- 1 Have you attempted anything on this yet? It usually much easier to help someone when they've posted some code that shows what they've attempted already. – tkone Commented Mar 4, 2013 at 19:13
- @tkone my issue is I'm not sure what to attempt. – snowballhg Commented Mar 4, 2013 at 19:15
- @bfavaretto thanks I'll look at that library, though I'm more interested in how I might acplish it myself. – snowballhg Commented Mar 4, 2013 at 19:17
- You could take a look at npmjs/package/generic-pool as well – Floby Commented Mar 4, 2013 at 19:24
1 Answer
Reset to default 8When your express app gets a request to edit the config, add an item to an async.js queue configured with concurrency of 1. This will prevent > 1 concurrent attempt to edit the remote configuration. Pseudo code would be:
- at app startup, define your
updateNetworkConfig
function - at app startup, create a queue of concurrency 1
var networkQueue = async.queue(updateNetworkConfig, 1);
- REST es in like PUT /api/config/network with the new config as the request body in JSON
- Add an item to the queue, passing along the new config data
networkQueue.push(req.body.config);
- (
req.body
provided by the bodyParser connect middleware)
- Repeat that setup for each independent config: filesystem, etc
You'll need a bunch of other plumbing to deal with callbacks and notify the REST clients when their configuration has been saved. Up to you whether to queue up calls or reject new calls if the queue is not empty.
Since you insist on not learning and using the awesome async.js library that will increase your fluency with asynchronous programming and teach you valuable things, here's the deal. Use this thing called a variable.
var updatingNetwork = false;
app.put('/api/config/network', function (req, res) {
if (updatingNetwork) {
return res.status(409).send("Try later");
}
updatingNetwork = true;
updateNetworkConfig(req.body.config, function (error) {
updatingNetwork = false;
if (error) {
return res.status(500).send(error);
}
res.status(200).send("done");
});
});
- When a request es in, if
updatingNetwork
is true, you know there's already an update in process, either queue this one up or reject it.
本文标签: javascriptHow to serialize NodeJSStack Overflow
版权声明:本文标题:javascript - How to serialize NodeJS? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745297969a2652192.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论