All Projects → ouqiang → Delay Queue

ouqiang / Delay Queue

Licence: mit
延迟队列

Programming Languages

go
31211 projects - #10 most used programming language
golang
3204 projects

Labels

Projects that are alternatives of or similar to Delay Queue

Springbootunity
rabbitmq、redis、scheduled、socket、mongodb、Swagger2、spring data jpa、Thymeleaf、freemarker etc. (muti module spring boot project) (with spring boot framework,different bussiness scence with different technology。)
Stars: ✭ 845 (-4.41%)
Mutual labels:  redis
Mysql redis replication
MySQL replication to Redis cache server via Ruby, Gearman, triggers, and MySQL UDF (Ubuntu version)
Stars: ✭ 10 (-98.87%)
Mutual labels:  redis
Ssm redis template
An template based on Maven, using Spring + Spring MVC + mybatis + spring-data-redis frames. It can be used to construct a new Java Web Appliaction quickly
Stars: ✭ 13 (-98.53%)
Mutual labels:  redis
Weixinmpsdk
微信全平台 SDK Senparc.Weixin for C#,支持 .NET Framework 及 .NET Core、.NET 6.0。已支持微信公众号、小程序、小游戏、企业号、企业微信、开放平台、微信支付、JSSDK、微信周边等全平台。 WeChat SDK for C#.
Stars: ✭ 7,098 (+702.94%)
Mutual labels:  redis
Hubot Env
Hubot manages environment variables in process.env and redis via commands
Stars: ✭ 10 (-98.87%)
Mutual labels:  redis
Predixy
A high performance and fully featured proxy for redis, support redis sentinel and redis cluster
Stars: ✭ 862 (-2.49%)
Mutual labels:  redis
Bestnote
👊 持续更新,Java Android 近几年最全面的技术点以及面试题 供自己学习使用
Stars: ✭ 841 (-4.86%)
Mutual labels:  redis
Redis Ratelimit
A fixed window rate limiter based on Redis
Stars: ✭ 15 (-98.3%)
Mutual labels:  redis
Netkiller.github.io
Netkiller Free ebook - 免费电子书
Stars: ✭ 861 (-2.6%)
Mutual labels:  redis
Javaok
必看!java后端,亮剑诛仙。java发展路线技术要点。
Stars: ✭ 867 (-1.92%)
Mutual labels:  redis
Predis
A flexible and feature-complete Redis client for PHP.
Stars: ✭ 6,966 (+688.01%)
Mutual labels:  redis
Redisbloom
Probabilistic Datatypes Module for Redis
Stars: ✭ 858 (-2.94%)
Mutual labels:  redis
Metta
An information security preparedness tool to do adversarial simulation.
Stars: ✭ 867 (-1.92%)
Mutual labels:  redis
Walrus
Lightweight Python utilities for working with Redis
Stars: ✭ 846 (-4.3%)
Mutual labels:  redis
Activemq
高性能消息监控无服务器,使用ActiveMQ消息中间件实现,使用MQ、redis、nlog 等技术实现
Stars: ✭ 13 (-98.53%)
Mutual labels:  redis
Puppet Redis cluster
Install multiple redis instances via Puppet to configure a cluster with redis-trib.rb (for Redis version >= 3.0)
Stars: ✭ 8 (-99.1%)
Mutual labels:  redis
Unredis
Redis Monitoring tool written in Golang and React.js
Stars: ✭ 11 (-98.76%)
Mutual labels:  redis
Treefrog Framework
TreeFrog Framework : High-speed C++ MVC Framework for Web Application
Stars: ✭ 885 (+0.11%)
Mutual labels:  redis
Go Demo
Go语言实例教程从入门到进阶,包括基础库使用、设计模式、面试易错点、工具类、对接第三方等
Stars: ✭ 881 (-0.34%)
Mutual labels:  redis
Tutorial Graphql Subscriptions Redis
GraphQL server implementation with Redis backing allowing pubsub
Stars: ✭ 12 (-98.64%)
Mutual labels:  redis

delay-queue

Go Report Card Downloads license Release

基于Redis实现的延迟队列, 参考有赞延迟队列设计实现

