{"id":19666851,"url":"https://github.com/mavolin/worker","last_synced_at":"2025-02-27T04:17:52.314Z","repository":{"id":142544355,"uuid":"613125754","full_name":"mavolin/worker","owner":"mavolin","description":"👪 Worker Pool and Concurrency Limiter with error and result collection.","archived":false,"fork":false,"pushed_at":"2023-06-18T18:16:18.000Z","size":34,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"v1","last_synced_at":"2025-01-10T02:47:53.867Z","etag":null,"topics":[],"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/mavolin.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","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":"2023-03-12T23:51:59.000Z","updated_at":"2023-12-31T15:24:03.000Z","dependencies_parsed_at":null,"dependency_job_id":"37bf9aa3-e0d3-4842-b35a-9a88fba23326","html_url":"https://github.com/mavolin/worker","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mavolin%2Fworker","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mavolin%2Fworker/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mavolin%2Fworker/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mavolin%2Fworker/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mavolin","download_url":"https://codeload.github.com/mavolin/worker/tar.gz/refs/heads/v1","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240976222,"owners_count":19887447,"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-11T16:29:14.716Z","updated_at":"2025-02-27T04:17:52.292Z","avatar_url":"https://github.com/mavolin.png","language":"Go","readme":"\u003cdiv align=\"center\"\u003e\n\u003ch1\u003eworker\u003c/h1\u003e\n\n[![Test](https://github.com/mavolin/worker/actions/workflows/test.yml/badge.svg)](https://github.com/mavolin/worker/actions)\n[![Code Coverage](https://codecov.io/gh/mavolin/worker/branch/develop/graph/badge.svg?token=ewFEQGgMES)](https://codecov.io/gh/mavolin/worker)\n[![Go Report Card](https://goreportcard.com/badge/github.com/mavolin/worker)](https://goreportcard.com/report/github.com/mavolin/worker)\n[![License MIT](https://img.shields.io/github/license/mavolin/worker)](https://github.com/mavolin/worker/blob/develop/LICENSE)\n\u003c/div\u003e\n\n---\n\n## About\n\n`worker` is a small library that provides a concurrency limiter and a worker pool.\n\n## Main Features\n\n**`Limiter`**\n\u003e Limits the amount of times a task is executed at once.\n\u003e * ⤵ Tasks are blockingly executed in the same goroutine as the caller\n\u003e * 👮 Ensures has a task is only executed n-times at the same time\n\u003e * 🔄 Tasks are executed in the order they are added in\n\u003e * 🚚 Result collection and error handling is done by the caller\n\u003e * ⭐ Support for `context.Context`\n\n**`Pool` and `ResultPool`**\n\u003e Execute multiple tasks at the same time.\n\u003e * ⤴ Task are executed in a separate goroutine, with max. n+1 goroutines running\n\u003e * 👮 Optionally limit the amount of concurrently running workers\n\u003e * 🔄 Tasks are executed in the order they are added in\n\u003e * 🚫 Tasks can abort execution of other tasks\n\u003e * 🚚 Result collection and error handling is done by the pool\n\u003e * ⭐ Support for `context.Context`\n\u003e * ⌚ `Wait()` to await the completion of all tasks\n\n## Examples\n\nFirst impressions matter, so here are examples of a limiter and a pool:\n\n```go\nvar l = worker.NewLimiter(5)\n\n// veryExpensiveComputation does a very expensive computation and returns with\n// the result.\n// At any given time only up to 5 computations will be made simultaneously.\n// If there are more than 5 calls to veryExpensiveComputations at once, \n// veryExpensiveComputation will queue up computations until another is\n// finished.\n//\n// If ctx is cancelled before the computation starts, veryExpensiveComputation\n// will return with ctx.Err.\nfunc veryExpensiveComputation(ctx context.Context) (res int, err error) {\n    ran := l.DoContext(ctx, func() {\n        // assign something to res and err\n    })\n    if !ran {\n        return 0, ctx.Err()\n    }\n    return res, err\n}\n```\n\n```go\n// veryExpensiveComputation does a very expensive computation.\n//\n// It splits the computation into 123 smaller computations.\n// These computations are executed concurrently, 5 at a time.\n//\n// If ctx is cancelled before the computation concludes, ctx.Err() is returned.\n//\n// Otherwise, the result or an error is returned.\nfunc veryExpensiveComputation(ctx context.Context) (res int, err error) {\n    partials := make([]worker.Result[int], 0, 123)\n    p := worker.NewResultPool(ctx, 5, worker.ToSlice(\u0026partials))\n    \n    for i := 0; i \u003c 123; i++ {\n        p.Go(func(ctx context.Context) (result int, err error, cancel bool) {\n            // return the result of the computation\n        })\n    }\n    \n    if ctxErr := p.Wait(); ctxErr != nil {\n        return 0, ctxErr\n    }\n\t\n    for _, partial := range partials {\n        if partial.Err != nil {\n            return 0, err\n        }\n\n        // compute res from partial result\n    }\n\t\n    return res, nil\n}\n```\n\n## License\n\nBuilt with ❤ by [Maximilian von Lindern](https://github.com/mavolin).\nAvailable under the [MIT License](./LICENSE).\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmavolin%2Fworker","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmavolin%2Fworker","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmavolin%2Fworker/lists"}