Skip to content

add bounded worker pool for connection handling - #2678

Closed
DARSHANR007 wants to merge 1 commit into
rqlite:masterfrom
DARSHANR007:fix/goroutine-connection-pool
Closed

DARSHANR007 wants to merge 1 commit into
rqlite:masterfrom
DARSHANR007:fix/goroutine-connection-pool

Conversation

@DARSHANR007

Copy link
Copy Markdown
Contributor

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

  • Fixed pool of 100 worker goroutines (configurable)
  • Buffered connection queue of 1000 (configurable)
  • Graceful shutdown with proper synchronization

Results

  • Memory: 100k connections: 100GB → 1-2MB
  • Tests: All pass (6 new tests )

Files Changed

  • cluster/service.go - Worker pool implementation
  • cluster/service_worker_pool_test.go - Test (new)

Fixes #2617

@otoolep

otoolep commented May 20, 2026

Copy link
Copy Markdown
Member

Commented on the bug. This is not the right way to do this, this won't be merged.

@otoolep

otoolep commented May 20, 2026

Copy link
Copy Markdown
Member

Thanks for your contribution, review the bug for more details.

@otoolep otoolep closed this May 20, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread cluster/service.go
}

// Open opens the Service.
func (s *Service) Open() error {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

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.

Suggested change
func (s *Service) Open() error {
func (s *Service) Open() error {
s.mu.Lock()
defer s.mu.Unlock()
if s.doneCh != nil {
return nil
}

Comment thread cluster/service.go
Comment on lines +327 to 342
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
}
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

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)
	}
}

Comment thread cluster/service.go
}

// Start the accept loop
go s.serve()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The serve goroutine should be tracked by the WaitGroup to ensure that Close() waits for the accept loop to fully terminate before returning. This prevents potential race conditions during shutdown.

Suggested change
go s.serve()
s.wg.Add(1)
go s.serve()

@DARSHANR007
DARSHANR007 deleted the fix/goroutine-connection-pool branch May 20, 2026 18:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Service.serve has no connection limit

2 participants