https://github.com/guoquanwei/alloter
Alloter is a goroutines concurrent toolkit.
https://github.com/guoquanwei/alloter
golang goroutine-pool goroutine-safe goroutinemanager
Last synced: 4 months ago
JSON representation
Alloter is a goroutines concurrent toolkit.
- Host: GitHub
- URL: https://github.com/guoquanwei/alloter
- Owner: guoquanwei
- License: mit
- Created: 2020-10-29T13:21:55.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2025-03-19T09:53:07.000Z (10 months ago)
- Last Synced: 2025-07-14T00:43:31.048Z (6 months ago)
- Topics: golang, goroutine-pool, goroutine-safe, goroutinemanager
- Language: Go
- Homepage:
- Size: 48.8 KB
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## Introduction
[](https://godoc.org/github.com/guoquanwei/alloter)
Alloter is a goroutine's concurrent toolkit to help execute functions concurrently in an efficient and safe way.
It was inspired by a Node.js package's function, [bluebird](https://npmjs.com/package/bluebird) .map()
* It supports concurrency limits.
* It supports recovery goroutine's panic.
* It supports specifying the overall timeout to avoid blocking.
* It supports the use of goroutines pool(invoke [ants/v2](https://github.com/panjf2000/ants)).
* It supports context passing; listen ctx.Done(), will return.
* It supports ending other tasks when an error occurs.
### init Alloter
```go
// simple concurrency
func NewAlloter() *Alloter
// concurrency control
func NewCtrlAlloter(workerNum int) *Alloter
// goroutines pool from ants/v2
func NewPooledAlloter(workerNum int) *Alloter
```
### exec tasks
```go
type Task func() error
func (c *Alloter) Exec(tasks []Task) error
func (c *Alloter) ExecWithContext(ctx context.Context, tasks []Task) error
```
### Demo!!!
```go
package main
import (
"github.com/guoquanwei/alloter"
"sync"
)
func (that *Controller) TestGoRunLock(ctx echo.Context) {
userIds := []string{
"uuid_1",
"uuid_2",
}
var tasks []alloter.Task
var users []third_parts.User
mux := sync.Mutex{}
for _, uid := range userIds {
func(userId string) {
tasks = append(tasks, func() error {
user, resErr := third_parts.GetUserById(userId)
if resErr != nil {
return resErr
}
mux.Lock()
users = append(users, user)
mux.Unlock()
return nil
})
}(uid)
}
p := alloter.NewCtrlAlloter(1)
err = p.ExecWithContext(ctx.Request().Context(), tasks)
if err != nil {
ctx.JSON(500, err.Error())
return
}
ctx.JSON(200, users)
return
}
```