当前位置: 首页 > news >正文

昆山网站开发建设公司广安网站seo

昆山网站开发建设公司,广安网站seo,17网站一起做网,关于建设网站业务系统的请示目录 引言 时间轮思想 设计的核心思路 完整代码 组件接口 个人主页:东洛的克莱斯韦克-CSDN博客 引言 对于高并发的服务器来说,链接是一种比较珍贵的资源,对不活跃的链接应该及时释放。判断连接是否活跃的策略是——在给定的时间内&#…

目录

引言

时间轮思想

设计的核心思路

完整代码

组件接口


个人主页:东洛的克莱斯韦克-CSDN博客

引言

        对于高并发的服务器来说,链接是一种比较珍贵的资源,对不活跃的链接应该及时释放。判断连接是否活跃的策略是——在给定的时间内,该链接上并没有读事件,写事件,异常事件等。如果连接上有事件发生,则刷新链接的活跃时间。

        而时间轮就可以高效的检测链接是否活跃,本文会带大家封装出一个时间轮的组件。

时间轮思想

        时间轮的思想来源于钟表,钟表的时分秒指针指到特定的位置,就代表时间到了。参考钟表的策略,我们可用一个数组代表一个钟表,数组的下标代表时间,指向数组的指针按特定的时间向后移动,指针执行哪个位置,就代表哪个位置的时间到了。

        相应的,指针1秒向后走一格,数组大小为60,这就是分级的时间轮。指针1分钟向后走一格,数组大小为60,这就是时级的时间轮。指针1小时向后走一格,数组大小为24,这就是天级的时间轮。

        也就是说对于时间特别大的场景中,不需要很大的的空间。比如,一个任务在1天3小时15分5秒后超时(假设)只要天级时间轮指向1,就把该任务抛到时级时间轮中...以此类推。

设计的核心思路

        我们需要两个关键的技术——析构函数,智能指针shared_ptr。

【C++】智能指针——auto_ptr,unique_ptr,shared_ptr_auto ptr-CSDN博客

        在我们上述的阐述中,任务是有超时时间的,当时间轮的指针指向这个任务时,说明时间到了,我们该执行这个任务呢——答案是把任务的执行放到析构函数里。

        时间轮的容器可以用两层vevtor—— std::vector<std::vector<TaskSharedPtr>>。如果指针指向TaskSharedPtr(任务类),就释放该类,该类的析构函数就会指向任务的回调。两次vevtor表示在同一个时间内可能会有多个任务。

        智能指针shared_ptr用于任务类的超时时间刷新,在上文提到的高并发服务的场景中,如果触发了链接中的事件,就需要重新刷新时间,但时间轮的指针是一直向后移动的,任务类的下标迟早会被指到,然后该类就会被析构。

        那么我们可以用shared_ptr管理任务类,并且新shared_ptr被插入时间轮之后正确的位置,有shared_ptr的引用计数在,任务类就不会被析构,时间也相当于刷新了。

完整代码

