Repository navigation
add bounded worker pool for connection handling - #2678
DARSHANR007 wants to merge 1 commit into
Conversation
|
Commented on the bug. This is not the right way to do this, this won't be merged. |
|
Thanks for your contribution, review the bug for more details. |
There was a problem hiding this comment.
Code Review
This pull request introduces a worker pool to the cluster service to manage incoming connections more efficiently and prevent unbounded goroutine creation. Key changes include the addition of a connection queue, a configurable number of worker goroutines, and a graceful shutdown mechanism using a WaitGroup. The review feedback highlights several critical improvements: making the Open method idempotent to prevent resource leaks, ensuring the worker pool drains the connection queue before shutting down, and tracking the main accept loop goroutine to ensure a fully synchronized shutdown.
| } | ||
|
|
||
| // Open opens the Service. | ||
| func (s *Service) Open() error { |
There was a problem hiding this comment.
The Open method is not idempotent. Calling it multiple times will overwrite the connCh and doneCh channels and spawn a new set of worker goroutines, while the previous ones will remain active and leaked because their control channels are no longer accessible. Consider adding a check to see if the service is already open.
| func (s *Service) Open() error { | |
| func (s *Service) Open() error { | |
| s.mu.Lock() | |
| defer s.mu.Unlock() | |
| if s.doneCh != nil { | |
| return nil | |
| } |
| func (s *Service) worker() { | ||
| defer s.wg.Done() | ||
| for { | ||
| select { | ||
| case conn, ok := <-s.connCh: | ||
| if !ok { | ||
| // Channel closed, exit worker | ||
| return | ||
| } | ||
| s.handleConn(conn) | ||
| case <-s.doneCh: | ||
| // Stop signal received, exit worker | ||
| return | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
The current worker implementation exits immediately when doneCh is closed, which causes any connections already buffered in connCh to be dropped. A more idiomatic and graceful approach is to have workers drain the channel using a range loop, which will naturally terminate once the channel is closed and empty.
func (s *Service) worker() {
defer s.wg.Done()
for conn := range s.connCh {
s.handleConn(conn)
}
}| } | ||
|
|
||
| // Start the accept loop | ||
| go s.serve() |
Title
Add bounded worker pool for connection handling
Description
Implements a bounded worker pool pattern for the cluster service to prevent unbounded goroutine spawning.
Problem
service.go:267-276 — each accepted connection spawns unbounded goroutines. Connection flood exhausts memory.
Solution
Results
Files Changed
cluster/service.go- Worker pool implementationcluster/service_worker_pool_test.go- Test (new)Fixes #2617