-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 0b4bc49
Showing
6 changed files
with
215 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
name: build | ||
on: | ||
push: | ||
- master | ||
- dev | ||
pull_request: | ||
branches: | ||
- master | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: actions/setup-go@v4 | ||
with: | ||
go-version: ">=1.19" | ||
- run: go version | ||
|
||
- name: "Install dependencies" | ||
run: go mod download | ||
|
||
- name: "Run tests" | ||
run: go test -v ./... | ||
|
||
- name: "Build" | ||
run: go build -o ./bin/main ./cmd | ||
env: | ||
CGO_ENABLED: "0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/.idea | ||
.DS_Store | ||
*.out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright © 2023 David Lavieri <david@lavieri.dev> | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# Krun - QueueRun | ||
|
||
Krun is a simple worker queue, which provides an easy way to manage and execute jobs concurrently. It can wait for all jobs to finish, and get the results from the executed jobs. The package can be found at [github.com/falmar/krun](https://github.com/falmar/krun). | ||
|
||
## Installation | ||
|
||
To install the Krun package, simply run: | ||
|
||
```bash | ||
go get -u github.com/falmar/krun | ||
``` | ||
|
||
## Usage | ||
|
||
Here's a basic example of how to use the Krun package: | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/falmar/krun" | ||
"time" | ||
) | ||
|
||
func main() { | ||
cfg := krun.NewWorkerQueueConfig{ | ||
Size: 5, // number of workers | ||
WaitSleep: time.Millisecond * 10, | ||
} | ||
|
||
queue := krun.NewWorkerQueue(cfg) | ||
|
||
job := func(ctx context.Context) (interface{}, error) { | ||
time.Sleep(time.Millisecond * 100) | ||
return "Hello, Krun!", nil | ||
} | ||
|
||
ctx := context.Background() | ||
resChan := queue.Run(ctx, job) | ||
|
||
res := <-resChan | ||
if res.Error != nil { | ||
fmt.Println("Error:", res.Error) | ||
} else { | ||
fmt.Println("Result:", res.Data) | ||
} | ||
} | ||
``` | ||
|
||
## License | ||
|
||
Krun is released under the MIT License. See [LICENSE](LICENSE) for more information. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module github.com/falmar/krun | ||
|
||
go 1.19 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package krun | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
"time" | ||
) | ||
|
||
type Result struct { | ||
Data interface{} | ||
Error error | ||
} | ||
type Job func(ctx context.Context) (interface{}, error) | ||
|
||
type Krun interface { | ||
Run(ctx context.Context, f Job) <-chan *Result | ||
Wait(ctx context.Context) | ||
Size() int | ||
} | ||
|
||
type krun struct { | ||
n int | ||
waitSleep time.Duration | ||
workers chan *worker | ||
working int | ||
mu sync.RWMutex | ||
} | ||
type worker struct { | ||
ctx context.Context | ||
job Job | ||
result chan *Result | ||
} | ||
|
||
type NewKrunConfig struct { | ||
Size int | ||
WaitSleep time.Duration | ||
} | ||
|
||
func New(cfg NewKrunConfig) Krun { | ||
k := &krun{ | ||
n: cfg.Size, | ||
workers: make(chan *worker, cfg.Size), | ||
waitSleep: cfg.WaitSleep, | ||
} | ||
|
||
for i := 0; i < cfg.Size; i++ { | ||
k.push(&worker{}) | ||
} | ||
|
||
return k | ||
} | ||
|
||
func (k *krun) Size() int { | ||
k.mu.RLock() | ||
s := k.n | ||
k.mu.RUnlock() | ||
return s | ||
} | ||
|
||
func (k *krun) Run(ctx context.Context, f Job) <-chan *Result { | ||
// get worker from the channel | ||
w := k.pop() | ||
|
||
// assign Job to the worker and Run it | ||
cr := make(chan *Result) | ||
w.job = f | ||
w.result = cr | ||
go k.work(ctx, w) | ||
|
||
// return channel to the caller | ||
return cr | ||
} | ||
|
||
func (k *krun) Wait(ctx context.Context) { | ||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
default: | ||
// "wait" until all workers are back | ||
if k.len() < k.n { | ||
time.Sleep(k.waitSleep) | ||
continue | ||
} | ||
|
||
break | ||
} | ||
} | ||
} | ||
|
||
func (k *krun) work(ctx context.Context, w *worker) { | ||
// run the job | ||
d, err := w.job(ctx) | ||
|
||
// send Result into the caller channel | ||
// this will block until is read | ||
w.result <- &Result{d, err} | ||
|
||
// return worker to Krun | ||
k.push(w) | ||
} | ||
func (k *krun) push(w *worker) { | ||
k.workers <- w | ||
} | ||
|
||
func (k *krun) pop() *worker { | ||
w := <-k.workers | ||
return w | ||
} | ||
|
||
func (k *krun) len() int { | ||
k.mu.RLock() | ||
l := len(k.workers) | ||
k.mu.RUnlock() | ||
|
||
return l | ||
} |