Phuks-co / throat

Open Source link aggregator and discussion platform powering Phuks
https://phuks.co
MIT License
73 stars 32 forks source link

Fix subscription counts, and other counters vulnerable to race conditions #152

Closed happy-river closed 4 years ago

happy-river commented 4 years ago

Subscriber counts on our instance were off by a huge margin and negative for some subs. On investigation we spotted this pattern which the peewee docs say specifically to not do:

        sub.subscribers += 1
        sub.save()

The reason to not do it this way is that it is vulnerable to race conditions if another request is simultaneously subscribing or unsubscribing.

That turned out not to be the main problem with subscription counts, which was that register was not updating them when it subscribed new users to the default subs.

This PR: