https://github.com/hlts2/gortinep
A goroutine pool for Go
https://github.com/hlts2/gortinep
go go-library golang golang-library goroutine goroutine-pool goroutinemanager gortinep hlts2 interceptor middleware worker-pool
Last synced: 4 months ago
JSON representation
A goroutine pool for Go
- Host: GitHub
- URL: https://github.com/hlts2/gortinep
- Owner: hlts2
- License: mit
- Created: 2018-12-02T06:07:28.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2019-02-25T01:33:20.000Z (about 7 years ago)
- Last Synced: 2025-01-20T08:09:30.454Z (over 1 year ago)
- Topics: go, go-library, golang, golang-library, goroutine, goroutine-pool, goroutinemanager, gortinep, hlts2, interceptor, middleware, worker-pool
- Language: Go
- Homepage:
- Size: 75.2 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# gortinep
gortinep is thinnest goroutine pool library for go application.
## Requirement
Go (>= 1.11)
## Install
```
go get github.com/hlts2/gortinep
```
## Example
### Basic Example
```go
package main
import (
"context"
"fmt"
"github.com/hlts2/gortinep"
"github.com/hlts2/gortinep/middlewares"
"github.com/hlts2/gortinep/middlewares/logger/zap"
"github.com/hlts2/gortinep/middlewares/recovery"
"go.uber.org/zap"
)
func main() {
z := zap.NewExample()
g := gortinep.New(
gortinep.WithErrorChannel(make(chan error, 1)),
gortinep.WithWorkerSize(256),
gortinep.WithInterceptor(
middlewares.ChainInterceptors(
gortinep_recovery.Interceptor(
func(p interface{}) {
z.Info("recovery from panic")
},
),
gortinep_zap.Interceptor(
z,
),
),
),
).Start(context.Background())
defer g.Stop()
const jobSize = 100000
for i := 0; i < jobSize; i++ {
// Register job.
g.Add(func(context.Context) error {
z.Info("finish job")
return nil
})
}
// Get error of job. If execution of all jobs is completed, exit Wait function.
for err := range g.Wait() {
if err != nil {
fmt.Println(err)
}
}
// Register job again.
for i := 0; i < jobSize; i++ {
g.Add(func(context.Context) error {
z.Info("finish job")
return nil
})
}
for err := range g.Wait() {
if err != nil {
fmt.Println(err)
}
}
}
```
### Example with no error channel option
```go
func main() {
z := zap.NewExample()
g := gortinep.New(
gortinep.WithWorkerSize(256),
gortinep.WithInterceptor(
middlewares.ChainInterceptors(
gortinep_recovery.Interceptor(
func(p interface{}) {
z.Info("recovery from panic")
},
),
gortinep_zap.Interceptor(
z,
),
),
),
).Start(context.Background())
defer g.Stop()
const jobSize = 100000
for i := 0; i < jobSize; i++ {
// Register job.
g.Add(func(context.Context) error {
z.Info("finish job")
return nil
})
}
// Execution of all jobs is completed, exit Wait function.
g.Wait()
}
```
## TODO
- [ ] Test Code :pray:
## Author
[hlts2](https://github.com/hlts2)
## LICENSE
gortinep released under MIT license, refer [LICENSE](https://github.com/hlts2/gortinep/blob/master/LICENSE) file.