{"id":15532059,"url":"https://github.com/togettoyou/mq-delay","last_synced_at":"2026-06-20T23:32:07.591Z","repository":{"id":57573175,"uuid":"350945049","full_name":"togettoyou/mq-delay","owner":"togettoyou","description":"基于RabbitMQ实现的延时消息队列","archived":false,"fork":false,"pushed_at":"2021-03-25T02:20:10.000Z","size":51,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-06-01T09:16:29.774Z","etag":null,"topics":["delay-queue","rabbitmq","rabbitmq-client"],"latest_commit_sha":null,"homepage":"","language":"Go","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/togettoyou.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":"2021-03-24T04:25:31.000Z","updated_at":"2022-01-21T09:24:13.000Z","dependencies_parsed_at":"2022-09-26T19:01:05.596Z","dependency_job_id":null,"html_url":"https://github.com/togettoyou/mq-delay","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/togettoyou/mq-delay","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/togettoyou%2Fmq-delay","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/togettoyou%2Fmq-delay/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/togettoyou%2Fmq-delay/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/togettoyou%2Fmq-delay/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/togettoyou","download_url":"https://codeload.github.com/togettoyou/mq-delay/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/togettoyou%2Fmq-delay/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34589204,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-20T02:00:06.407Z","response_time":98,"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":["delay-queue","rabbitmq","rabbitmq-client"],"created_at":"2024-10-02T11:28:58.862Z","updated_at":"2026-06-20T23:32:07.568Z","avatar_url":"https://github.com/togettoyou.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 基于RabbitMQ实现的延时消息队列\n\n# 使用\n```shell\ngo get -v github.com/togettoyou/mq-delay\n```\n\n# 应用场景\n\n1. 对消息生产和消费有时间窗口要求的场景。例如，在电商交易中超时未支付关闭订单的场景，在订单创建时会发送一条延时消息。这条消息将会在30分钟以后投递给消费者，消费者收到此消息后需要判断对应的订单是否已完成支付。如支付未完成，则关闭订单。如已完成支付则忽略。\n\n1. 通过消息触发延时任务的场景。例如，在指定时间段之后向用户发送提醒消息。\n\n# 实现机制\n\n| 机制 | 实现 |\n| ------------ | ------------- |\n| 死信Exchange+Queue的消息存活时间 | |\n| 死信Exchange+消息的消息存活时间 | |\n| [rabbitmq-delayed-message-exchange插件](https://github.com/rabbitmq/rabbitmq-delayed-message-exchange) | ✔️ |\n\n# Example\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"github.com/streadway/amqp\"\n\t\"github.com/togettoyou/mq-delay\"\n\t\"log\"\n\t\"net/http\"\n\t_ \"net/http/pprof\"\n\t\"sync\"\n\t\"time\"\n)\n\nconst (\n\turl               = \"amqp://guest:guest@127.0.0.1:5672/\"\n\tdelayExchangeName = \"delayed.exchange\"\n\troutingKey        = \"hello-delayed-routingKey\"\n\tqueueName         = \"hello-queue\"\n\tconsumerTag       = \"hello-consumer\"\n)\n\nfunc main() {\n\t// 创建客户端\n\tcli, err := mq_delay.NewClient(url)\n\tfail(err)\n\t// 创建延时交换机\n\tfail(cli.CreateDelayExchange(delayExchangeName))\n\n\t// 构建消费者\n\tconsumer := cli.GetConsumer()\n\t// 创建队列\n\tqueue, err := consumer.CreateQueue(queueName)\n\tfail(err)\n\t// 绑定队列\n\tfail(consumer.BindQueue(queue.Name, delayExchangeName, routingKey, false, nil))\n\t// 实时接受消息\n\tfail(consumer.Receive(queue.Name, consumerTag, func(d amqp.Delivery) {\n\t\tlog.Println(d.DeliveryTag, string(d.Body))\n\t}))\n\n\t// 构建生产者\n\tmu := sync.Mutex{}\n\tcond := sync.NewCond(\u0026mu)\n\tproducer := cli.GetProducer()\n\tfor i := 0; i \u003c 100000; i++ {\n\t\tgo func() {\n\t\t\tcond.L.Lock()\n\t\t\tdefer cond.L.Unlock()\n\t\t\tcond.Wait()\n\t\t\tfmt.Println(\"开始\")\n\t\t\tfail(producer.SendTimeoutMsg(delayExchangeName, routingKey, []byte(\"你好-\"+time.Now().String()), 5))\n\t\t}()\n\t}\n\tgo func() {\n\t\ttime.Sleep(10 * time.Second)\n\t\tcond.Broadcast()\n\t}()\n\tfail(http.ListenAndServe(\":8999\", nil))\n}\n\nfunc fail(err error) {\n\tif err != nil {\n\t\tlog.Fatalln(err.Error())\n\t}\n}\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftogettoyou%2Fmq-delay","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftogettoyou%2Fmq-delay","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftogettoyou%2Fmq-delay/lists"}