du8980919 2016-10-11 02:00
浏览 98

如何按性别拒绝同时请求(基于PHP的API)

While I have some ideas (IPC, using mysql heap table, memory cache) none of my ideas seem to be working "perfectly".

I look for a way to block any simultanous access to the PHP script from the same user.
All users accessing my script supply a User-ID on every call, so I know who is knocking the door before I open it.
However, it can happen that users "spam" the hell out of my API, I do not want that anyone can access the script while a previous "api call" is unfinished.

I need something with low system impact and without any race-conditions (flawless handling even if requests come in within a millisecond or faster).

I'd be glad for suggestions or info how you solved a similar issue.

Platform is Linux.

Update:
I rejected using a database so far because I fear that sending thousands of update or similar queries per second could stall the server even if the single impact is low.
Sometimes the users tool accessing my API is faulty and just bombards it with massive queries, I expect that to be a performance hit if it's handled through my DB.

  • 写回答

1条回答 默认 最新

  • doutang6130 2016-10-11 03:00
    关注

    If I were in your situation, I'd probably start with Redis. Redis is pretty darned performant, and is likely good enough.

    In particular, I'd be looking at the SETNX command. And I'd look at making the key expire after a short while (so if your PHP code dies and doesn't DEL the key, your customer isn't locked out indefinitely).

    Once I had something like that working, and convinced myself it would perform well enough, I'd follow the suggestion on the above-linked page, and look into distributed locks.

    评论

报告相同问题?

悬赏问题

  • ¥15 yolov8边框坐标
  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真