Skip to content

Commit

Permalink
make time out configurable when perform connect on socket
Browse files Browse the repository at this point in the history
  • Loading branch information
Hao Gao committed Jan 22, 2019
1 parent c485bbf commit 0735a41
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions lib/fluent/plugin/out_forward.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ def initialize

desc 'The timeout time when sending event logs.'
config_param :send_timeout, :time, default: 60
desc 'The timeout time when connecting to the server.'
config_param :connect_timeout, :time, default: 30
desc 'The transport protocol to use for heartbeats.(udp,tcp,none)'
config_param :heartbeat_type, default: :udp do |val|
case val.downcase
Expand Down Expand Up @@ -281,7 +283,8 @@ def forward_header

#FORWARD_TCP_HEARTBEAT_DATA = FORWARD_HEADER + ''.to_msgpack + [].to_msgpack
def send_heartbeat_tcp(node)
sock = connect(node)
#TODO should we use heartbeat_interval?
sock = connect(node, @connect_timeout.to_i)
begin
opt = [1, @send_timeout.to_i].pack('I!I!') # { int l_onoff; int l_linger; }
sock.setsockopt(Socket::SOL_SOCKET, Socket::SO_LINGER, opt)
Expand All @@ -296,7 +299,7 @@ def send_heartbeat_tcp(node)
end

def send_data(node, tag, chunk)
sock = connect(node)
sock = connect(node, @connect_timeout.to_i)
begin
opt = [1, @send_timeout.to_i].pack('I!I!') # { int l_onoff; int l_linger; }
sock.setsockopt(Socket::SOL_SOCKET, Socket::SO_LINGER, opt)
Expand Down Expand Up @@ -373,9 +376,18 @@ def send_data(node, tag, chunk)
end
end

def connect(node)
def connect(node, timeout)
# TODO unix socket?
TCPSocket.new(node.resolved_host, node.port)
rh = node.resolved_host
#@log.trace "connecting to #{node.host}(#{rh}:#{node.port}) with timeout #{timeout}"
begin
Socket.tcp(rh, node.port, connect_timeout: timeout)
rescue Errno::ETIMEDOUT => e
@log.warn "timed out when connecting to #{node.host}(#{rh}:#{node.port})"
raise e
rescue => e
raise e
end
end

class HeartbeatRequestTimer < Coolio::TimerWatcher
Expand Down

0 comments on commit 0735a41

Please sign in to comment.