alykoshin / express-queue

Express middleware to limit a number of simultaneously processing requests using queue
MIT License
45 stars 12 forks source link

[FeatureRequest] support for queue per parameter value #2

Open tylkomat opened 8 years ago

tylkomat commented 8 years ago

For my api /do/:something I would like to allow only one request per value of something.

For example:

This is a simple example. The values for something are unspecified values like uuids.

alykoshin commented 8 years ago

Hello,

The issue is that internally there is only one Queue object per middleware. Dynamic creation of queues is a bit more complex than it is now.

Also the question is how to define something, i.e. how to define which part of the route to be used to limit the request: /do/:something/:other/action

tylkomat commented 8 years ago

The parameters are already parsed in the request object. Limiting could be done by matching parameters from an option value to the request parameters. Active limit could become an object for example:

app.use(queue({ activeLimit: {limit: 2, params: ['something'] }));

To limit only a changing something and ignoring other. Meaning a different value for other will still land on the same queue if something didn't change.

app.use(queue({ activeLimit: {limit: 2, params: ['something', 'other'] }));

In this case there should be probably a different queue for any something and other combination.

When a queue is empty it can be destroyed to not take up too much memory for routes which may not be accessed any more.

I would not got into the complexity of setting different limits for different parameters.

tylkomat commented 8 years ago

It should not used globally in this way, but as a per route middleware.

tylkomat commented 8 years ago

I don't know what use case you had in mind when you created this middleware. My use case was to limit access to one route which involved creation of a limited number of database artifacts. There I can't allow any concurrency since noSQL databases do not allow transactions and for every request I have to check if the item can be created. In a worst case when only one item is left to create there could come 2 or more requests at the same time and each get the answer that it is fine to create. I could involve redis for that matter but I was looking for a simple approach.