{"id":13413074,"url":"https://github.com/xxjwxc/gowp","last_synced_at":"2025-05-16T13:04:21.448Z","repository":{"id":35486817,"uuid":"208432326","full_name":"xxjwxc/gowp","owner":"xxjwxc","description":"golang worker pool , Concurrency limiting goroutine pool","archived":false,"fork":false,"pushed_at":"2024-09-29T03:30:19.000Z","size":103,"stargazers_count":522,"open_issues_count":3,"forks_count":69,"subscribers_count":18,"default_branch":"master","last_synced_at":"2025-03-30T22:08:20.372Z","etag":null,"topics":["concurrency","golang","pool","workerpool","workpool"],"latest_commit_sha":null,"homepage":"https://xxjwxc.github.io/post/gowp/","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/xxjwxc.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,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-09-14T11:43:50.000Z","updated_at":"2025-03-28T16:47:26.000Z","dependencies_parsed_at":"2024-10-30T16:04:15.203Z","dependency_job_id":"9433d47f-8c30-42a5-90c0-209b92e73560","html_url":"https://github.com/xxjwxc/gowp","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xxjwxc%2Fgowp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xxjwxc%2Fgowp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xxjwxc%2Fgowp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xxjwxc%2Fgowp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xxjwxc","download_url":"https://codeload.github.com/xxjwxc/gowp/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247563934,"owners_count":20958971,"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":["concurrency","golang","pool","workerpool","workpool"],"created_at":"2024-07-30T20:01:33.143Z","updated_at":"2025-04-06T23:15:30.733Z","avatar_url":"https://github.com/xxjwxc.png","language":"Go","funding_links":[],"categories":["Goroutines","Go","Go (531)","Goroutines `goroutines的管理和使用`","Relational Databases"],"sub_categories":["Search and Analytic Databases","检索及分析资料库","SQL 查询语句构建库","Advanced Console UIs"],"readme":"[![Build Status](https://travis-ci.org/xxjwxc/gowp.svg?branch=master)](https://travis-ci.org/xxjwxc/gowp)\n[![Go Report Card](https://goreportcard.com/badge/github.com/xxjwxc/gowp)](https://goreportcard.com/report/github.com/xxjwxc/gowp)\n[![codecov](https://codecov.io/gh/xxjwxc/gowp/branch/master/graph/badge.svg)](https://codecov.io/gh/xxjwxc/gowp)\n[![GoDoc](https://godoc.org/github.com/xxjwxc/gowp?status.svg)](https://godoc.org/github.com/xxjwxc/gowp)\n[![Mentioned in Awesome Go](https://awesome.re/mentioned-badge.svg)](https://github.com/avelino/awesome-go)\n\u003ca href=\"https://hellogithub.com/repository/0f15a9cd05d14fec9e9ceafa2f5ae984\" target=\"_blank\"\u003e\u003cimg src=\"https://abroad.hellogithub.com/v1/widgets/recommend.svg?rid=0f15a9cd05d14fec9e9ceafa2f5ae984\u0026claim_uid=6IkmGoxiuN4LWFn\u0026theme=small\" alt=\"Featured｜HelloGitHub\" /\u003e\u003c/a\u003e\n\n## golang worker pool\n\n### [中文说明](README_cn.md)\n\n- Concurrency limiting goroutine pool. \n- Limits the concurrency of task execution, not the number of tasks queued. \n- Never blocks submitting tasks, no matter how many tasks are queued.\n- Support timeout\n- Support through security queues [queue](https://github.com/xxjwxc/public/tree/master/myqueue)\n\n\n## Installation\n\nThe simplest way to install the library is to run:\n\n```\n$ go get github.com/xxjwxc/gowp\n```\n\n\n### Support the maximum number of tasks, put them in the workpool and wait for them to be completed\n\n## Example\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"time\"\n\n\t\"github.com/xxjwxc/gowp/workpool\"\n)\n\nfunc main() {\n\twp := workpool.New(10)     // Set the maximum number of threads\n\tfor i := 0; i \u003c 20; i++ { // Open 20 requests \n\t\tii := i\n\t\twp.Do(func() error {\n\t\t\tfor j := 0; j \u003c 10; j++ { // 0-10 values per print\n\t\t\t\tfmt.Println(fmt.Sprintf(\"%v-\u003e\\t%v\", ii, j))\n\t\t\t\ttime.Sleep(1 * time.Second)\n\t\t\t}\n\t\t\t//time.Sleep(1 * time.Second)\n\t\t\treturn nil\n\t\t})\n\t}\n\n\twp.Wait()\n\tfmt.Println(\"down\")\n}\n\n```\n\n### Support for error return\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"time\"\n\n\t\"github.com/xxjwxc/gowp/workpool\"\n)\n\nfunc main() {\n\twp := workpool.New(10)             // Set the maximum number of threads\n\tfor i := 0; i \u003c 20; i++ { \n\t\tii := i\n\t\twp.Do(func() error {\n\t\t\tfor j := 0; j \u003c 10; j++ { // 0-10 values per print\n\t\t\t\tfmt.Println(fmt.Sprintf(\"%v-\u003e\\t%v\", ii, j))\n\t\t\t\tif ii == 1 {\n\t\t\t\t\treturn errors.Cause(errors.New(\"my test err\")) // have err return\n\t\t\t\t}\n\t\t\t\ttime.Sleep(1 * time.Second)\n\t\t\t}\n\n\t\t\treturn nil\n\t\t})\n\t}\n\n\terr := wp.Wait()\n\tif err != nil {\n\t\tfmt.Println(err)\n\t}\n\tfmt.Println(\"down\")\n\t}\n```\n\n### Supporting judgement of completion (non-blocking)\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"time\"\n\n\t\"github.com/xxjwxc/gowp/workpool\"\n)\n\nfunc main() {\n\twp := workpool.New(5)              // Set the maximum number of threads\n\tfor i := 0; i \u003c 10; i++ { \n\t\t//\tii := i\n\t\twp.Do(func() error {\n\t\t\tfor j := 0; j \u003c 5; j++ { \n\t\t\t\t//fmt.Println(fmt.Sprintf(\"%v-\u003e\\t%v\", ii, j))\n\t\t\t\ttime.Sleep(1 * time.Second)\n\t\t\t}\n\t\t\treturn nil\n\t\t})\n\n\t\tfmt.Println(wp.IsDone())\n\t}\n\twp.Wait()\n\tfmt.Println(wp.IsDone())\n\tfmt.Println(\"down\")\n}\n```\n\n### Support synchronous waiting for results\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"time\"\n\n\t\"github.com/xxjwxc/gowp/workpool\"\n)\n\nfunc main() {\n\twp := workpool.New(5) // Set the maximum number of threads\n\tfor i := 0; i \u003c 10; i++ { \n\t\tii := i\n\t\twp.DoWait(func() error {\n\t\t\tfor j := 0; j \u003c 5; j++ { \n\t\t\t\tfmt.Println(fmt.Sprintf(\"%v-\u003e\\t%v\", ii, j))\n\t\t\t\t// if ii == 1 {\n\t\t\t\t// \treturn errors.New(\"my test err\")\n\t\t\t\t// }\n\t\t\t\ttime.Sleep(1 * time.Second)\n\t\t\t}\n\n\t\t\treturn nil\n\t\t\t//time.Sleep(1 * time.Second)\n\t\t\t//return errors.New(\"my test err\")\n\t\t})\n\t}\n\n\terr := wp.Wait()\n\tif err != nil {\n\t\tfmt.Println(err)\n\t}\n\tfmt.Println(\"down\")\n}\n```\n\n### Support timeout exit\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"time\"\n\t\"time\"\n\t\"github.com/xxjwxc/gowp/workpool\"\n)\n\nfunc main() {\n\twp := workpool.New(5)              // Set the maximum number of threads\n\t\twp.SetTimeout(time.Millisecond) // set max timeout\n\tfor i := 0; i \u003c 10; i++ { \n\t\tii := i\n\t\twp.DoWait(func() error {\n\t\t\tfor j := 0; j \u003c 5; j++ {\n\t\t\t\tfmt.Println(fmt.Sprintf(\"%v-\u003e\\t%v\", ii, j))\n\t\t\t\ttime.Sleep(1 * time.Second)\n\t\t\t}\n\n\t\t\treturn nil\n\t\t})\n\t}\n\n\terr := wp.Wait()\n\tif err != nil {\n\t\tfmt.Println(err)\n\t}\n\tfmt.Println(\"down\")\n}\n```\n\n\n## limiter(cache)\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"sync\"\n\t\"time\"\n\n\t\"github.com/xxjwxc/gowp/limiter\"\n)\n\nfunc main() {\n\tlimiter := limiter.NewLimiter(limiter.WithLimit(10), limiter.WithNamespace(\"test\"), limiter.WithTsTimeout(true) /*, limiter.WithRedis(res)*/)\n\tvar wg sync.WaitGroup\n\tfor i := 0; i \u003c 20; i++ {\n\t\twg.Add(1)\n\t\tgo func() {\n\t\t\tdefer wg.Done()\n\t\t\ttoken, _ := limiter.Acquire(10) // get\n\t\t\tfmt.Println(token)\n\n\t\t\ttime.Sleep(1 * time.Second)\n\t\t\tlimiter.Release(token) \n\t\t}()\n\t}\n\twg.Wait()\n\tfmt.Println(\"down\")\n}\n```\n## limiter(redis)\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"sync\"\n\t\"time\"\n\n\t\"github.com/xxjwxc/gowp/limiter\"\n\t\"github.com/xxjwxc/public/myredis\"\n)\n\nfunc main() {\n\tconf := myredis.InitRedis(myredis.WithAddr(\"127.0.0.1:6379\"), myredis.WithPwd(\"123456\"), myredis.WithGroupName(\"test\"))\n\tres, err := myredis.NewRedis(conf)\n\tif err != nil {\n\t\tfmt.Println(err)\n\t\treturn\n\t}\n\n\tlimiter := limiter.NewLimiter(limiter.WithRedis(res), limiter.WithLimit(10), limiter.WithNamespace(\"test\") /*, limiter.WithRedis(res)*/)\n\tvar wg sync.WaitGroup\n\tfor i := 0; i \u003c 20; i++ {\n\t\twg.Add(1)\n\t\tgo func() {\n\t\t\tdefer wg.Done()\n\t\t\ttoken, _ := limiter.Acquire(10) \n\t\t\tfmt.Println(token)\n\n\t\t\ttime.Sleep(1 * time.Second)\n\t\t\tlimiter.Release(token) \n\t\t}()\n\t}\n\twg.Wait()\n\tfmt.Println(\"down\")\n}\n\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxxjwxc%2Fgowp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxxjwxc%2Fgowp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxxjwxc%2Fgowp/lists"}