Skip to content

sjnam/ofanin

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Ordered Fan-In

You can use this ofanin package when you want to speed up processing with goroutines while guaranteeing ordering.

Usage

See ordered and ctan for examples of its use.

package main

import (
	"context"
	"fmt"
	"math/rand"
	"time"

	"github.com/sjnam/ofanin"
)

func main() {
	ctx, cancel := context.WithCancel(context.TODO())
	defer cancel()

	ofin := ofanin.NewOrderedFanIn[string /*input param*/, string /*output param*/](ctx)
	ofin.InputStream = func() <-chan string {
		ch := make(chan string)
		go func() {
			defer close(ch)
			for i := 0; i < 1000; i++ {
				// sleep instread of reading a file
				time.Sleep(time.Duration(rand.Intn(5)) * time.Millisecond)
				ch <- fmt.Sprintf("line:%3d", i)
			}
		}()
		return ch
	}()
	ofin.DoWork = func(str string) string {
		// sleep instead of fetching
		time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond)
		return fmt.Sprintf("%s ... is fetched!", str)
	}

	start := time.Now()

	for s := range ofin.Process() {
		fmt.Println(s)
	}

	fmt.Println("done", time.Now().Sub(start))
}

References

  1. chan chan は意外と美味しい
  2. Concurrency in Go
  3. Sudoku solver with dlx and ofanin

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages