https://github.com/paulshpilsher/concurrent-go
Golang: a concurrent function runner with quota on how many functions can be executing at the same time. There are two flavors of concurrent runners are implemented. One that uses semaphore synchronization primitive and the other uses channels.
https://github.com/paulshpilsher/concurrent-go
go goatomic gochannels golang golang-package goroutine goroutine-pool semaphore
Last synced: about 2 months ago
JSON representation
Golang: a concurrent function runner with quota on how many functions can be executing at the same time. There are two flavors of concurrent runners are implemented. One that uses semaphore synchronization primitive and the other uses channels.
- Host: GitHub
- URL: https://github.com/paulshpilsher/concurrent-go
- Owner: PaulShpilsher
- License: mit
- Created: 2023-02-04T06:26:43.000Z (about 2 years ago)
- Default Branch: master
- Last Pushed: 2023-11-23T16:34:57.000Z (over 1 year ago)
- Last Synced: 2025-01-17T16:59:06.752Z (3 months ago)
- Topics: go, goatomic, gochannels, golang, golang-package, goroutine, goroutine-pool, semaphore
- Language: Go
- Homepage:
- Size: 25.4 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Golang concurrent function (goroutine) runner with quota
[](https://goreportcard.com/report/github.com/paulshpilsher/concurrent-go)A concurrent function runner with quota on how many functions can be executing at the same time.
## Features
A concurrent runner purpose is to enforce maximum number of goroutines that can execute simultaneously (quota). When the quota is reached scheduling new function executions is blocked until some of the running functions are finished.
It also maintains an atomic counter of how many functions are executing at any point of time.
## Implementation
There are two flavors of concurrent runners are implemented. One that uses semaphore synchronization primitive and the other uses channels.
Both have common functionality described by the interface:
```go
type Runner interface {
// Concurrently executes a function wrapped in a goroutine.
Run(task func()) error// Waits for all running functions to complete and frees resources.
WaitAndClose() error// Returns the number of currently executing functions.
GetNumberOfRunningTasks() int// Returns the quota limit
GetQuota() int
}
```## Usage
Get the package
```bash
go get github.com/paulshpilsher/concurrent-go
```In code import runner.
To use channel-based runner:
```go
import "github.com/paulshpilsher/concurrent-go/concurrency/chan/runner"
```To use sync-based runner:
```go
import "github.com/paulshpilsher/concurrent-go/concurrency/sync/runner"
```Use the runner
```go
theRunner := runner.New(quota)
if err != nil {
panic(err)
}
for i := 0; i < 1000; i++ {
theRunner.Run(func() {
// put some code to be exectuted
})
}theRunner.WaitAndClose()
```## Examples
The exapmples are in the ./examples/ directory.
Running examples using make utility:
```shell
make run-example-channel
```or
```shell
make run-example-sync
```## Testing
Running unit tests using make utility:
```shell
make test
```Benchmarks:
```shell
make bench
```## Acknowledgements
- Inspiration ["Simple Made Easy" - Rich Hickey (2011)](https://www.youtube.com/watch?v=SxdOUGdseq4)
- References