Skip to content

Commit

Permalink
test: Correct linger option related type
Browse files Browse the repository at this point in the history
In Winsock, linger related struct's menber types
are not `int` but `u_short` (unsigned short).

`linger_timeout` also should be set up when linger_timeout > 0.

Signed-off-by: Hiroshi Hatake <hatake@clear-code.com>
  • Loading branch information
cosmo0920 committed Mar 5, 2020
1 parent 7ae24c5 commit 7d16531
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion lib/fluent/plugin_helper/socket_option.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
module Fluent
module PluginHelper
module SocketOption
# ref: https://docs.microsoft.com/en-us/windows/win32/api/winsock/ns-winsock-linger
FORMAT_STRUCT_LINGER_WINDOWS = 'S!S!' # { u_short l_onoff; u_short l_linger; }
FORMAT_STRUCT_LINGER = 'I!I!' # { int l_onoff; int l_linger; }
FORMAT_STRUCT_TIMEVAL = 'L!L!' # { time_t tv_sec; suseconds_t tv_usec; }

Expand Down Expand Up @@ -49,9 +51,21 @@ def socket_option_set(sock, resolve_name: nil, nonblock: false, linger_timeout:
if nonblock
sock.fcntl(Fcntl::F_SETFL, Fcntl::O_NONBLOCK)
end
if linger_timeout
if Fluent.windows?
# To prevent closing socket forcibly on Windows,
# this options shouldn't be set up when linger_timeout equals to 0 (including nil).
# This unintended behavior always ocurrs on Windows when linger_timeout.to_i == 0.
# This unintented behavior causes "Errno::ECONNRESET: An existing connection was forcibly
# closed by the remote host." on Windows.
if linger_timeout.to_i > 0
optval = [1, linger_timeout.to_i].pack(FORMAT_STRUCT_LINGER_WINDOWS)
socket_option_set_one(sock, :SO_LINGER, optval)
end
else
if linger_timeout
optval = [1, linger_timeout.to_i].pack(FORMAT_STRUCT_LINGER)
socket_option_set_one(sock, :SO_LINGER, optval)
end
end
if recv_timeout
optval = [recv_timeout.to_i, 0].pack(FORMAT_STRUCT_TIMEVAL)
Expand Down

0 comments on commit 7d16531

Please sign in to comment.