{"id":21667434,"url":"https://github.com/xsymphony/notify","last_synced_at":"2025-06-15T11:37:37.717Z","repository":{"id":223803778,"uuid":"270906832","full_name":"xsymphony/notify","owner":"xsymphony","description":null,"archived":false,"fork":false,"pushed_at":"2020-06-09T10:07:34.000Z","size":4,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-04-18T11:23:15.202Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/xsymphony.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,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2020-06-09T05:00:32.000Z","updated_at":"2020-06-09T10:18:20.000Z","dependencies_parsed_at":"2024-02-22T06:48:52.292Z","dependency_job_id":null,"html_url":"https://github.com/xsymphony/notify","commit_stats":null,"previous_names":["xsymphony/notify"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xsymphony%2Fnotify","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xsymphony%2Fnotify/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xsymphony%2Fnotify/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xsymphony%2Fnotify/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xsymphony","download_url":"https://codeload.github.com/xsymphony/notify/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244566936,"owners_count":20473451,"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":[],"created_at":"2024-11-25T11:38:20.842Z","updated_at":"2025-03-20T07:13:30.040Z","avatar_url":"https://github.com/xsymphony.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# notify\nnotify包用于单应用内的消息通信，提供`发布/订阅`、`取消订阅`、`超时`、`超时处理`、`扇出`、`工作池`等功能。\n\n## 基础功能\n```go\npackage main\n\nimport (\n    \"context\"\n    \"fmt\"\n    \"time\"\n\n    \"github.com/xsymphony/notify\"\n)\n\nfunc main() {\n    // consume from the \"simple\" topic\n    notify.Watch(context.TODO(), \"simple\", func(msg interface{}) {\n        fmt.Println(\"[watch]\", msg)\n    })\n    \n    // producer msg to the \"simple\" topic\n    for i := 0; i \u003c 10; i++ {\n        notify.Send(context.TODO(), \"simple\", i)\n    }\n    time.Sleep(1*time.Second)\n}\n\n// stdout:\n// [watch]1\n// [watch]2\n// [watch]3\n//   ...\n// [watch]9\n```\n\n## 消费超时设置\n```go\npackage main\n\nimport (\n    \"context\"\n    \"fmt\"\n    \"time\"\n\n    \"github.com/xsymphony/notify\"\n)\n\nfunc main() {\n    var fn = func(msg interface{}) {\n        time.Sleep(1*time.Second)\n        fmt.Println(\"[watch]\", msg)\n    }\n    var timeoutFn = func(msg interface{}) {\n        fmt.Println(\"[watch]msg timeout \", msg)\n    }\n\n    notify.Watch(context.TODO(), \"timeout\", fn, notify.Timeout(500*time.Millisecond), notify.TimeoutFunc(timeoutFn))\n\n    notify.Send(context.TODO(), \"timeout\", 1)\n    \n    time.Sleep(1*time.Second)\n}\n\n// stdout:\n// [watch]msg timeout 1\n```\n\n## 扇出\n```go\npackage main\n\nimport (\n    \"context\"\n    \"fmt\"\n    \"time\"\n\n    \"github.com/xsymphony/notify\"\n)\n\nfunc main() {\n    notify.Watch(context.TODO(), \"fanout\", func(msg interface{}) {\n        fmt.Println(\"[watch][A]\", msg)\n    })\n    notify.Watch(context.TODO(), \"fanout\", func(msg interface{}) {\n        fmt.Println(\"[watch][B]\", msg)\n    })\n    notify.Watch(context.TODO(), \"fanout\", func(msg interface{}) {\n        fmt.Println(\"[watch][C]\", msg)\n    })\n\n    notify.Send(context.TODO(), \"fanout\", 1)\n\n    time.Sleep(1*time.Second)\n}\n\n// stdout:\n// [watch][A]1  \n// [watch][C]1 \n// [watch][B]1 \n```\n\n## 工作池\n```go\npackage main\n\nimport (\n    \"bytes\"\n    \"context\"\n    \"fmt\"\n    \"runtime\"\n    \"strconv\"\n\n    \"github.com/xsymphony/notify\"\n)\n\nfunc getGID() uint64 {\n    b := make([]byte, 64)\n    b = b[:runtime.Stack(b, false)]\n    b = bytes.TrimPrefix(b, []byte(\"goroutine \"))\n    b = b[:bytes.IndexByte(b, ' ')]\n    n, _ := strconv.ParseUint(string(b), 10, 64)\n    return n\n}\n\nfunc main() {\n    notify.Watch(context.TODO(), \"simple\", func(msg interface{}) {\n        fmt.Println(\"[watch]\", \"id:\", getGID(), msg)\n    }, notify.WorkerSize(3))\n\n    for i := 0; i \u003c 10; i++ {\n        notify.Send(context.TODO(), \"simple\", i)\n    }\n}\n\n// stdout:\n// [watch] id: 49 0\n// [watch] id: 50 1\n// [watch] id: 49 3\n// [watch] id: 51 2\n// [watch] id: 50 4\n// [watch] id: 51 6\n// [watch] id: 49 5\n// [watch] id: 50 7\n// [watch] id: 49 9\n// [watch] id: 51 8\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxsymphony%2Fnotify","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxsymphony%2Fnotify","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxsymphony%2Fnotify/lists"}