{"id":29182495,"url":"https://github.com/asmsh/sema","last_synced_at":"2025-08-02T09:35:38.226Z","repository":{"id":301973169,"uuid":"1010808661","full_name":"asmsh/sema","owner":"asmsh","description":"A feature-rich concurrency manager for Go, providing an alternative to sync.WaitGroup, channel-based limiter and semaphore.Weighted, with support for select-based waiting and internal counter inspection.","archived":false,"fork":false,"pushed_at":"2025-06-29T22:28:38.000Z","size":25,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-06-29T22:32:28.006Z","etag":null,"topics":["concurrency","go","goroutine","limiter"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/asmsh.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,"zenodo":null}},"created_at":"2025-06-29T20:58:07.000Z","updated_at":"2025-06-29T22:24:01.000Z","dependencies_parsed_at":"2025-06-29T22:42:41.310Z","dependency_job_id":null,"html_url":"https://github.com/asmsh/sema","commit_stats":null,"previous_names":["asmsh/sema"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/asmsh/sema","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/asmsh%2Fsema","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/asmsh%2Fsema/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/asmsh%2Fsema/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/asmsh%2Fsema/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/asmsh","download_url":"https://codeload.github.com/asmsh/sema/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/asmsh%2Fsema/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":268362998,"owners_count":24238544,"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","status":"online","status_checked_at":"2025-08-02T02:00:12.353Z","response_time":74,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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":["concurrency","go","goroutine","limiter"],"created_at":"2025-07-01T20:06:36.080Z","updated_at":"2025-08-02T09:35:38.165Z","avatar_url":"https://github.com/asmsh.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Sema: A feature-rich concurrency manager for Go.\n\n[![PkgGoDev](https://pkg.go.dev/badge/github.com/asmsh/sema)](https://pkg.go.dev/github.com/asmsh/sema)\n[![Go Report Card](https://goreportcard.com/badge/github.com/asmsh/sema)](https://goreportcard.com/report/github.com/asmsh/sema)\n[![Tests](https://github.com/asmsh/sema/workflows/Tests/badge.svg)](https://github.com/asmsh/sema/actions)\n[![Go Coverage](https://github.com/asmsh/sema/wiki/coverage.svg)](https://raw.githack.com/wiki/asmsh/sema/coverage.html)\n\nIt bridges the gap between using channels, [sync.WaitGroup](https://pkg.go.dev/sync#WaitGroup) and [semaphore.Weighted](https://pkg.go.dev/golang.org/x/sync/semaphore#Weighted) for\nmanaging concurrency.\n\n### Features\n\n* Fast, low-overhead implementation with performance comparable to `sync.WaitGroup` and `semaphore.Weighted`.\n* Enables `select`-based waiting, in addition to standard `sync.WaitGroup` wait behavior.\n* Exposes the internal counters in a concurrent-safe way.\n* Clear API that's compatible with `sync.WaitGroup` and `semaphore.Weighted` with minimal changes.\n* Usable zero value that's comparable to a `sync.WaitGroup`.\n\n### Notes\n\n* It wakes up blocked calls in random order, unlike `semaphore.Weighted` which preserves the order of calls.\n* It's more suitable as a replacement for `semaphore.Weighted` when all the weights are of equal size.\n* It relies on a single channel for blocking and wake-ups, so the Go runtime guarantees no starvation.\n\n### Examples\n\n#### Using it as a `sync.WaitGroup`:\n\n```go\nfunc example() {\n\tvar sg sema.Group\n\n\tfor i := 0; i \u003c 10; i++ {\n\t\tsg.Reserve()\n\t\tgo func(i int) {\n\t\t\tdefer sg.Free()\n\n\t\t\t// Do some work...\n\t\t}(i)\n\t}\n\n\tsg.Wait()\n}\n```\n\n#### Using `select`-based waiting:\n\n```go\nfunc example() {\n\tvar sg sema.Group\n\n\tctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)\n\tdefer cancel()\n\n\tfor i := 0; i \u003c 10; i++ {\n\t\tsg.Reserve()\n\t\tgo func(i int) {\n\t\t\tdefer sg.Free()\n\n\t\t\t// Do some work using the ctx...\n\t\t\tctx = ctx\n\t\t}(i)\n\t}\n\n\twaitChan := sg.WaitChan()\n\n\tselect {\n\tcase \u003c-waitChan:\n\t\t// all the work is done.\n\tcase \u003c-ctx.Done():\n\t\t// the work times out.\n\t}\n}\n```\n\n#### Using it to limit concurrency (instead of a `semaphore.Weighted` or a channel):\n\n```go\nfunc example() {\n\tvar sg sema.Group\n\tsg.SetSize(10)\n\n\tctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)\n\tdefer cancel()\n\n\tfor i := 0; i \u003c 10; i++ {\n\t\tsg.ReserveN(ctx.Done(), 5)\n\t\tgo func(i int) {\n\t\t\tdefer sg.FreeN(5)\n\n\t\t\t// Do some work...\n\t\t\tlog.Println(\"some work...\")\n\t\t}(i)\n\t}\n\n\tsg.Wait()\n}\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fasmsh%2Fsema","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fasmsh%2Fsema","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fasmsh%2Fsema/lists"}