{"id":19738587,"url":"https://github.com/gustavokatel/asyncutils","last_synced_at":"2026-06-09T04:31:24.464Z","repository":{"id":36143242,"uuid":"190388466","full_name":"GustavoKatel/asyncutils","owner":"GustavoKatel","description":"Synchornization and asynchronous operations utilities in golang","archived":false,"fork":false,"pushed_at":"2023-05-19T08:57:24.000Z","size":67,"stargazers_count":3,"open_issues_count":1,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-02-24T00:43:58.505Z","etag":null,"topics":["async","event","executor","go","golang","job","queue","scheduler","sync","worker"],"latest_commit_sha":null,"homepage":null,"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/GustavoKatel.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-06-05T12:15:25.000Z","updated_at":"2022-07-04T11:27:06.000Z","dependencies_parsed_at":"2024-06-20T16:27:55.394Z","dependency_job_id":"23e5a826-d38e-4c4f-b802-d3befb9a7767","html_url":"https://github.com/GustavoKatel/asyncutils","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GustavoKatel%2Fasyncutils","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GustavoKatel%2Fasyncutils/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GustavoKatel%2Fasyncutils/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GustavoKatel%2Fasyncutils/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/GustavoKatel","download_url":"https://codeload.github.com/GustavoKatel/asyncutils/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241068009,"owners_count":19903934,"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":["async","event","executor","go","golang","job","queue","scheduler","sync","worker"],"created_at":"2024-11-12T01:14:36.383Z","updated_at":"2026-06-09T04:31:24.432Z","avatar_url":"https://github.com/GustavoKatel.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# AsyncUtils\n\n[![GoDoc](https://godoc.org/github.com/GustavoKatel/asyncutils?status.svg)](https://godoc.org/github.com/GustavoKatel/asyncutils)\n![Main](https://github.com/GustavoKatel/asyncutils/workflows/Main/badge.svg)\n\nSynchornization and asynchronous operations utilities in golang\n\n## Queue\n\nThread-safe queue implementation\n\n```go\ntype Queue interface {\n\tPushBack(el interface{})\n\tPushFront(el interface{})\n\n\t// PopBack removes an element from the back of the queue. Returns nil if queue is empty\n\tPopBack() interface{}\n\n\t// PopFront removes an element from the head of the queue. Returns nil if queue is empty\n\tPopFront() interface{}\n\n\t// Get returns an element in position \"pos\" or nil if \"pos\" is out of bounds\n\tGet(pos int) interface{}\n\n\tSize() int\n}\n```\n\n## Event\n\nEvent synchronizes goroutines with a set-reset flag style\n\n```go\ntype EventWaiter interface {\n\t// Wait waits this flag to be set\n\tWait()\n\n\t// WaitTimeout waits this flag to be set or timeout\n\tWaitTimeout(d time.Duration)\n}\n\n// Event synchronizes goroutines with a set-reset flag style\ntype Event interface {\n\tEventWaiter\n\n\t// IsSet returns true if set has been called\n\tIsSet() bool\n\n\t// Set sets the flag to true and awake pending goroutines\n\tSet()\n\n\t// SetOne sets the flag to true and awake only one pending goroutines\n\tSetOne()\n\n\t// Reset resets this flag\n\tReset()\n}\n```\n\n## Executor\n\nAsynchronous function execution\n\n```go\ntype Executor interface {\n\t// Start starts the executor\n\tStart() error\n\n\t// Stop stops the executor and all the pending jobs\n\tStop() error\n\n\t// PostJob enqueue a job\n\tPostJob(job JobFn) error\n\n\t// Collect executes all jobs posted and return the results in order\n\t// if an error happens, the resulting slice will contain less elements than jobs\n\t// please check ErrorChan\n\tCollect(jobs ...JobWithResultFn) ([]interface{}, error)\n\n\t// CollectChan same as Collect but return a channel with the results\n\tCollectChan(jobs ...JobWithResultFn) \u003c-chan interface{}\n\n\t// ErrorChan registers an error emitting channel\n\tErrorChan(ch chan error)\n\n\t// Len size of the pending queue\n\tLen() int\n}\n```\n\n### Example:\n\n#### Collect results\n\n`Collect` and `CollectChan` keeps the order of the results (Similar to `Promise.all` in js)\n\n```go\n// One worker (goroutine)\nexc, err := NewDefaultExecutor(1)\nassert.Nil(err)\n\nassert.Nil(exc.Start())\ndefer exc.Stop()\n\njob1 := func(ctx context.Context) (interface{}, error) {\n    \u003c-time.After(500 * time.Millisecond)\n    return 1, nil\n}\njob2 := func(ctx context.Context) (interface{}, error) {\n    return 2, nil\n}\n\nresults, err := exc.Collect(job1, job2)\nassert.Nil(err)\nassert.Equal(2, len(results))\nassert.Equal(1, results[0])\nassert.Equal(2, results[1])\n```\n\n#### Enqueue\n\n```go\n// Two workers (goroutines)\nexc, err := NewDefaultExecutor(2)\nassert.Nil(err)\n\nassert.Nil(exc.Start())\ndefer exc.Stop()\n\nresults := make(chan int, 2)\nexc.PostJob(func(ctx context.Context) error {\n    \u003c-time.After(500 * time.Millisecond)\n    results \u003c- 2\n    return nil\n})\n\nexc.PostJob(func(ctx context.Context) error {\n    results \u003c- 1\n    return nil\n})\n\nre := \u003c-results\nassert.Equal(1, re)\n\nre = \u003c-results\nassert.Equal(2, re)\n```\n\n## Scheduler\n\nScheduler and throttler. See [Scheduler](https://github.com/GustavoKatel/asyncutils/blob/master/scheduler/README.md)\n\n```go\ntype Scheduler interface {\n\t// Start starts the worker channel\n\tStart() error\n\n\t// Stop stops all running and scheduled jobs\n\tStop() error\n\n\t// PostJob schedules a job execution\n\tPostJob(job executorIfaces.JobFn) error\n\n\t// PostThrottledJob posts a job only and only if the time span of its last execution was greater than \"duration\"\n\tPostThrottledJob(job executorIfaces.JobFn, delay time.Duration) error\n\n\t// Len returns the number of jobs scheduled\n\tLen() int\n\n\t// ErrorChan registers an error emitting channel\n\tErrorChan(ch chan error)\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgustavokatel%2Fasyncutils","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgustavokatel%2Fasyncutils","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgustavokatel%2Fasyncutils/lists"}