trustmaster / goflow

Flow-based and dataflow programming library for Go (golang)
MIT License
1.6k stars 125 forks source link

Support concurrency limits inside Components #18

Closed rw closed 10 years ago

rw commented 10 years ago

It is sometimes desirable to set the number of active goroutines inside a Component. One common example would be to create a pool of workers to fetch items from a database, because too many active requests would cause a decline in overall throughput.

This issue is for discussing how to best solve the problem for both async and sync modes.

Note that it would be valuable to make the concurrency level adjustable. For example, if the network connection used by the Component becomes saturated, I'd like to tell the Component to reduce its active goroutine count.

trustmaster commented 10 years ago

Adjusting concurrency limit at run-time would require making that option thread-safe.

trustmaster commented 10 years ago

I've added this feature as a separate component functioning mode called flow.ComponentModePool rather than mixing it up with Sync or Async. Here is an example of starting such a component:

// Creating a process just any normal way
proc := new(Processor)
// Switching it into Pool mode
proc.Component.Mode = flow.ComponentModePool
// Setting pool size, up to 255 concurrent workers supported currently
proc.Component.PoolSize = 8 // 8 workers
// And it's run just as any other proc
flow.RunProc(proc)

The PoolSize is set at design/compile time because it preforks when just starting the network/process. It can be changed at run-time too, but requires restarting the process anyways. Making it pure dynamic would require a lot more syncing and thread-safety spaghetti.