{"id":33988280,"url":"https://github.com/esd-projects/hashed-wheel-timer-plugin","last_synced_at":"2025-12-13T05:55:32.696Z","repository":{"id":56979241,"uuid":"193846921","full_name":"esd-projects/hashed-wheel-timer-plugin","owner":"esd-projects","description":"HashedWheelTimer","archived":false,"fork":false,"pushed_at":"2019-06-27T08:54:12.000Z","size":26,"stargazers_count":2,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-11-27T14:56:29.521Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/esd-projects.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-06-26T06:54:54.000Z","updated_at":"2023-12-11T06:17:27.000Z","dependencies_parsed_at":"2022-08-21T11:50:36.805Z","dependency_job_id":null,"html_url":"https://github.com/esd-projects/hashed-wheel-timer-plugin","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/esd-projects/hashed-wheel-timer-plugin","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/esd-projects%2Fhashed-wheel-timer-plugin","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/esd-projects%2Fhashed-wheel-timer-plugin/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/esd-projects%2Fhashed-wheel-timer-plugin/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/esd-projects%2Fhashed-wheel-timer-plugin/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/esd-projects","download_url":"https://codeload.github.com/esd-projects/hashed-wheel-timer-plugin/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/esd-projects%2Fhashed-wheel-timer-plugin/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":27701208,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-12-13T02:00:09.769Z","response_time":147,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-12-13T05:55:32.211Z","updated_at":"2025-12-13T05:55:32.688Z","avatar_url":"https://github.com/esd-projects.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# hashed-wheel-timer-plugin\nHashedWheelTimer是采用一种定时轮的方式来管理和维护大量的Timer调度算法。\n一个 HashedWheelTimer 是环形结构，类似一个时钟，分为很多槽，一个槽代表一个时间间隔，每个槽又对应一个类似Map结构的对象，使用双向链表存储定时任务，指针周期性的跳动，跳动到一个槽位，就执行该槽位的定时任务。\n环形结构可以根据超时时间的 hash 值(这个 hash 值实际上就是ticks \u0026 mask)将 task 分布到不同的槽位中, 当 tick 到那个槽位时, 只需要遍历那个槽位的 task 即可知道哪些任务会超时(而使用线性结构, 你每次 tick 都需要遍历所有 task), 所以, 我们任务量大的时候, 相应的增加 wheel 的 ticksPerWheel 值, 可以减少 tick 时遍历任务的个数.\n\n本插件模拟java HashedWheelTimer 的实现。使用redis做持久存储。\n\n# 配置方法\n配置一个环形结构池\n~~~\nhashedWheelTimer:\n  db: default\n  max_pending_timeouts: 100\n  wheel:\n    - {name: aaa, tick_duration: 1, ticks_per_wheel: 60 }\n~~~\nname  池子名称，投递任务的时候需要\n\ntick_duration，时间间隔，1秒\n\nticks_per_wheel，时间槽数量，60个，代表一分钟一个轮次\n\nmax_pending_timeouts，每一个槽允许的最大协程数，默认100，如果耗时任务或者队列特别长需要适当增加时间槽数量比如3600。不适合特别精准的延时场景。\n\ndb，使用的 redis 配置，强烈建议不要使用default，会占用http服务的连接数，应该复制一份配置专用\n\n# 使用方法\n\n## 投递任务\n~~~\n  //环形池名称，执行任务的类，投递参数，延迟执行时间，秒\n  $this-\u003eaddTask('aaa', TimerTask::class,['a'=\u003e'b', 'time' =\u003e time()], 60);\n~~~\n\n## 投递类\n投递的类需要继承 HashedWheelTimerRunnable \n~~~\n\u003c?php\nnamespace app\\Controller;\n\nuse ESD\\Plugins\\HashedWheelTimer\\HashedWheelTimerRunnable;\n\nclass  TimerTask extends HashedWheelTimerRunnable{\n\n    public function run()\n    {\n        /**\n        //此处可根据延迟次数设置不同的延迟时间，比如支付通知失败\n        if($this-\u003egetDelayTimes() \u003c= 1){\n            $this-\u003esetDelayTTL(10);\n        }else if ($this-\u003egetDelayTimes() \u003c= 2){\n            $this-\u003esetDelayTTL(20);\n        } else if ($this-\u003egetDelayTimes() \u003c= 3){\n            $this-\u003esetDelayTTL(30);\n        } else if ($this-\u003egetDelayTimes() \u003c= 4){\n            $this-\u003esetDelayTTL(40);\n        }else if ($this-\u003egetDelayTimes() \u003c= 5){\n            $this-\u003esetDelayTTL(40);\n        }\n         * **/\n\n\n        //如果 return false 或者该类触发任意异常，系统会将此任务重新投递到下一次执行的位置。\n        //如果不需要失败重试，需要 return true。可通过 getRetryTimes 获取重试次数进行判断\n        //如超过5次则不再重试，直接return true。\n        $this-\u003egetRetryTimes()\n\n\n        //获取投递参数\n        $params = $this-\u003egetParams();\n        // TODO: Implement run() method.\n\n        $this-\u003einfo('run', $params);\n        //如果执行 setDelayTTL ， 需要return true ，否则会被重新投递到下一次执行的位置。\n        return true;\n    }\n}\n~~~\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fesd-projects%2Fhashed-wheel-timer-plugin","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fesd-projects%2Fhashed-wheel-timer-plugin","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fesd-projects%2Fhashed-wheel-timer-plugin/lists"}