几个小时后共享变量不断丢失
我正在使用 shm_ 函数向我的 API 提供共享数组。 该数组存储客户端数据,有助于限制传入请求(速率限制等) 我使用 suhosin 补丁运行 PHP 5.2.6-1
这是代码,对于正在寻找在 PHP 中创建/读取/写入/存储共享内存数据的代码的人来说,它实际上可能很有用。
$shm_key=ftok(__FILE__, 'S' );
$data=shm_attach($shm_key,500000);
$ar=shm_get_var($data,"variable");
if ($ar === FALSE))
{
// this should rarely ever happen !
// recreate empty arry
}
//...
// reading and changing the $ar array contents
//...
shm_put_var($data,"variable",$ar);
shm_detach($data);
序列化变量数据很少超过 10-11kb,因此 500kb 应该是足够的可用空间。 每小时总共有 10,000-50,000 次 php 脚本调用。
($ar === FALSE) 每天被击中 4-5 次,导致所有节流数据完全丢失。 多年来我已经接受了这种行为,但仍然希望看到它得到解决。
我的猜测是,这要么是 shm_ 中的错误,要么是写入/读取时的竞争条件,并且多个客户端同时调用脚本。
I'm using the shm_ functions to provide a shared array to an API of mine.
The array stores client data which helps to throttle incomming requests (rate limiting etc)
I run PHP 5.2.6-1 with suhosin patch
Here is the code, it might actually be useful to someone who's looking for code to create/read/write/store shared memory data in PHP.
$shm_key=ftok(__FILE__, 'S' );
$data=shm_attach($shm_key,500000);
$ar=shm_get_var($data,"variable");
if ($ar === FALSE))
{
// this should rarely ever happen !
// recreate empty arry
}
//...
// reading and changing the $ar array contents
//...
shm_put_var($data,"variable",$ar);
shm_detach($data);
The serialized variable data is rarely ever larger than 10-11kb, so 500kb should be plenty of free space.
In total there 10,000-50,000 of php script calls per hour.
The ($ar === FALSE) is hit 4-5 times a day resulting in a complete loss of all throttling data.
I've accepted this behaviour for years but would still like to see it solved.
My guess is that it's either a bug in shm_ or a race condition when writing/reading and multiple clients call the script at the same moment.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论