Hi everyone, I’ve a concurrency issue in the code below:
Parse.Cloud.define("subscribe", async (request) => {
.....
const listId = .....
const subscriberId = ...
const SubscribersList = Parse.Object.extend("SubscribersList");
const query = new Parse.Query(SubscribersList);
query.equalTo("listId", listId);
let numberSubscriptions = await query.count({ useMasterKey: true });
// maxSubscribers retrieved from another collection linked to the SubscribersList with the same listId
const maxSubscribers = ..... get("maxSubscribers");
if (numberSubscriptions < maxSubscribers) {
const subscriber = new SubscribersList();
subscriber.set("subscriberId", subscriberId);
subscriber.set("subscriptionDate", new Date(Date.now()));
subscriber.save(null, { useMasterKey: true });
}
}, {
requireUser: true
});
When I add a new subscriber, if there are more concurrent requests, they could exceed the “maxSubscribers” that I have for a specific subscription list.
Any suggestion on how to manage the concurrency in this case?
Thanks