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

Code before:
        check = @check
        sockets = [check] + @binder.ios
        pool = @thread_pool
        queue_requests = @queue_requests

        remote_addr_value = nil
        remote_addr_header = nil

        case @options[:remote_address]
        when :value
          remote_addr_value = @options[:remote_address_value]
        when :header
          remote_addr_header = @options[:remote_address_header]
        end

        while @status == :run
          begin
            ios = IO.select sockets
            ios.first.each do |sock|
              if sock == check
                break if handle_check
              else
                begin
                  pool.wait_until_not_full
                  if io = sock.accept_nonblock
                    client = Client.new io, @binder.env(sock)
                    if remote_addr_value
                      client.peerip = remote_addr_value
                    elsif remote_addr_header
                      client.remote_addr_header = remote_addr_header
                    end

                    pool << client
                  end
                rescue SystemCallError
                  # nothing
                rescue Errno::ECONNABORTED
                  # client closed the socket even before accept
                  begin
                    io.close
                  rescue
                    Thread.current.purge_interrupt_queue if Thread.current.respond_to? :purge_interrupt_queue
                  end
                end
              end
            end
          rescue Object => e
            @events.unknown_error self, e, "Listen loop"
          end
        end


Review comment (performance):
There's a micro-optimisation here, which I think is worth flagging only because this happens in a while loop. `to_f` is already called when the option's value is set, [here](https://github.com/puma/puma/pull/2079/files#diff-122c03b9c96f060a9cc29b5ed3446467e1909b0054b4f5cfd95ee41cfff832e7R676).

```ruby
require "benchmark/ips"

options = {foo: 0.5}

Benchmark.ips do |b|
  b.report("to_f on option") { options[:foo].to_f }

  b.report("option directly") { options[:foo] }

  b.compare!
end
```

```
Warming up --------------------------------------
      to_f on option     1.593M i/100ms
     option directly     2.169M i/100ms
Calculating -------------------------------------
      to_f on option     16.280M (± 1.2%) i/s -     82.842M in   5.089545s
     option directly     20.139M (± 8.5%) i/s -    101.943M in   5.103727s

Comparison:
     option directly: 20138636.0 i/s
      to_f on option: 16279512.8 i/s - 1.24x  (± 0.00) slower
```

Code after:
        check = @check
        sockets = [check] + @binder.ios
        pool = @thread_pool
        queue_requests = @queue_requests

        remote_addr_value = nil
        remote_addr_header = nil

        case @options[:remote_address]
        when :value
          remote_addr_value = @options[:remote_address_value]
        when :header
          remote_addr_header = @options[:remote_address_header]
        end

        while @status == :run
          begin
            ios = IO.select sockets
            ios.first.each do |sock|
              if sock == check
                break if handle_check
              else
                begin
                  pool.wait_until_not_full
                  pool.wait_for_less_busy_worker(
                    @options[:wait_for_less_busy_worker].to_f)

                  if io = sock.accept_nonblock
                    client = Client.new io, @binder.env(sock)
                    if remote_addr_value
                      client.peerip = remote_addr_value
                    elsif remote_addr_header
                      client.remote_addr_header = remote_addr_header
                    end

                    pool << client
                  end
                rescue SystemCallError
                  # nothing
                rescue Errno::ECONNABORTED
                  # client closed the socket even before accept
                  begin
                    io.close
                  rescue
                    Thread.current.purge_interrupt_queue if Thread.current.respond_to? :purge_interrupt_queue
                  end
                end
              end
            end
          rescue Object => e
            @events.unknown_error self, e, "Listen loop"