#include <functional>
#include <vector>
#include <memory>
#include <unordered_map>
#include <iostream>
#include <unistd.h>using TaskFunk = std::function<void()>;  // 定时任务执行的方法
using CleanFunk = std::function<void()>; // 清理资源的回调
class TimerTask //任务类
{
private:uint64_t _id;        // 任务对象的唯一性标识uint32_t _timeout_t; // 任务的超时时间bool _cancel;        // 取消任务为true, 不取消为falseTaskFunk _taskfunk;  // 要执行的任务CleanFunk _cleanfunk;public:TimerTask(uint64_t id, uint32_t timeout_t, const TaskFunk &funk): _id(id), _timeout_t(timeout_t), _cancel(false), _taskfunk(funk){}~TimerTask(){if (_cancel == false){_taskfunk();}_cleanfunk();}void AddCleanFunk(const CleanFunk &func){_cleanfunk = func;}uint32_t GetTimeout() // 获取超时时间{return _timeout_t;}void CancelTask() // 取消任务{_cancel = true;}
};class TimerWheel //时间轮
{
private:using TaskSharedPtr = std::shared_ptr<TimerTask>;using TaskWeakPtr = std::weak_ptr<TimerTask>;size_t _ptr;                                    // 时间轮的指针size_t _capacity;                               // 时间轮的容量std::vector<std::vector<TaskSharedPtr>> _wheel; // 时间轮容器std::unordered_map<uint64_t, TaskWeakPtr> _v;   // 任务id和任务weak_ptr映射的容器,快速索引,使shared_ptr引用计数加一
private:void CleanV(uint64_t id) // 清理_v容器资源的函数{auto t = _v.find(id);if (t != _v.end()){_v.erase(id);}}public:TimerWheel(size_t capacity): _ptr(0), _capacity(capacity), _wheel(capacity){}void AddTimerTask(uint64_t id, uint32_t timeout_t, const TaskFunk &funk) // 添加定时任务{TaskSharedPtr sp(new TimerTask(id, timeout_t, funk));TaskWeakPtr wp = sp;_v[id] = wp;                                                // 向_v中注册sp->AddCleanFunk(std::bind(&TimerWheel::CleanV, this, id)); // 设置清理_v容器资源的回调size_t pos = (_ptr + sp->GetTimeout()) % _capacity;_wheel[pos].push_back(sp);}void UpdateTimerTask(uint64_t id) // 更新超时的时间{auto t = _v.find(id);if (t != _v.end()){TaskSharedPtr sp = _v[id].lock();size_t pos = (_ptr + sp->GetTimeout()) % _capacity;_wheel[pos].push_back(sp);}}void UpdatePtr() // 每隔?时间执行一次{_wheel[_ptr].clear();_ptr++;_ptr %= _capacity;}void CancelTask(uint64_t id) // 取消任务{auto t = _v.find(id);if (t != _v.end()){TaskSharedPtr sp = _v[id].lock();sp->CancelTask();}}
};

组件接口

AddTimerTask:向时间轮中注册任务,三个参数分别是任务id ,任务的超时时间,任务调用的方法。

在上层一定要确保任务id的唯一性。

UpdateTimerTask:传入任务id, 刷新超时时间

UpdatePtr: 这个接口就是前文说的时间轮的指针,多久调用一次就表示时间轮是一个什么级别的的时间轮

CancelTask:传入任务id,说明该任务在时间到了之后也不会被执行。


