Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,14 @@ Use nonblocking write(`IO#write_nonblock`) instead of normal write(`IO#write`).

If `false`, `Logger#post` raises an error when nonblocking write gets `EAGAIN` (i.e. `use_nonblock` must be `true`, otherwise this will have no effect). Default: `true`

#### connect_timeout (Integer)

Specify timeout in seconds from connecting. This parameter is available with Ruby 3.0 or above. Default: `nil`

#### resolve_timeout (Integer)

Specify timeout in seconds from when the hostname resolution starts. This parameter is available with Ruby 3.0 or above. Default: `nil`

#### buffer_overflow_handler (Proc)

Pass callback for handling buffer overflow with pending data. See "Buffer overflow" section.
Expand Down
13 changes: 12 additions & 1 deletion lib/fluent/logger/fluent_logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ def initialize(tag_prefix = nil, *args)
@wait_writeable = true
@wait_writeable = options[:wait_writeable] if options.key?(:wait_writeable)

@connect_timeout = options[:connect_timeout]
@resolv_timeout = options[:resolv_timeout]

@last_error = {}

begin
Expand Down Expand Up @@ -170,7 +173,11 @@ def create_socket!
if @socket_path
@con = UNIXSocket.new(@socket_path)
else
@con = TCPSocket.new(@host, @port)
if supported_timeout?
@con = TCPSocket.new(@host, @port, connect_timeout: @connect_timeout, resolv_timeout: @resolv_timeout)
else
@con = TCPSocket.new(@host, @port)
end
if @tls_options
context = OpenSSL::SSL::SSLContext.new
if @tls_options[:insecure]
Expand Down Expand Up @@ -378,6 +385,10 @@ def wait_writeable?(e)
true
end
end

def supported_timeout?
@supported_timeout ||= Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('3.0.0')
end
end
end
end
21 changes: 21 additions & 0 deletions spec/fluent_logger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
require 'logger'
require 'stringio'
require 'fluent/logger/fluent_logger/cui'
require 'timeout'

describe Fluent::Logger::FluentLogger do
let(:fluentd) {
Expand Down Expand Up @@ -434,4 +435,24 @@ def flush(messages)
}
end
end

if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('3.0.0')
context "timeout" do
it ('support connect_timeout') {
Timeout::timeout(5) do
# Use invalid IP address to make sure that the connection will timeout.
# (192.0.2.0 is a special IP address that can be used in only documentation. Ref. RFC 5737)
logger = Fluent::Logger::FluentLogger.new(nil, host: '192.0.2.0', port: fluentd.port, connect_timeout: 1)
expect(logger.last_error).to be_a_kind_of(IO::TimeoutError)
end
}
it ('support resolv_timeout') {
expect {
# It just checks that the resolv_timeout option is supported
# because it can't use a stub for the DNS resolution.
Fluent::Logger::FluentLogger.new(nil, host: 'localhost', port: fluentd.port, resolv_timeout: 1)
}.to_not raise_error
}
end
end
end