Defer checking if socket is closed #2602
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Avoid making
getsockopt
system call for every requestDescription
In function
handle_request
, we can check if the socket is closed when rescuing IOError, saving onegetsockopt
system call for every request.In the current implementation, the socket may be closed by the client after we check the socket status using
closed_socket?
and before finishing writing the data.Writing to a closed socket will raise
IOError
. I am not sure which of the followingError
I should use, my commit uses "Socket timeout writing data"Analysis detail:
hello.ru
benchmark for 30 seconds,sudo perf stat -e 'syscalls:*' -p $(pidof bundle) sleep 10
, this will use perf to collect all syscalls made by puma process for 10 secondsperf stat result:
Benchmark RPS is around 4k to 5k, here is my basic understanding of what each syscall is used for:
getpeername
, for getting the peer IP addressrecvfrom
, ?setsockopt
,cork_socket
anduncork_socket
, this will be removed in Improve client response code, chunked bodies #2595getsockopt
, check socket status to see if the socket is closedepoll_*
, epoll functions used innio4r
select
,IO.select
will get the socket that is ready for reading or writingppoll
, probably some blocking IO functionsread
andwrite
, for reading request and writing response datamadvise
andmprotect
, GC stuffsfutex
, puma uses threads,mutex
andconditional variable
would cause lot's offutex
usage, looking forward to getting rid of them(ALL) in Support for non-blocking fibers #2601sched_yield
, process management, https://linux.die.net/man/2/sched_yieldYour checklist for this pull request
[ci skip]
to the title of the PR.#issue
" to the PR description or my commit messages.