{"id":27880859,"url":"https://github.com/atomicgo/pool","last_synced_at":"2025-10-28T22:06:23.097Z","repository":{"id":182122825,"uuid":"667990894","full_name":"atomicgo/pool","owner":"atomicgo","description":"🤿 Minimalistic worker pool for Go","archived":false,"fork":false,"pushed_at":"2024-08-08T17:04:30.000Z","size":46,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-08-08T19:59:55.871Z","etag":null,"topics":["atomicgo","concurrency","go","golang","golang-library","goroutine","pool","worker"],"latest_commit_sha":null,"homepage":"https://atomicgo.dev","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/atomicgo.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},"funding":{"github":["MarvinJWendt"]}},"created_at":"2023-07-18T19:04:39.000Z","updated_at":"2024-08-08T17:04:33.000Z","dependencies_parsed_at":"2023-12-10T21:25:10.189Z","dependency_job_id":"c1840cc1-db4d-43a1-a281-035b3dc42a31","html_url":"https://github.com/atomicgo/pool","commit_stats":null,"previous_names":["atomicgo/pool"],"tags_count":1,"template":false,"template_full_name":"atomicgo/template","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/atomicgo%2Fpool","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/atomicgo%2Fpool/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/atomicgo%2Fpool/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/atomicgo%2Fpool/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/atomicgo","download_url":"https://codeload.github.com/atomicgo/pool/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252442480,"owners_count":21748448,"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":["atomicgo","concurrency","go","golang","golang-library","goroutine","pool","worker"],"created_at":"2025-05-05T04:38:51.417Z","updated_at":"2025-10-28T22:06:23.045Z","avatar_url":"https://github.com/atomicgo.png","language":"Go","funding_links":["https://github.com/sponsors/MarvinJWendt"],"categories":[],"sub_categories":[],"readme":"\u003c!--\n\n\n\n┌───────────────────────────────────────────────────────────────────┐\n│                                                                   │\n│                          IMPORTANT NOTE                           │\n│                                                                   │\n│               This file is automatically generated                │\n│           All manual modifications will be overwritten            │\n│                                                                   │\n└───────────────────────────────────────────────────────────────────┘\n\n\n\n--\u003e\n\n\u003ch1 align=\"center\"\u003eAtomicGo | pool\u003c/h1\u003e\n\n\u003cp align=\"center\"\u003e\n\u003cimg src=\"https://img.shields.io/endpoint?url=https%3A%2F%2Fatomicgo.dev%2Fapi%2Fshields%2Fpool\u0026style=flat-square\" alt=\"Downloads\"\u003e\n\n\u003ca href=\"https://github.com/atomicgo/pool/releases\"\u003e\n\u003cimg src=\"https://img.shields.io/github/v/release/atomicgo/pool?style=flat-square\" alt=\"Latest Release\"\u003e\n\u003c/a\u003e\n\n\u003ca href=\"https://codecov.io/gh/atomicgo/pool\" target=\"_blank\"\u003e\n\u003cimg src=\"https://img.shields.io/github/actions/workflow/status/atomicgo/pool/go.yml?style=flat-square\" alt=\"Tests\"\u003e\n\u003c/a\u003e\n\n\u003ca href=\"https://codecov.io/gh/atomicgo/pool\" target=\"_blank\"\u003e\n\u003cimg src=\"https://img.shields.io/codecov/c/gh/atomicgo/pool?color=magenta\u0026logo=codecov\u0026style=flat-square\" alt=\"Coverage\"\u003e\n\u003c/a\u003e\n\n\u003ca href=\"https://codecov.io/gh/atomicgo/pool\"\u003e\n\u003c!-- unittestcount:start --\u003e\u003cimg src=\"https://img.shields.io/badge/Unit_Tests-7-magenta?style=flat-square\" alt=\"Unit test count\"\u003e\u003c!-- unittestcount:end --\u003e\n\u003c/a\u003e\n\n\u003ca href=\"https://opensource.org/licenses/MIT\" target=\"_blank\"\u003e\n\u003cimg src=\"https://img.shields.io/badge/License-MIT-yellow.svg?style=flat-square\" alt=\"License: MIT\"\u003e\n\u003c/a\u003e\n  \n\u003ca href=\"https://goreportcard.com/report/github.com/atomicgo/pool\" target=\"_blank\"\u003e\n\u003cimg src=\"https://goreportcard.com/badge/github.com/atomicgo/pool?style=flat-square\" alt=\"Go report\"\u003e\n\u003c/a\u003e   \n\n\u003c/p\u003e\n\n---\n\n\u003cp align=\"center\"\u003e\n\u003cstrong\u003e\u003ca href=\"https://pkg.go.dev/atomicgo.dev/pool#section-documentation\" target=\"_blank\"\u003eDocumentation\u003c/a\u003e\u003c/strong\u003e\n|\n\u003cstrong\u003e\u003ca href=\"https://github.com/atomicgo/atomicgo/blob/main/CONTRIBUTING.md\" target=\"_blank\"\u003eContributing\u003c/a\u003e\u003c/strong\u003e\n|\n\u003cstrong\u003e\u003ca href=\"https://github.com/atomicgo/atomicgo/blob/main/CODE_OF_CONDUCT.md\" target=\"_blank\"\u003eCode of Conduct\u003c/a\u003e\u003c/strong\u003e\n\u003c/p\u003e\n\n---\n\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"https://raw.githubusercontent.com/atomicgo/atomicgo/main/assets/header.png\" alt=\"AtomicGo\"\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n\u003ctable\u003e\n\u003ctbody\u003e\n\u003c/tbody\u003e\n\u003c/table\u003e\n\u003c/p\u003e\n\u003ch3  align=\"center\"\u003e\u003cpre\u003ego get atomicgo.dev/pool\u003c/pre\u003e\u003c/h3\u003e\n\u003cp align=\"center\"\u003e\n\u003ctable\u003e\n\u003ctbody\u003e\n\u003c/tbody\u003e\n\u003c/table\u003e\n\u003c/p\u003e\n\n\u003c!-- gomarkdoc:embed:start --\u003e\n\n\u003c!-- Code generated by gomarkdoc. DO NOT EDIT --\u003e\n\n# pool\n\n```go\nimport \"atomicgo.dev/pool\"\n```\n\nPackage pool provides a generic and concurrent worker pool implementation. It allows you to enqueue tasks and process them using a fixed number of workers.\n\n\n\n\n\n```go\npackage main\n\nimport (\n\t\"atomicgo.dev/pool\"\n\t\"context\"\n\t\"log\"\n\t\"time\"\n)\n\nfunc main() {\n\t// Create a new pool with 3 workers\n\tp := pool.New[int](pool.Config{\n\t\tMaxWorkers: 3,\n\t})\n\n\t// Set the task handler to process integers, simulating a 1-second task\n\tp.SetHandler(func(ctx context.Context, i int) error {\n\t\tlog.Printf(\"Processing %d\", i)\n\t\ttime.Sleep(time.Second)\n\t\treturn nil\n\t})\n\n\t// Start the pool\n\tp.Start()\n\n\t// Add 10 tasks to the pool\n\tfor i := 0; i \u003c 10; i++ {\n\t\tp.Add(i)\n\t}\n\n\t// Close the pool and wait for all tasks to complete\n\tp.Close()\n}\n```\n\n\n\n\n\n\n\n```go\npackage main\n\nimport (\n\t\"atomicgo.dev/pool\"\n\t\"context\"\n\t\"fmt\"\n\t\"log\"\n)\n\nfunc main() {\n\t// Create a new pool\n\tp := pool.New[int](pool.Config{\n\t\tMaxWorkers: 2,\n\t})\n\n\t// Set the task handler, which returns an error for even numbers\n\tp.SetHandler(func(ctx context.Context, i int) error {\n\t\tif i%2 == 0 {\n\t\t\treturn fmt.Errorf(\"error processing %d\", i)\n\t\t}\n\n\t\tlog.Printf(\"Successfully processed %d\", i)\n\n\t\treturn nil\n\t})\n\n\t// Set a custom error handler that logs errors\n\tp.SetErrorHandler(func(err error, pool *pool.Pool[int]) {\n\t\tlog.Printf(\"Encountered an error: %v\", err)\n\t\t// Optional: you can call pool.Kill() here if you want to stop the whole pool on the first error\n\t})\n\n\t// Start the pool\n\tp.Start()\n\n\t// Add 5 tasks to the pool\n\tfor i := 0; i \u003c 5; i++ {\n\t\tp.Add(i)\n\t}\n\n\t// Close the pool and wait for all tasks to complete\n\tp.Close()\n}\n```\n\n\n\n\n\n\n\n```go\npackage main\n\nimport (\n\t\"atomicgo.dev/pool\"\n\t\"context\"\n\t\"log\"\n\t\"time\"\n)\n\nfunc main() {\n\t// Create a new pool with a short timeout of 2 seconds\n\tp := pool.New[int](pool.Config{\n\t\tMaxWorkers: 3,\n\t\tTimeout:    time.Second * 2,\n\t})\n\n\t// Set the task handler with a task that may exceed the timeout\n\tp.SetHandler(func(ctx context.Context, i int) error {\n\t\tlog.Printf(\"Processing %d\", i)\n\t\ttime.Sleep(time.Second * 3) // This sleep is longer than the timeout\n\t\treturn nil\n\t})\n\n\t// Start the pool\n\tp.Start()\n\n\t// Add tasks to the pool\n\tfor i := 0; i \u003c 5; i++ {\n\t\tp.Add(i)\n\t}\n\n\t// Close the pool and wait for all tasks to complete\n\tp.Close()\n}\n```\n\n\n\n## Index\n\n- [type Config](\u003c#Config\u003e)\n- [type ErrorHandler](\u003c#ErrorHandler\u003e)\n- [type Pool](\u003c#Pool\u003e)\n  - [func New\\[T any\\]\\(config Config\\) \\*Pool\\[T\\]](\u003c#New\u003e)\n  - [func \\(p \\*Pool\\[T\\]\\) Add\\(item T\\)](\u003c#Pool[T].Add\u003e)\n  - [func \\(p \\*Pool\\[T\\]\\) Close\\(\\)](\u003c#Pool[T].Close\u003e)\n  - [func \\(p \\*Pool\\[T\\]\\) Kill\\(\\)](\u003c#Pool[T].Kill\u003e)\n  - [func \\(p \\*Pool\\[T\\]\\) SetErrorHandler\\(handler ErrorHandler\\[T\\]\\) \\*Pool\\[T\\]](\u003c#Pool[T].SetErrorHandler\u003e)\n  - [func \\(p \\*Pool\\[T\\]\\) SetHandler\\(handler func\\(context.Context, T\\) error\\) \\*Pool\\[T\\]](\u003c#Pool[T].SetHandler\u003e)\n  - [func \\(p \\*Pool\\[T\\]\\) Start\\(\\)](\u003c#Pool[T].Start\u003e)\n\n\n\u003ca name=\"Config\"\u003e\u003c/a\u003e\n## type [Config](\u003chttps://github.com/atomicgo/pool/blob/main/pool.go#L17-L20\u003e)\n\nConfig struct defines the configuration parameters for the Pool. \\- MaxWorkers: The maximum number of concurrent workers in the pool. \\- Timeout: The maximum duration for processing a single task. If a task takes longer, it will be terminated.\n\nIf the Timeout is set to 0, tasks will not be terminated. This is the default behavior. If MaxWorkers is set to 0, it will be set to the number of logical CPUs on the machine.\n\n```go\ntype Config struct {\n    MaxWorkers int\n    Timeout    time.Duration\n}\n```\n\n\u003ca name=\"ErrorHandler\"\u003e\u003c/a\u003e\n## type [ErrorHandler](\u003chttps://github.com/atomicgo/pool/blob/main/pool.go#L24\u003e)\n\nErrorHandler is a function type for handling errors. It provides a way for users to define custom error handling logic. The function receives an error and a pointer to the pool, allowing users to log errors, modify the pool, or perform other actions.\n\n```go\ntype ErrorHandler[T any] func(error, *Pool[T])\n```\n\n\u003ca name=\"Pool\"\u003e\u003c/a\u003e\n## type [Pool](\u003chttps://github.com/atomicgo/pool/blob/main/pool.go#L28-L36\u003e)\n\nPool struct represents a pool of workers processing tasks of type T. It encapsulates the task handling logic, error handling, and synchronization mechanisms.\n\n```go\ntype Pool[T any] struct {\n    // contains filtered or unexported fields\n}\n```\n\n\u003ca name=\"New\"\u003e\u003c/a\u003e\n### func [New](\u003chttps://github.com/atomicgo/pool/blob/main/pool.go#L41\u003e)\n\n```go\nfunc New[T any](config Config) *Pool[T]\n```\n\nNew creates and returns a new pool with the specified configuration. It initializes the internal structures but does not start the worker goroutines. \\- config: Configuration settings for the pool, including max workers and task timeout.\n\n\u003ca name=\"Pool[T].Add\"\u003e\u003c/a\u003e\n### func \\(\\*Pool\\[T\\]\\) [Add](\u003chttps://github.com/atomicgo/pool/blob/main/pool.go#L139\u003e)\n\n```go\nfunc (p *Pool[T]) Add(item T)\n```\n\nAdd enqueues a task into the pool. If the pool's worker goroutines are running, the task will be picked up for processing. If the pool is not running or has been closed, the behavior of Add is undefined and may result in a deadlock or panic. \\- item: The task to be added to the pool for processing.\n\n\u003ca name=\"Pool[T].Close\"\u003e\u003c/a\u003e\n### func \\(\\*Pool\\[T\\]\\) [Close](\u003chttps://github.com/atomicgo/pool/blob/main/pool.go#L145\u003e)\n\n```go\nfunc (p *Pool[T]) Close()\n```\n\nClose gracefully shuts down the pool. It stops accepting new tasks and waits for all ongoing tasks to complete. This method should be called to ensure a clean shutdown of the pool.\n\n\u003ca name=\"Pool[T].Kill\"\u003e\u003c/a\u003e\n### func \\(\\*Pool\\[T\\]\\) [Kill](\u003chttps://github.com/atomicgo/pool/blob/main/pool.go#L152\u003e)\n\n```go\nfunc (p *Pool[T]) Kill()\n```\n\nKill immediately stops all workers in the pool. It cancels the context, causing all worker goroutines to exit. Any ongoing tasks may be left unfinished. This method is useful for emergency shutdown scenarios.\n\n\u003ca name=\"Pool[T].SetErrorHandler\"\u003e\u003c/a\u003e\n### func \\(\\*Pool\\[T\\]\\) [SetErrorHandler](\u003chttps://github.com/atomicgo/pool/blob/main/pool.go#L70\u003e)\n\n```go\nfunc (p *Pool[T]) SetErrorHandler(handler ErrorHandler[T]) *Pool[T]\n```\n\nSetErrorHandler sets a custom error handling function for the pool. It is optional. The error handler allows custom logic to be executed when a task processing results in an error. \\- handler: The function to be called when a task encounters an error.\n\n\u003ca name=\"Pool[T].SetHandler\"\u003e\u003c/a\u003e\n### func \\(\\*Pool\\[T\\]\\) [SetHandler](\u003chttps://github.com/atomicgo/pool/blob/main/pool.go#L62\u003e)\n\n```go\nfunc (p *Pool[T]) SetHandler(handler func(context.Context, T) error) *Pool[T]\n```\n\nSetHandler sets the task handling function for the pool. It must be called before starting the pool. The handler function takes a context \\(for timeout control\\) and a task of type T, and returns an error. \\- handler: The function that will be called to process each task.\n\n\u003ca name=\"Pool[T].Start\"\u003e\u003c/a\u003e\n### func \\(\\*Pool\\[T\\]\\) [Start](\u003chttps://github.com/atomicgo/pool/blob/main/pool.go#L77\u003e)\n\n```go\nfunc (p *Pool[T]) Start()\n```\n\nStart initiates the worker goroutines. It should be called after setting up the task handler and optionally the error handler. This method spins up MaxWorkers number of goroutines, each listening for tasks to process.\n\nGenerated by [gomarkdoc](\u003chttps://github.com/princjef/gomarkdoc\u003e)\n\n\n\u003c!-- gomarkdoc:embed:end --\u003e\n\n---\n\n\u003e [AtomicGo.dev](https://atomicgo.dev) \u0026nbsp;\u0026middot;\u0026nbsp;\n\u003e with ❤️ by [@MarvinJWendt](https://github.com/MarvinJWendt) |\n\u003e [MarvinJWendt.com](https://marvinjwendt.com)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fatomicgo%2Fpool","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fatomicgo%2Fpool","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fatomicgo%2Fpool/lists"}