Skip to content

Service.serve has no connection limit #2617

Closed

Description

@otoolep

service.go:267-276 — each accepted connection spawns unbounded goroutines. Connection flood exhausts memory.

Activity

  1. added a commit that references this issue on Apr 29, 2026
    53a6bf6
  2. DARSHANR007 commented on May 20, 2026

    @DARSHANR007
    Contributor

    Hey @otoolep

    Appreciate if you could check my solution for this issue.

    Problem

    The cluster service currently creates a new goroutine for every incoming connection without any limit.

    This can become a problem if too many connections are opened at once. A large number of goroutines increases memory usage heavily and may eventually cause the server to run out of memory and crash.

    For example, if an attacker or client opens thousands of connections continuously, the server may create thousands of goroutines and become unstable.

    Proposed Solution

    Use a bounded worker pool to control how many connections are handled at the same time.

    Suggested approach:

    • Create a fixed number of worker goroutines (for example: 100)
    • Store incoming connections in a buffered queue
    • Workers pick connections from the queue and process them
    • Add proper synchronization and graceful shutdown handling

    This prevents unlimited goroutine creation and makes connection handling more predictable and memory-safe.

  3. otoolep commented on May 20, 2026

    @otoolep
    MemberAuthor

    That is the wrong approach, and not how systems solve the problem. Opening goroutines ahead of time is wasteful.

    Instead you count the number of connections in use (there is already a goroutine spun up per connection) and when the next connection attempt that comes in that would exceed the max count you don't answer it or you block waiting for a goroutine to come free. This is normally done in Go with channels of a fixed capacity.

    Take a look at Go concurrency patterns online, especially those concerning channels.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions