{"id":36492055,"url":"https://github.com/lpong/rmq","last_synced_at":"2026-01-12T01:56:52.038Z","repository":{"id":61839465,"uuid":"551440458","full_name":"lpong/rmq","owner":"lpong","description":"go redis queue","archived":false,"fork":false,"pushed_at":"2023-02-22T11:18:15.000Z","size":54,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-11-15T07:12:13.743Z","etag":null,"topics":["async","go","queue","queue-message","redis"],"latest_commit_sha":null,"homepage":"","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/lpong.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,"governance":null}},"created_at":"2022-10-14T12:05:11.000Z","updated_at":"2022-10-22T02:43:28.000Z","dependencies_parsed_at":"2023-01-30T17:25:14.207Z","dependency_job_id":null,"html_url":"https://github.com/lpong/rmq","commit_stats":null,"previous_names":["lea21st/rmq"],"tags_count":9,"template":false,"template_full_name":null,"purl":"pkg:github/lpong/rmq","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lpong%2Frmq","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lpong%2Frmq/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lpong%2Frmq/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lpong%2Frmq/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lpong","download_url":"https://codeload.github.com/lpong/rmq/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lpong%2Frmq/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28331369,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-12T00:36:25.062Z","status":"ssl_error","status_checked_at":"2026-01-12T00:36:15.229Z","response_time":60,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"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":["async","go","queue","queue-message","redis"],"created_at":"2026-01-12T01:56:51.967Z","updated_at":"2026-01-12T01:56:52.018Z","avatar_url":"https://github.com/lpong.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Rmq\n\n## 概述\n\n一个用go写的redis消息队列\n\n## Import\n\n`go get github.com/lpong/rmq`\n\n[https://github.com/lpong/rmq](https://github.com/lpong/rmq) 欢迎提交 Issues 和 Pull Requests\n## 创建Task\n```\n// Test1 ,仅一个函数\nfunc Test1(ctx context.Context, msg *rmq.Message) (result string, err error) {\n    return\n}\n\n// TestTask，实现 rmq.Task\ntype TestTask struct {\n    Name string `json:\"name\"`\n    Val  int    `json:\"val\"`\n}\nfunc (t *TestTask) TaskName() string {\n    // TODO implement me\n    return \"TestTask\"\n}\n\nfunc (t *TestTask) Run(ctx context.Context) (result string, err error) {\n    return \"ok\", nil\n}\n\n// 自带的Task\nrmq.CommandTask 执行一个系统命令\nrmq.HttpTask: 执行一个http请求\n\n// 定义解析task的数据，默认使用json.Unmarshal()\ntype TaskScanner interface {\n    Scan(src []byte) error\n}\n\n// 定义task的数据序列号方式，默认json.Marshal()\ntype TaskValuer interface {\n    Value() ([]byte, error)\n}\n\n// OnLoad 加载时执行的方法\ntype OnLoad interface {\n    Load(ctx context.Context, msg *Message) error\n}\n\n// OnSuccess 执行成功时执行的方法\ntype OnSuccess interface {\n    OnSuccess(ctx context.Context)\n}\n\n// OnFail 执行失败时执行的方法\ntype OnFail interface {\n    OnFail(ctx context.Context)\n}\n\n// OnComplete 执行完成时会调用\ntype OnComplete interface {\n    OnComplete(ctx context.Context)\n}\n\n```\n\n## 创建消息\n```\nmsg, err := rmq.NewMsg().SetTask(\u0026TestTask{\n    Name: \"name-1\",\n    Val:  1,\n})\n\n// 定制消息\nmsg := rmq.NewMsg() // OR msg := rmq.NewBlankMsg()\nmsg.SetMeta(rmq.RetryMeta).SetDelay(3 * time.Minute).SetMaxRetry(1).SetTraceId(\"traceId\").SetTimeout(30 * time.Second).SetExpire(30 * time.Second).SetExpiredAt(time.Now().Add(1 * time.Hour))\n\n// 该消息要执行的任务\nmsg.SetRawTask(\"test1\", map[string]any{\"x\": 1})\n// or \nmsg.SetTask(\u0026TestTask{\n    Name: fmt.Sprintf(\"testTask-%d\", i),\n    Val:  i * i,\n})\n\nqueue.Push(msg)\n```\n\n## 创建队列\n\n```\nrdb := redis.NewClient(\u0026redis.Options{\n    Addr:     \"localhost:6379\",\n    Password: \"\",\n    DB:       0,\n})\n\nlog := rmq.DefaultLog\nbroker := rmq.NewRedisBroker(rdb, rmq.DefaultRedisBrokerConfig, log)\nqueue = rmq.NewRmq(broker, log)\nqueue.RegisterFunc(\"test1\", Test1)\nqueue.Register(\u0026TestTask{})\n```\n\n## 消费\n```\nqueue.StartWorker(\u0026rmq.WorkerConfig{\n    WorkerNum:  2, //多pod环境下，建议设置为1，主要影响获取消息的速度\n    Concurrent: 20, //同时执行的任务数量，多个协程并发执行任务数量\n})\n```\n\n## Rmq Hook\n```\n\n// 可选，返回false将不会push消息\nqueue.Hook.OnPush(func(ctx context.Context, msg ...*rmq.Message) ([]*rmq.Message, error) {\n    return msg,nil\n})\n\n// 任务开始执行时调用，注意，返回error将取消任务执行\nqueue.Hook.OnRun(func(ctx context.Context, r *rmq.TaskRuntime) error {\n    fmt.Println(\"任务开始:\", runtime.NumGoroutine())\n    return nil\n})\n\n// 任务执行完成时调用\nqueue.Hook.OnComplete(func(ctx context.Context, r *rmq.TaskRuntime) error {\n    fmt.Println(\"任务结束:\", runtime.NumGoroutine())\n    fmt.Println(rmq.Json(r))\n    return nil\n})\n\n// 任务开始，创建Context时调用\nqueue.Hook.OnContext(func(ctx context.Context, r *rmq.TaskRuntime) context.Context {\n    return context.WithValue(ctx, \"x_trace_id\", r.Msg.Meta.TraceId)\n})\n```\n\n## Task Hook\n```\nfunc (t *TestTask) OnSuccess(ctx context.Context) {\n    fmt.Println(t.Name, \"Success hook\")\n}\n\nfunc (t *TestTask) OnFail(ctx context.Context) {\n    // TODO implement me\n    fmt.Println(t.Name, \"Fail hook\")\n}\n\nfunc (t *TestTask) OnComplete(ctx context.Context) {\n    // TODO implement me\n    fmt.Println(t.Name, \"Complete hook\")\n}\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flpong%2Frmq","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flpong%2Frmq","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flpong%2Frmq/lists"}