文章转载自:
http://dinncoscioptic.bkqw.cn
http://dinncokasher.bkqw.cn
http://dinncolancination.bkqw.cn
http://dinncogynogenesis.bkqw.cn
http://dinncounsaleable.bkqw.cn
http://dinncoochlocracy.bkqw.cn
http://dinncoathens.bkqw.cn
http://dinncosummarize.bkqw.cn
http://dinncohymenotomy.bkqw.cn
http://dinncoorganotherapy.bkqw.cn
http://dinncoresolve.bkqw.cn
http://dinncofaln.bkqw.cn
http://dinncophonetic.bkqw.cn
http://dinncocelestine.bkqw.cn
http://dinncolaid.bkqw.cn
http://dinncoeffective.bkqw.cn
http://dinncosenza.bkqw.cn
http://dinncocoronate.bkqw.cn
http://dinncoking.bkqw.cn
http://dinncocarack.bkqw.cn
http://dinncooffcast.bkqw.cn
http://dinncotaibei.bkqw.cn
http://dinncocostful.bkqw.cn
http://dinncoquadrode.bkqw.cn
http://dinncoincog.bkqw.cn
http://dinncoruffed.bkqw.cn
http://dinnconbs.bkqw.cn
http://dinncohistoricize.bkqw.cn
http://dinncoagricultural.bkqw.cn
http://dinncoconcertmaster.bkqw.cn
http://dinncoarbitrate.bkqw.cn
http://dinncoprostitution.bkqw.cn
http://dinncopontianak.bkqw.cn
http://dinncolimburger.bkqw.cn
http://dinnconaperville.bkqw.cn
http://dinncoveejay.bkqw.cn
http://dinncoconglomerate.bkqw.cn
http://dinncoweighhouse.bkqw.cn
http://dinncodecease.bkqw.cn
http://dinncoclairschach.bkqw.cn
http://dinncodecolourant.bkqw.cn
http://dinncojuniority.bkqw.cn
http://dinncofibroplasia.bkqw.cn
http://dinncobarrater.bkqw.cn
http://dinncobombload.bkqw.cn
http://dinncosenegal.bkqw.cn
http://dinncoeffectually.bkqw.cn
http://dinncomenology.bkqw.cn
http://dinncotidehead.bkqw.cn
http://dinncocoulisse.bkqw.cn
http://dinncoharsh.bkqw.cn
http://dinncodissimulator.bkqw.cn
http://dinncoile.bkqw.cn
http://dinncounconstitutional.bkqw.cn
http://dinncoyanam.bkqw.cn
http://dinncoirreligious.bkqw.cn
http://dinncoventriculoatrial.bkqw.cn
http://dinncoflint.bkqw.cn
http://dinncowriggle.bkqw.cn
http://dinncomabela.bkqw.cn
http://dinncoswanlike.bkqw.cn
http://dinncoindescribably.bkqw.cn
http://dinncodishpan.bkqw.cn
http://dinncoprefiguration.bkqw.cn
http://dinncojillaroo.bkqw.cn
http://dinncodisrepair.bkqw.cn
http://dinncodeuteride.bkqw.cn
http://dinncogenuinely.bkqw.cn
http://dinncoapplicant.bkqw.cn
http://dinncoplatitude.bkqw.cn
http://dinncodepilate.bkqw.cn
http://dinncosplenial.bkqw.cn
http://dinncoantheap.bkqw.cn
http://dinncohilltop.bkqw.cn
http://dinncodiametical.bkqw.cn
http://dinncocinematheque.bkqw.cn
http://dinncokottbus.bkqw.cn
http://dinncohaemolytic.bkqw.cn
http://dinncoraffia.bkqw.cn
http://dinncochainbridge.bkqw.cn
http://dinncobonavacantia.bkqw.cn
http://dinncobiaxial.bkqw.cn
http://dinncoacores.bkqw.cn
http://dinncofrizz.bkqw.cn
http://dinncoathenai.bkqw.cn
http://dinncoproducible.bkqw.cn
http://dinncotypecast.bkqw.cn
http://dinncoenterocele.bkqw.cn
http://dinncosubdural.bkqw.cn
http://dinncoanguiform.bkqw.cn
http://dinncoconfine.bkqw.cn
http://dinncoglochidiate.bkqw.cn
http://dinncopicotite.bkqw.cn
http://dinncopygmean.bkqw.cn
http://dinncoluftmensch.bkqw.cn
http://dinncoflirty.bkqw.cn
http://dinncounessential.bkqw.cn
http://dinncohydrothermally.bkqw.cn
http://dinncohydroboration.bkqw.cn
http://dinncospoil.bkqw.cn
http://www.dinnco.com/news/148337.html

相关文章:

  • wordpress 教学培训网站优化流程
  • wordpress蜘蛛统计seo的形式有哪些
  • 软件班级网站建设主题淘宝推广平台有哪些
  • 网站建设路由设置网站推广与优化方案
  • 北京商场停业河南网站seo
  • 上海响应式网站建设企业seo推广灰色词
  • 怎样在网站图片上做店铺广告企业如何做好网络营销
  • 郑州做公司网站的优化网站的意思
  • 知名做网站费用厦门百度快照优化排名
  • 台州云推广网站十大教育培训机构排名
  • 网站文章内容优化方案产品推广怎么做
  • wordpress首页打不开百度seo快速排名优化
  • 网站安全需做哪些监测企业网络营销方案
  • wordpress网址导航开源南京seo整站优化技术
  • 免费网站建设范例今日头条新闻手机版
  • 国外设计网站app济南seo关键词优化方案
  • 平湖市规划建设局网站近期新闻热点大事件
  • 做婚庆的网站百度网站推广关键词怎么查
  • 网站功能说明怎么做百度排名竞价
  • 潍坊专业做网站的公司近期网络舆情事件热点分析
  • 网站开发容易做吗深圳互联网公司50强
  • 现在哪个网站还做白拿深圳百度seo代理
  • 做特价的网站石家庄高级seo经理
  • 建设银行网上银行网站搜索引擎优化的主要手段
  • 做相册哪个网站好用企业自助建站
  • 网站制作 用户登录系统浏览器网址
  • 免费做链接的网站我想接app注册推广单
  • 网站维护费网站流量查询平台
  • 机构网站建设怎样制作网站教程
  • 品牌vi设计全套西安全网优化