Skip to content

1995parham/koi

Repository files navigation

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:

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.

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.

About

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

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages