Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow IO::Endpoint::Wrapper#accept to accept task: #14

Open
korbin opened this issue Oct 1, 2024 · 0 comments
Open

Allow IO::Endpoint::Wrapper#accept to accept task: #14

korbin opened this issue Oct 1, 2024 · 0 comments

Comments

@korbin
Copy link

korbin commented Oct 1, 2024

I'm currently in the process of migrating from Puma to Falcon and was looking for a way to throttle the incoming request queue so that my upstream nginx reverse proxy doesn't overload a single thread with Fibers-per-request - someone else had this issue as well.

I'd like to prevent the actual Socket accept from happening until capacity is available:

This may or may not be the best way to go about this and this behavior should probably be customizable, but, I would abstractly like to be able to:

def accept(server, timeout: nil, linger: nil, task: Task.current, **options, &block)
  loop do
    task.async do
      socket, address = server.accept

      if linger
        socket.setsockopt(SOL_SOCKET, SO_LINGER, 1)
      end

      if timeout
        set_timeout(socket, timeout)
      end

      # Some sockets, notably SSL sockets, need application level negotiation before they are ready:
      if socket.respond_to?(:start)
        begin
          socket.start
        rescue
          socket.close
          raise
        end
      end

      # It seems like OpenSSL doesn't return the address of the peer when using `accept`, so we need to get it from the socket:
      address ||= socket.remote_address

      yield socket, address
    end
  end
end

And then in Falcon, I can pass an Async::Semaphore to the endpoint.

This is currently working with the above monkey-patched into the class (in config/falcon.rb) - maybe there's a cleaner abstraction.

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

No branches or pull requests

1 participant