bandwidth-throttle / token-bucket

Implementation of the Token Bucket algorithm in PHP.
Do What The F*ck You Want To Public License
503 stars 77 forks source link

MemcachedStorage CASMutex TimeoutException #12

Closed davyrolink closed 6 years ago

davyrolink commented 6 years ago

The current implementation of MemcachedStorage doesn't work.

When the consume method is called and "$delta < 0", then $this->storage->getMutex()->synchronized keeps looping the "callable $code". This is because "$this->storage->getMutex()->notify()" is never called when "$delta < 0", so "$this->loop->execute($code)" (CASMutex) keeps running and will reach the timeout "malkusch\lock\exception\TimeoutException" "Timeout of 3 seconds exceeded.".

The current implementation of MemcachedStorage uses CAS, where the MemcacheStorage (without D) implementation doesn't. I've created a MemcachedStorage implementation without CAS (also using "MemcachedMutex" instead of "CASMutex") and that seems to work fine. What was the reason to use the CAS tokens?

malkusch commented 6 years ago

Thanks for pointing out that issue. I'll soon have a fix release.

What was the reason to use the CAS tokens?

CAS is preferable if you have rare lock contention.

malkusch commented 6 years ago

This fix is included in 1.1.1