pond icon indicating copy to clipboard operation
pond copied to clipboard

Feature request: Add "Join" functionality to the group

Open opengs opened this issue 8 months ago • 1 comments

Problem

I am running an application that consistently operates with a specific number of workers. Additionally, when a worker discovers work, it requires a boost from the pool. To achieve this, a limited shared pool for boosting is necessary.

Proposed Feature

The idea is to introduce a Join functionality to the Group, enabling the current goroutine not only to wait for all tasks in the group to complete but also to assist in executing the tasks within it. So the general flow is:

  • When a goroutine calls Join on a Group, it becomes part of the execution pool for that group.
  • The goroutine waits for all tasks within the group to finish, actively participating in executing these tasks rather than idly waiting.

Example

package main

import (
	"fmt"

	"github.com/alitto/pond"
)

func main() {

	// Create a pool
	pool := pond.New(10, 1000)
	defer pool.StopAndWait()

	// Create a task group
	group := pool.Group()

	// Submit a group of tasks
	for i := 0; i < 20; i++ {
		n := i
		group.Submit(func() {
			fmt.Printf("Running group task #%d\n", n)
		})
	}

	// Join pool for this group and help for all tasks in the group to complete
	group.Join()
}

Other

Maybe for consistency this functionality also should be added to the WorkerPool object itself, but I cant imagine situation where it can be needed.

opengs avatar May 27 '24 16:05 opengs