An open API service indexing awesome lists of open source software.

https://github.com/1995parham/koi

Generic Goroutine and Worker Manager based on https://github.com/mehditeymorian/koi
https://github.com/1995parham/koi

Last synced: 9 months ago
JSON representation

Generic Goroutine and Worker Manager based on https://github.com/mehditeymorian/koi

Awesome Lists containing this project

README

          


Koi logo

KOI

Generic Goroutine and Worker Manager



go version

license
GitHub Workflow Status
Codecov

## Installation

You can add **Koi** into your project as follows:

```bash
go get github.com/1995parham/koi
```

## Usage

In Koi you first register a worker on a Pond then push your inputs.
Your worker has concurrency configuration for handling inputs.

Worker has generic interface. The first generic parameter is an input rype and the second parameter
is an output parameter.

```go
package main

import (
"log"
"sync"
"time"

"github.com/1995parham/koi"
)

func main() {
pond := koi.NewPond[int, koi.NoReturn]()

var wg sync.WaitGroup

printer := func(a int) koi.NoReturn {
time.Sleep(1 * time.Second)
log.Println(a)

wg.Done()

return koi.None
}

// nolint: gomnd
printWorker := koi.MustNewWoker(printer, 2, 10)

pond.MustRegisterWorker("printer", printWorker)

for i := 0; i < 10; i++ {
wg.Add(1)

if _, err := pond.AddWork("printer", i); err != nil {
log.Printf("error while adding job: %s\n", err)
}
}

wg.Wait()
log.Println("all job added")
}
```

**Note**: `pond.AddWork` is non-blocking unless worker queue is full.

## Terminology

- **Koi**: Koi is an informal name for the colored variants of C. rubrofuscus kept for ornamental purposes.
- **Pond**: an area of water smaller than a lake, often artificially made.