redis跨服分布式全局锁
问题描述在玩家切换场景之前我存了一个坐标。在玩家再次切换场景的时候我取出上次存的坐标居然不是我存的坐标 然后看log save_last_scene 10007500009521 101 43 57 ------remsg--------- { [scene_id] 101, [pos] { [x] 37, [y] 66, }, [reconnect] false, } last_x 和 last_y不是保存的值 看起来是redis保存失败了 进程 logic function Oper.User.SetGameInfo(userid, game_info) if(not game_info)then return end -- 更新redis local user_cache redis_wraper.wrap_table(user_info_cache,userid) local userinfo user_cache:select(userid) userinfo.game_info game_info userinfo.room_id game_info.room_id user_cache:save(userid, userinfo) player_mgr.set(userid, game_info,game_info) end 进程 scene function CMD.save_last_scene(userid, scene_id, x, y) local t redis_wraper.wrap_table(user_info_cache,userid) local userinfo t:select(userid) userinfo.last_scene_id scene_id userinfo.last_x x userinfo.last_y y t:save(userid, userinfo) print(save_last_scene,userid,scene_id,x,y) end local function build_enter_scene_remsg(mbr) local userid mbr.userid local user_info_cache proxy.call_game(nil, nil, get_user_info_cache, userid) or {} if user_info_cache.last_scene_id and user_info_cache.last_scene_id g_data.scene_id then mbr.cur_pos.x user_info_cache.last_x mbr.cur_pos.y user_info_cache.last_y end local remsg { scene_id g_data.scene_id, pos mbr.cur_pos, reconnect mbr.reconnect, state g_data.state } mbr.reconnect nil --重连标记是一次性的用完清空 -- 战斗ID相关逻辑 if mbr.battleId then local battleRecord Handler.Battle.GetBattle(mbr.battleId) if battleRecord then for _, battleUserId in pairs(battleRecord.userList or {}) do if battleUserId userid then remsg.battleId mbr.battleId break end end end end print(------remsg---------,tostring(remsg)) return remsg end 玩家切换场景回先通知 logic 调用函数 SetGameInfo 然后通知 scene调用 save_last_scene。 理论上如果是一个地方存就没问题但是我们先不讨论设计的问题先讨论保存2次出现bug. 其实就是要加锁redis一样存在不同进程同时操作同一个数据的问题修改会失败。 下面是修改后的 function Oper.User.SetGameInfo(userid, game_info) if(not game_info)then return end -- 更新redis整条读写包进跨服全局锁和 save_last_scene 串行化避免互相覆盖 last_x/last_y local lock_ok redis_wraper.redis_global_lock(userid, function() local user_cache redis_wraper.wrap_table(user_info_cache,userid) local userinfo user_cache:select(userid) logger.fmt.info([set_game_info_lock] GOT_LOCK userid:%s read_last_x:%s read_last_y:%s - set game_info.scene_id:%s, tostring(userid), tostring(userinfo and userinfo.last_x), tostring(userinfo and userinfo.last_y), tostring(game_info and game_info.scene_id)) userinfo.game_info game_info userinfo.room_id game_info.room_id user_cache:save(userid, userinfo) logger.fmt.info([set_game_info_lock] SAVED userid:%s keep_last_x:%s keep_last_y:%s, tostring(userid), tostring(userinfo.last_x), tostring(userinfo.last_y)) end) logger.fmt.info([set_game_info_lock] DONE userid:%s lock_ok:%s, tostring(userid), tostring(lock_ok)) player_mgr.set(userid, game_info,game_info) end function CMD.save_last_scene(userid, scene_id, x, y) logger.fmt.info([save_last_scene_lock] REQ userid:%s scene_id:%s x:%s y:%s, tostring(userid), tostring(scene_id), tostring(x), tostring(y)) local lock_ok redis_wraper.redis_global_lock(userid, function() local t redis_wraper.wrap_table(user_info_cache,userid) local userinfo t:select(userid) logger.fmt.info([save_last_scene_lock] GOT_LOCK userid:%s read_last_x:%s read_last_y:%s - write x:%s y:%s, tostring(userid), tostring(userinfo and userinfo.last_x), tostring(userinfo and userinfo.last_y), tostring(x), tostring(y)) userinfo.last_scene_id scene_id userinfo.last_x x userinfo.last_y y t:save(userid, userinfo) end) logger.fmt.info([save_last_scene_lock] DONE userid:%s lock_ok:%s, tostring(userid), tostring(lock_ok)) print(save_last_scene,userid,scene_id,x,y) end 加锁函数 -- 【跨服分布式全局锁】逻辑服/游戏服共用防止并发脏写 user_info_cache -- 作用以 userid 为维度跨服/多机 原子串行化 Redis 整条 read-modify-write -- 用法redis_wraper.redis_global_lock(userid, function() ... end) local REDIS_LOCK_PREFIX redis:global:lock: local LOCK_EXPIRE 300 -- 0.3秒自动释放临界区是 Redis 整条读写(亚毫秒)0.3s足够缩短崩溃恢复窗口 local LOCK_RETRY 5 -- 瞬时竞态下(另一服正持有)最多重试次数避免输家直接跳过写入 local LOCK_RETRY_TICK 1 -- 每次重试间隔(skynet tick, 1 tick≈10ms)5次≈50ms 远小于 300ms TTL function redis_wraper.redis_global_lock(userid, func) local key REDIS_LOCK_PREFIX .. tostring(userid) local uuid skynet.self() .. : .. skynet.now() local conn redis_wraper.get_raw_conn(userid) -- 原子加锁 (SET key value NX PX 毫秒)失败时短暂退避重试 local acquired false for _ 1, LOCK_RETRY do local ok conn:set(key, uuid, NX, PX, LOCK_EXPIRE) if ok then acquired true break end skynet.sleep(LOCK_RETRY_TICK) end if not acquired then logger.error([RedisLock] 获取Redis全局锁失败 userid .. tostring(userid)) return nil end -- 执行业务逻辑整条读改写都在临界区内 local suc, err pcall(func) if not suc then logger.error([RedisLock] Redis操作失败 userid .. tostring(userid), err) end -- 原子解锁 (Lua脚本只有持有者能释放防误删) local script [[ if redis.call(get, KEYS[1]) ARGV[1] then return redis.call(del, KEYS[1]) end return 0 ]] conn:eval(script, 1, key, uuid) return true end -- 【跨服锁结束】

相关新闻

用一篇文章,帮你了解交互设计方法论「渐进式披露」

用一篇文章,帮你了解交互设计方法论「渐进式披露」

大家好。今天为大家分享的是「渐进式披露」。在设计中并不是给用户的选择越多越好,每一个额外的选择都会导致做决定所需的时间变长。 大家好,我是 Clippp。今天为大家分享的是「渐进式披露」。在设计中并不是给用户的选择越多越好,每一个额外…

2026/10/24 22:05:58 阅读更多 →
事件驱动架构(EDA)核心原理与实战优化

事件驱动架构(EDA)核心原理与实战优化

1. 事件驱动架构的本质与核心价值第一次接触事件驱动架构(EDA)是在2016年一个电商促销系统改造项目中。当时我们的单体应用在流量高峰时频繁崩溃,而引入基于事件的解耦方案后,系统吞吐量提升了8倍。这种架构范式与传统请求/响应模…

2026/10/26 15:31:59 阅读更多 →
如何免费解锁网易云音乐NCM格式:3分钟快速转换指南

如何免费解锁网易云音乐NCM格式:3分钟快速转换指南

如何免费解锁网易云音乐NCM格式:3分钟快速转换指南 【免费下载链接】ncmdump ncmdump - 网易云音乐NCM转换 项目地址: https://gitcode.com/gh_mirrors/ncmdu/ncmdump ncmdump是一款专业的网易云音乐NCM格式转换工具,能够快速解密网易云音乐的加密…

