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.
BSD 3-Clause "New" or "Revised" License
37 stars 8 forks source link

Change WorkFunction to anonymous function #6

Open akamensky opened 2 years ago

akamensky commented 2 years ago

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

akamensky commented 2 years ago

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

tejzpr commented 2 years ago

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

akamensky commented 2 years ago

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.

ricardobranco777 commented 1 year ago

I manage to pass more variables like this:

https://github.com/ricardobranco777/regview/blob/master/print.go#L18