tejzpr / ordered-concurrently

Ordered-concurrently a library for concurrent processing with ordered output in Go. Process work concurrently and returns output in a channel in the order of input. It is useful in concurrently processing items in a queue, and get output in the order provided by the queue.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Change WorkFunction to anonymous function

akamensky opened this issue · comments

Is your feature request related to a problem? Please describe.
Since this is right now declared as an interface with a single Run() method I am unable to pass any other variables as part of context.

Describe the solution you'd like
Using anonymous function instead of interface we don't need to declare any new types and are able to use execution context. Such as (from the example code):

func main() {
        // init some external client
        c := client.New(...)
	max := 10
	inputChan := make(chan concurrently.WorkFunction)
	output := concurrently.Process(inputChan, &concurrently.Options{PoolSize: 10, OutChannelBuffer: 10})
	go func() {
		for work := 0; work < max; work++ {
			inputChan <- func() interface{} {
				// do some work using client
				result := c.DoWork(work)
				return result
			}
		}
		close(inputChan)
	}()
	for out := range output {
		log.Println(out.Value)
	}
}

Describe alternatives you've considered
N/A

Additional context
N/A

I provided an changeset for this. The code change is minimal, mostly changes are in tests.

However I'd suggest to not merge it as-is to v2.0.0 because this will break compatibility for those using this library

commented

v3.0.0 adds support for passing a context, please check if this satisfies your requirement.

It doesn't. Instantiating a new type object doesn't allow for this whatsoever. Contexts semantically are not for passing variables. And there are way more flexibility with anonymous functions than with context.