2026/10/25 19:55:21 阅读更多 →

最新新闻

Electron桌面应用开发实战:从环境搭建到打包发布全流程

Electron桌面应用开发实战:从环境搭建到打包发布全流程

做前端这些年,我大部分时间都在跟浏览器标签页打交道,直到第一次把桌面应用跑起来,才发现原来自己一直被限制在地址栏和刷新按钮围起来的框框里。Electron这个词,最开始是在某段时间频繁出现在同事的讨论里,一个用HTML…

2026/10/27 17:33:32 阅读更多 →
滑盖智能锁T109深度解析:结构原理、安装调试与故障排查

滑盖智能锁T109深度解析:结构原理、安装调试与故障排查

简介:亚太天能T109滑盖智能锁产品说明书是一份PDF格式的官方技术文档,主要面向智能锁安装人员、终端用户及售后维护者,帮助快速掌握产品安装、配置与常见故障处理。全包仅1个PDF文件,大小4.37MB,内容结构清晰&#xff…

2026/10/28 7:28:00 阅读更多 →
PMSM弱磁控制原理与Simulink建模:高速AGV驱动系统设计指南

PMSM弱磁控制原理与Simulink建模:高速AGV驱动系统设计指南

上个月我在调试一台高速AGV的驱动系统,整车要求空载跑到8m/s,电机转速已经拉到额定点,驱动器的输出电压也顶到了母线电压极限附近。再往上踩速度指令,转速纹丝不动,三相电流反而开始震荡。那会儿我的第一反应就是&…

2026/10/28 9:02:11 阅读更多 →
C++核心知识点:从内存管理到STL的实战指南

C++核心知识点:从内存管理到STL的实战指南

C 这门语言我前前后后折腾了十几年,用它写过嵌入式驱动、做过游戏服务端,也搭过基础组件库。每次看到新人一头扎进"C 核心知识点"这个搜索词,我其实挺感慨的——这门语言的难点从来不是语法多,而是知识点之间的耦合太深…

2026/10/27 18:20:50 阅读更多 →
T109滑盖智能锁怎么装?安装调试与故障排查全攻略

T109滑盖智能锁怎么装?安装调试与故障排查全攻略

简介:亚太天能T109滑盖智能锁产品说明书以PDF格式发布,适合智能锁用户、安装人员与售后维修人员阅读。内容围绕T109系列指纹密码刷卡锁展开,先介绍前面板、后面板、锁体、机械钥匙、电池盒盖等部件名称与功能,再给出完整安装步骤&…

2026/10/28 9:00:54 阅读更多 →
LLM知识降熵:从信息垄断到理解权下放

LLM知识降熵:从信息垄断到理解权下放

你有没有过这种经历:翻开源码注释、论文摘要或者一份专业报告,每个字都认识,连在一起却像天书。想找人聊聊,身边要么没人懂,要么懂的懒得跟你解释。后来我试着把同一段内容丢给一个大语言模型,几秒钟之后&a…

2026/10/28 7:29:02 阅读更多 →

日新闻

周新闻

月新闻

我发现了一个新思路:用 Remotion + Claude Code 像写代码一样自动化生成短视频

我发现了一个新思路:用 Remotion + Claude Code 像写代码一样自动化生成短视频

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/10/27 13:55:19 阅读更多 →
Windows下 Codex 中 Chrome 和 Computer Use 插件不可用问题排查及解决参考方式:TaoToken 统一 Key 配置与验证

Windows下 Codex 中 Chrome 和 Computer Use 插件不可用问题排查及解决参考方式:TaoToken 统一 Key 配置与验证

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/10/28 9:01:47 阅读更多 →
黑夜航拍船只数据集训练YOLOV5模型全流程解析

黑夜航拍船只数据集训练YOLOV5模型全流程解析

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/10/27 19:19:37 阅读更多 →