Coder Social home page Coder Social logo

悲观锁 about easy-tips HOT 8 CLOSED

tigerb avatar tigerb commented on August 16, 2024
悲观锁

from easy-tips.

Comments (8)

TIGERB avatar TIGERB commented on August 16, 2024

写反了~

谢谢~

from easy-tips.

slawdan avatar slawdan commented on August 16, 2024

这块儿写的逻辑有严重问题……

  1. 22行 $getTime 和 $microtime 的大小关系判断写反了
  2. 29行,在超时抢锁时,getset 会不断的将(当前)未拿到锁的新的 timeout 写入�lock.count,如果先抢到锁的进程如果没有 del 掉 lock.count,那么当前进程永远都会 wait……

所以这个实现明显是有问题的……

写这么多,真正起作用的还是 setnx ……处理小于1秒级的超时时间的锁,就好好自行实现个 timeout ……你这个实现错误百出,不如直接用 expire 让 redis 帮你处理超时呢……

from easy-tips.

TIGERB avatar TIGERB commented on August 16, 2024

谢谢~

  • 22行:是反了
  • 29行:判断是超时之后才去抢锁,getset之间毫秒级别时间差,一般情况可以忽略

学习了~ 

from easy-tips.

TIGERB avatar TIGERB commented on August 16, 2024

fix it ~

from easy-tips.

slawdan avatar slawdan commented on August 16, 2024

关于 29行的问题,你在 36 行加入 sleep(10) 以模拟长时间占锁的情况,就会发现等待锁的进程一定要等到先拿到锁的进程完全退出才能拿到锁,抢锁机制是无效的。

所以才说你真正起作用的只是 setnx 而已……

from easy-tips.

TIGERB avatar TIGERB commented on August 16, 2024

别的进程getset拿不到锁?

我看看~

谢谢~

from easy-tips.

slawdan avatar slawdan commented on August 16, 2024

我重新验证了一下,getset 的逻辑本身没啥问题。问题出在 setnx 时的类型转换上。

setnx 进去的 timeout 被转成了科学计数法表示导致后续比较时精度丢失。我的 PHP 版本是 5.6

试了一下,两个办法可以解决:

  1. 17行 (microtime(true) % 1000) * 1000 强制把高位数字去掉
  2. 20行 setnx(..., strval($microtimeout)); 强转为数字表示的字符串

from easy-tips.

TIGERB avatar TIGERB commented on August 16, 2024

学习精神啊~

想你学习~

谢谢~

我看看~

from easy-tips.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.