Repository: puma/puma
File: lib/puma/server.rb (Ruby)

Code before:
    def add_tcp_listener(host, port, optimize_for_latency = true, backlog = 1024)
      @binder.add_tcp_listener host, port, optimize_for_latency, backlog
    end

    def add_ssl_listener(host, port, ctx, optimize_for_latency = true,
                         backlog = 1024)
      @binder.add_ssl_listener host, port, ctx, optimize_for_latency, backlog
    end

    def add_unix_listener(path, umask = nil, mode = nil, backlog = 1024)
      @binder.add_unix_listener path, umask, mode, backlog
    end

    def update_thread_pool_min_max(min: @thread_pool.min, max: @thread_pool.max)
      if min > max
        @log_writer.log "`min' value cannot be greater than `max' value."
        return
      end

      if min <= 0
        @log_writer.log "`min' value cannot be less than 0"
        return
      end

      @thread_pool.min, @thread_pool.max = min, max
      @min_threads, @max_threads = min, max
    end

    # @!attribute [r] connected_ports
    def connected_ports
      @binder.connected_ports
    end
  end
end


Review comment (suggestion):
If we want the metrics from `def stats` in the pool to be correct, I think we need to wrap setting these values in a mutex call.

There's other logic that also uses this value like `trim`

```ruby
      with_mutex do
        free = @waiting - @todo.size
        if (force or free > 0) and @spawned - @trim_requested > @min
```

And

```ruby
    def <<(work)
      with_mutex do
        if @shutdown
          raise "Unable to add work while shutting down"
        end

        @todo << work
        t = @todo.size
        @backlog_max = t if t > @backlog_max

        if @waiting < @todo.size and @spawned < @max
          spawn_thread
        end
```

We might want to introduce more, finer-grained, locks in the future. Because right now there's only one and it's "stop everything" which includes stopping enqueuing and dequing of requests. 

Code after:
    # below are 'delegations' to binder
    # remove in Puma 7?


    def add_tcp_listener(host, port, optimize_for_latency = true, backlog = 1024)
      @binder.add_tcp_listener host, port, optimize_for_latency, backlog
    end

    def add_ssl_listener(host, port, ctx, optimize_for_latency = true,
                         backlog = 1024)
      @binder.add_ssl_listener host, port, ctx, optimize_for_latency, backlog
    end

    def add_unix_listener(path, umask = nil, mode = nil, backlog = 1024)
      @binder.add_unix_listener path, umask, mode, backlog
    end

    # Updates the minimum and maximum number of threads in the thread pool.
    #
    # This method allows dynamic adjustment of the thread pool size while the server
    # is running. It validates the provided values and updates both the thread pool
    # and the server's thread configuration.
    #
    # @param min [Integer] The minimum number of threads to maintain in the pool.
    #   Defaults to the current minimum if not specified. Must be greater than 0
    #   and less than or equal to max.
    # @param max [Integer] The maximum number of threads allowed in the pool.
    #   Defaults to the current maximum if not specified. Must be greater than or
    #   equal to min.
    #
    # @return [void]
    #
    # @note If validation fails, a warning message is logged and no changes are made.
    #
    # @example Update both min and max threads
    #   server.update_thread_pool_min_max(min: 2, max: 8)
    #
    # @example Update only the minimum threads
    #   server.update_thread_pool_min_max(min: 4)
    #
    # @example Update only the maximum threads
    #   server.update_thread_pool_min_max(max: 16)
    #
    def update_thread_pool_min_max(min: @min_threads, max: @max_threads)
      if min > max
        @log_writer.log "`min' value cannot be greater than `max' value."
        return
      end

      if min < 0
        @log_writer.log "`min' value cannot be less than 0"