Skip to content

Commit 477a220

Browse files
dylanahsmithEric Wong
authored andcommitted
Check for Socket::TCP_INFO constant before trying to get TCP_INFO
The ruby constant Socket::TCP_INFO is only defined if TCP_INFO is defined in C, so we can just check for the presence of that ruby constant instead of rescuing SocketError from the call to getsockopt.
1 parent 9cced5d commit 477a220

File tree

1 file changed

+3
-14
lines changed

1 file changed

+3
-14
lines changed

lib/unicorn/http_request.rb

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class Unicorn::HttpParser
2929
EMPTY_ARRAY = [].freeze
3030
@@input_class = Unicorn::TeeInput
3131
@@check_client_connection = false
32-
@@tcpi_inspect_ok = nil
32+
@@tcpi_inspect_ok = Socket.const_defined?(:TCP_INFO)
3333

3434
def self.input_class
3535
@@input_class
@@ -154,19 +154,8 @@ def closed_state?(state) # :nodoc:
154154
# Not that efficient, but probably still better than doing unnecessary
155155
# work after a client gives up.
156156
def check_client_connection(socket) # :nodoc:
157-
if Unicorn::TCPClient === socket && @@tcpi_inspect_ok != false
158-
if @@tcpi_inspect_ok
159-
opt = socket.getsockopt(:IPPROTO_TCP, :TCP_INFO).inspect
160-
else
161-
@@tcpi_inspect_ok = true
162-
opt = begin
163-
socket.getsockopt(:IPPROTO_TCP, :TCP_INFO)
164-
rescue SocketError
165-
@@tcpi_inspect_ok = false
166-
return write_http_header(socket)
167-
end.inspect
168-
end
169-
157+
if Unicorn::TCPClient === socket && @@tcpi_inspect_ok
158+
opt = socket.getsockopt(Socket::IPPROTO_TCP, Socket::TCP_INFO).inspect
170159
if opt =~ /\bstate=(\S+)/
171160
raise Errno::EPIPE, "client closed connection".freeze,
172161
EMPTY_ARRAY if closed_state_str?($1)

0 commit comments

Comments
 (0)