skynet框架:并发热点处理方案
- 对于关键流程,所有请求都要求返回有效结果,如创建socket连接:
function luasocket:connect()
return socketcore.open(self.__host, self.__port)
end
显然外部调用需要获取到正确的socket句柄用于数据交互,当并发调用此接口时,所有调用都需要获取到有效的句柄以保证业务正常;
方案:加锁,保证临界区只由唯一的coroutine正确执行,并发的其他coroutine在临界区处休眠等待,临界区执行完成后,负责唤醒所有休眠的coroutine,直接获取有效结果返回;
function luasocket:safe_connect()
local conn
local nwaitingco = #self.__connecting -- wait queue
if nwaitingco > 0 then
-- connecting in other coroutine
local co = coroutine.running()
table.insert(self.__connecting, co)
skynet.wait(co)
return assert(conn) -- return conn when being awakened
end
-- the effective coroutine which build connect
self.__connecting[1] = true -- lock
conn = socketcore.open(self.__host, self.__port)
-- wake all wait coroutines
for i=2, #self.__connecting do
local co = self.__connecting[i]
self.__connecting[i] = nil
skynet.wakeup(co)
end
self.__connecting[1] = nil -- unlock
return assert(conn)
end
- 对于非关键或接受单次请求无效,支持重入的流程,如定时存盘:
function user:save(query, document)
return mongodb.update(query, document)
end
数据以全量覆盖的形式存盘,那么单次存盘无效认为是可以接受的,下一次请求到来的时候数据能够正确写入;
方案:临界区加锁,无效并发的存盘请求;
function user:safe_save(query, document)
if user.__saving then
-- saving in other coroutine, return succ
return true
end
user.__saving = true -- lock
-- do save
mongodb.update(query, document)
user.__saving = nil -- unlock
return true
end
做得更好的方案,对并发的coroutine做消息降级,兼顾请求有效同时确保请求能被正确唤醒;
function user:safe_save2(query, document)
if user.__saving then
-- saving in other coroutine, sleep
skynet.sleep(delay)
if user.__saving then -- double check
return true
end
end
user.__saving = true -- lock
-- do save
mongodb.update(query, document)
user.__saving = nil -- unlock
return true
end
本文来自博客园,作者:linxx-,转载请注明原文链接:https://www.cnblogs.com/linxx-/p/18189905
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 【.NET】调用本地 Deepseek 模型
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库