Comments (8)
写反了~
谢谢~
from easy-tips.
这块儿写的逻辑有严重问题……
- 22行 $getTime 和 $microtime 的大小关系判断写反了
- 29行,在超时抢锁时,getset 会不断的将(当前)未拿到锁的新的 timeout 写入�lock.count,如果先抢到锁的进程如果没有 del 掉 lock.count,那么当前进程永远都会 wait……
所以这个实现明显是有问题的……
写这么多,真正起作用的还是 setnx ……处理小于1秒级的超时时间的锁,就好好自行实现个 timeout ……你这个实现错误百出,不如直接用 expire 让 redis 帮你处理超时呢……
from easy-tips.
谢谢~
- 22行:是反了
- 29行:判断是超时之后才去抢锁,getset之间毫秒级别时间差,一般情况可以忽略
学习了~
from easy-tips.
fix it ~
from easy-tips.
关于 29行的问题,你在 36 行加入 sleep(10)
以模拟长时间占锁的情况,就会发现等待锁的进程一定要等到先拿到锁的进程完全退出才能拿到锁,抢锁机制是无效的。
所以才说你真正起作用的只是 setnx 而已……
from easy-tips.
别的进程getset拿不到锁?
我看看~
谢谢~
from easy-tips.
我重新验证了一下,getset 的逻辑本身没啥问题。问题出在 setnx 时的类型转换上。
setnx 进去的 timeout 被转成了科学计数法表示导致后续比较时精度丢失。我的 PHP 版本是 5.6
试了一下,两个办法可以解决:
- 17行 (microtime(true) % 1000) * 1000 强制把高位数字去掉
- 20行 setnx(..., strval($microtimeout)); 强转为数字表示的字符串
from easy-tips.
学习精神啊~
想你学习~
谢谢~
我看看~
from easy-tips.
Related Issues (20)
- English version HOT 1
- 关于设计模式中单例模式的意见 HOT 1
- 有歧义的变量命名 HOT 2
- [英文版翻译申请] HOT 8
- 数据结构 HOT 1
- 感觉之前提过的代码都不见了啊。。 HOT 3
- TIGERB/easy-tips/php/artisan.md 文件中关于使用匿名函数代码错误 HOT 3
- 引擎部分,聚簇索引上下文介绍不一致 HOT 4
- easy-tips/patterns/builder/ 建造者模式有错误,方法名字不对 HOT 2
- 悲观锁执行超时处理 HOT 1
- 设计模式 HOT 1
- 单利模式 HOT 1
- TIGERB------ HOT 1
- 为什么作者的设计模式25种 HOT 3
- 老哥,最近更新很慢啊 HOT 1
- 再通过list生成map时可通过内置函数实现 HOT 1
- tcp/ip网络模型想补充下 HOT 1
- Magic method access test HOT 1
- 组件并发模式会出现 go协程阻塞积压 HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from easy-tips.