应用场景

  • 订单超过30分钟未支付,自动关闭
  • 订单完成后, 如果用户一直未评价, 5天后自动好评
  • 会员到期前15天, 到期前3天分别发送短信提醒

支付宝异步通知实现

支付宝异步通知时间间隔是如何实现的(通知的间隔频率一般是:2m,10m,10m,1h,2h,6h,15h)  

订单支付成功后, 生成通知任务, 放入消息队列中.
任务内容包含Array{0,0,2m,10m,10m,1h,2h,6h,15h}和通知到第几次N(这里N=1, 即第1次).
消费者从队列中取出任务, 根据N取得对应的时间间隔为0, 立即发送通知.

第1次通知失败, N += 1 => 2
从Array中取得间隔时间为2m, 添加一个延迟时间为2m的任务到延迟队列, 任务内容仍包含Array和N

第2次通知失败, N += 1 => 3, 取出对应的间隔时间10m, 添加一个任务到延迟队列, 同上
......
第7次通知失败, N += 1 => 8, 取出对应的间隔时间15h, 添加一个任务到延迟队列, 同上
第8次通知失败, N += 1 => 9, 取不到间隔时间, 结束通知

实现原理

利用Redis的有序集合,member为JobId, score为任务执行的时间戳,
每秒扫描一次集合,取出执行时间小于等于当前时间的任务.

依赖

  • Redis

下载

releases

源码安装

  • go语言版本1.7+
  • go get -d github.com/ouqiang/delay-queue
  • go build

运行

./delay-queue -c delay-queue.conf

HTTP Server监听0.0.0.0:9277, Redis连接地址127.0.0.1:6379, 数据库编号1

客户端

PHP

HTTP接口

  • 请求方法 POST
  • 请求Body及返回值均为json

返回值

{
  "code": 0,
  "message": "添加成功",
  "data": null
}
参数名 类型 含义 备注
code int 状态码 0: 成功 非0: 失败
message string 状态描述信息
data object, null 附加信息

添加任务

URL地址 /push

{
  "topic": "order",
  "id": "15702398321",
  "delay": 3600,
  "ttr": 120,
  "body": "{\"uid\": 10829378,\"created\": 1498657365 }"
}
参数名 类型 含义 备注
topic string Job类型
id string Job唯一标识 需确保JobID唯一
delay int Job需要延迟的时间, 单位:秒
ttr int Job执行超时时间, 单位:秒
body string Job的内容,供消费者做具体的业务处理,如果是json格式需转义

轮询队列获取任务

服务端会Hold住连接, 直到队列中有任务或180秒后超时返回,
任务执行完成后需调用finish接口删除任务, 否则任务会重复投递, 消费端需能处理同一任务的多次投递

URL地址 /pop

{
  "topic": "order"
}
参数名 类型 含义 备注
topic string Job类型

队列中有任务返回值

{
  "code": 0,
  "message": "操作成功",
  "data": {
    "id": "15702398321",
    "body": "{\"uid\": 10829378,\"created\": 1498657365 }"
  }
}

队列为空返回值

{
  "code": 0,
  "message": "操作成功",
  "data": null
}

删除任务

URL地址 /delete

{
  "id": "15702398321"
}
参数名 类型 含义 备注
id string Job唯一标识

完成任务

URL地址 /finish

{
  "id": "15702398321"
}
参数名 类型 含义 备注
id string Job唯一标识

查询任务

URL地址 /get

{
  "id": "15702398321"
}
参数名 类型 含义 备注
id string Job唯一标识

返回值

{
    "code": 0,
    "message": "操作成功",
    "data": {
        "topic": "order",
        "id": "15702398321",
        "delay": 1506787453,
        "ttr": 60,
        "body": "{\"uid\": 10829378,\"created\": 1498657365 }"
    
    }
}
参数名 类型 含义 备注
topic string Job类型
id string Job唯一标识
delay int Job延迟执行的时间戳
ttr int Job执行超时时间, 单位:秒
body string Job内容,供消费者做具体的业务处理

Job不存在返回值

{
  "code": 0,
  "message": "操作成功",
  "data": null
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].