Throttler
Throttler copied to clipboard
Race condition in `throttle()`
I faced a race condition using func throttle()
: it is possible to reach await actor.run(operation)
multiple times in less time than the specified duration
parameter.
Nothing prevents throttle()
to be called simultaneously from different threads. Throttler
being an actor
, this is safe but the race condition is still here: let lastDate = lastAttemptDate[identifier]
can be set from 2 (or more) different threads before self.lastAttemptDate[identifier] = Date()
is reached.
When this happens, the operation
is called multiple times in a row, breaking the throttle contract.
I'll try to suggest a fix later.