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

Code before:
          client.finish(@first_data_timeout)
        end

        while true
          @requests_count += 1
          case handle_request(client, buffer)
          when false
            break
          when :async
            close_socket = false
            break
          when true
            buffer.reset

            ThreadPool.clean_thread_locals if clean_thread_locals

            requests += 1

            # Closing keepalive sockets after they've made a reasonable
            # number of requests allows Puma to service many connections
            # fairly, even when the number of concurrent connections exceeds
            # the size of the threadpool. It also allows cluster mode Pumas
            # to keep load evenly distributed across workers, because clients
            # are randomly assigned a new worker when opening a new connection.
            #
            # Previously, Puma would kick connections in this conditional back
            # to the reactor. However, because this causes the todo set to increase
            # in size, the wait_until_full mutex would never unlock, leaving
            # any additional connections unserviced.
            break if requests >= @max_fast_inline

            check_for_more_data = @status == :run

            next_request_ready = with_force_shutdown(client) do
              client.reset(check_for_more_data)
            end

            unless next_request_ready
              break unless @queue_requests
              client.set_timeout @persistent_timeout
              if @reactor.add client
                close_socket = false
                break
              end
            end
          end
        end
        true
      rescue StandardError => e
        client_error(e, client)
        # The ensure tries to close +client+ down

Review comment (suggestion):
`fast_check = requests < @max_fast_inline && thread_pool.backlog == 0`

Code after:
        with_force_shutdown(client) do
          client.finish(@first_data_timeout)
        end

        while true
          @requests_count += 1
          case handle_request(client, buffer, requests + 1)
          when false
            break
          when :async
            close_socket = false
            break
          when true
            buffer.reset

            ThreadPool.clean_thread_locals if clean_thread_locals

            requests += 1

            # As an optimization, try to read the next request from the
            # socket for a short time before returning to the reactor.
            fast_check = @status == :run

            # Always pass the client back to the reactor after a reasonable
            # number of inline requests if there are other requests pending.
            fast_check = false if requests >= @max_fast_inline &&
              @thread_pool.backlog > 0

            next_request_ready = with_force_shutdown(client) do
              client.reset(fast_check)
            end

            unless next_request_ready
              break unless @queue_requests
              client.set_timeout @persistent_timeout
              if @reactor.add client
                close_socket = false
                break
              end
            end
          end
        end
        true
      rescue StandardError => e
        client_error(e, client)
        # The ensure tries to close +client+ down
        requests > 0
      ensure
        buffer.reset

        begin