{"id":13462408,"url":"https://github.com/ouqiang/delay-queue","last_synced_at":"2025-04-12T19:52:53.958Z","repository":{"id":43957200,"uuid":"95505768","full_name":"ouqiang/delay-queue","owner":"ouqiang","description":"延迟队列","archived":false,"fork":false,"pushed_at":"2017-12-03T05:46:46.000Z","size":115,"stargazers_count":1188,"open_issues_count":13,"forks_count":241,"subscribers_count":46,"default_branch":"master","last_synced_at":"2025-04-12T19:52:27.778Z","etag":null,"topics":["delay-queue","delayed-job","delayqueue","golang","redis"],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ouqiang.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-06-27T01:44:17.000Z","updated_at":"2025-04-09T05:21:21.000Z","dependencies_parsed_at":"2022-08-27T20:02:56.769Z","dependency_job_id":null,"html_url":"https://github.com/ouqiang/delay-queue","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ouqiang%2Fdelay-queue","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ouqiang%2Fdelay-queue/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ouqiang%2Fdelay-queue/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ouqiang%2Fdelay-queue/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ouqiang","download_url":"https://codeload.github.com/ouqiang/delay-queue/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248625507,"owners_count":21135513,"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","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":["delay-queue","delayed-job","delayqueue","golang","redis"],"created_at":"2024-07-31T12:00:47.192Z","updated_at":"2025-04-12T19:52:53.933Z","avatar_url":"https://github.com/ouqiang.png","language":"Go","readme":"# delay-queue\n[![Go Report Card](https://goreportcard.com/badge/github.com/ouqiang/delay-queue)](https://goreportcard.com/report/github.com/ouqiang/delay-queue)\n[![Downloads](https://img.shields.io/github/downloads/ouqiang/delay-queue/total.svg)](https://github.com/ouqiang/delay-queue/releases)\n[![license](https://img.shields.io/github/license/mashape/apistatus.svg?maxAge=2592000)](https://github.com/ouqiang/delay-queue/blob/master/LICENSE)\n[![Release](https://img.shields.io/github/release/ouqiang/delay-queue.svg?label=Release)](https://github.com/ouqiang/delay-queue/releases)\n\n基于Redis实现的延迟队列, 参考[有赞延迟队列设计](http://tech.youzan.com/queuing_delay)实现\n\n## 应用场景\n* 订单超过30分钟未支付，自动关闭\n* 订单完成后, 如果用户一直未评价, 5天后自动好评\n* 会员到期前15天, 到期前3天分别发送短信提醒\n\n## 支付宝异步通知实现\n支付宝异步通知时间间隔是如何实现的(通知的间隔频率一般是：2m,10m,10m,1h,2h,6h,15h)  \n \n订单支付成功后, 生成通知任务, 放入消息队列中.    \n任务内容包含Array{0,0,2m,10m,10m,1h,2h,6h,15h}和通知到第几次N(这里N=1, 即第1次).    \n消费者从队列中取出任务, 根据N取得对应的时间间隔为0, 立即发送通知.   \n\n第1次通知失败, N += 1 =\u003e 2  \n从Array中取得间隔时间为2m, 添加一个延迟时间为2m的任务到延迟队列, 任务内容仍包含Array和N     \n\n第2次通知失败, N += 1 =\u003e 3, 取出对应的间隔时间10m, 添加一个任务到延迟队列, 同上   \n......    \n第7次通知失败, N += 1 =\u003e 8, 取出对应的间隔时间15h, 添加一个任务到延迟队列, 同上  \n第8次通知失败, N += 1 =\u003e 9, 取不到间隔时间, 结束通知    \n\n\n## 实现原理\n\u003e 利用Redis的有序集合，member为JobId, score为任务执行的时间戳,    \n每秒扫描一次集合，取出执行时间小于等于当前时间的任务.   \n\n## 依赖\n* Redis\n\n## 下载\n[releases](https://github.com/ouqiang/delay-queue/releases)\n\n\n## 源码安装\n* `go`语言版本1.7+\n* `go get -d github.com/ouqiang/delay-queue`\n* `go build`\n\n\n## 运行\n`./delay-queue -c delay-queue.conf`  \n\u003e HTTP Server监听`0.0.0.0:9277`, Redis连接地址`127.0.0.1:6379`, 数据库编号`1`\n\n## 客户端\n[PHP](https://github.com/ouqiang/delayqueue-php)\n\n## HTTP接口\n\n* 请求方法 `POST`   \n* 请求Body及返回值均为`json`\n\n### 返回值\n```json\n{\n  \"code\": 0,\n  \"message\": \"添加成功\",\n  \"data\": null\n}\n```\n\n|  参数名 |     类型    |     含义     |        备注       |\n|:-------:|:-----------:|:------------:|:-----------------:|\n|   code  |     int     |    状态码    | 0: 成功 非0: 失败 |\n| message |    string   | 状态描述信息 |                   |\n|   data  | object, null |   附加信息   |                   |\n\n### 添加任务   \nURL地址 `/push`   \n```json\n{\n  \"topic\": \"order\",\n  \"id\": \"15702398321\",\n  \"delay\": 3600,\n  \"ttr\": 120,\n  \"body\": \"{\\\"uid\\\": 10829378,\\\"created\\\": 1498657365 }\"\n}\n```\n|  参数名 |     类型    |     含义     |        备注       |\n|:-------:|:-----------:|:------------:|:-----------------:|\n|   topic  | string     |    Job类型                   |                     |\n|   id     | string     |    Job唯一标识                   | 需确保JobID唯一                  |\n|   delay  | int        |    Job需要延迟的时间, 单位：秒    |                   |\n|   ttr  | int        |    Job执行超时时间, 单位：秒   |                   |\n|   body   | string     |    Job的内容，供消费者做具体的业务处理，如果是json格式需转义 |                   |\n\n### 轮询队列获取任务\n服务端会Hold住连接, 直到队列中有任务或180秒后超时返回,   \n任务执行完成后需调用`finish`接口删除任务, 否则任务会重复投递, 消费端需能处理同一任务的多次投递   \n\n \nURL地址 `/pop`    \n```json\n{\n  \"topic\": \"order\"\n}\n```\n|  参数名 |     类型    |     含义     |        备注       |\n|:-------:|:-----------:|:------------:|:-----------------:|\n|   topic  | string     |    Job类型                   |                     |\n\n\n队列中有任务返回值\n```json\n{\n  \"code\": 0,\n  \"message\": \"操作成功\",\n  \"data\": {\n    \"id\": \"15702398321\",\n    \"body\": \"{\\\"uid\\\": 10829378,\\\"created\\\": 1498657365 }\"\n  }\n}\n```\n队列为空返回值   \n```json\n{\n  \"code\": 0,\n  \"message\": \"操作成功\",\n  \"data\": null\n}\n```\n\n\n### 删除任务  \nURL地址 `/delete`   \n\n```json\n{\n  \"id\": \"15702398321\"\n}\n```\n\n|  参数名 |     类型    |     含义     |        备注       |\n|:-------:|:-----------:|:------------:|:-----------------:|\n|   id  | string     |    Job唯一标识       |            |\n\n\n### 完成任务   \nURL地址 `/finish`   \n\n```json\n{\n  \"id\": \"15702398321\"\n}\n```\n\n|  参数名 |     类型    |     含义     |        备注       |\n|:-------:|:-----------:|:------------:|:-----------------:|\n|   id  | string     |    Job唯一标识    |                     |\n\n\n### 查询任务  \nURL地址 `/get`   \n\n```json\n{\n  \"id\": \"15702398321\"\n}\n```\n\n|  参数名 |     类型    |     含义     |        备注       |\n|:-------:|:-----------:|:------------:|:-----------------:|\n|   id  | string     |    Job唯一标识       |            |\n\n\n返回值\n```json\n{\n    \"code\": 0,\n    \"message\": \"操作成功\",\n    \"data\": {\n        \"topic\": \"order\",\n        \"id\": \"15702398321\",\n        \"delay\": 1506787453,\n        \"ttr\": 60,\n        \"body\": \"{\\\"uid\\\": 10829378,\\\"created\\\": 1498657365 }\"\n    \n    }\n}\n```\n\n|  参数名 |     类型    |     含义     |        备注       |\n|:-------:|:-----------:|:------------:|:-----------------:|\n|   topic  | string     |    Job类型                   |                     |\n|   id     | string     |    Job唯一标识           |                   |\n|   delay  | int        |    Job延迟执行的时间戳    |                   |\n|   ttr  | int        |    Job执行超时时间, 单位：秒   |                   |\n|   body   | string     |    Job内容，供消费者做具体的业务处理 | \n\n\nJob不存在返回值  \n```json\n{\n  \"code\": 0,\n  \"message\": \"操作成功\",\n  \"data\": null\n}\n```\n  ","funding_links":[],"categories":["Uncategorized","Go"],"sub_categories":["Uncategorized"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fouqiang%2Fdelay-queue","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fouqiang%2Fdelay-queue","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fouqiang%2Fdelay-queue/lists"}