Skip to content

Add timeout option #104

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ 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`

#### timeout (Integer)

Specify a timeout in seconds for connecting. Default: `0`

#### buffer_overflow_handler (Proc)

Pass callback for handling buffer overflow with pending data. See "Buffer overflow" section.
Expand Down
7 changes: 6 additions & 1 deletion lib/fluent/logger/fluent_logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
require 'monitor'
require 'logger'
require 'json'
require 'timeout'

module Fluent
module Logger
Expand Down Expand Up @@ -115,6 +116,8 @@ def initialize(tag_prefix = nil, *args)
@wait_writeable = true
@wait_writeable = options[:wait_writeable] if options.key?(:wait_writeable)

@timeout = options[:timeout] || 0

@last_error = {}

begin
Expand Down Expand Up @@ -170,7 +173,9 @@ def create_socket!
if @socket_path
@con = UNIXSocket.new(@socket_path)
else
@con = TCPSocket.new(@host, @port)
@con = Timeout.timeout(@timeout) do
TCPSocket.new(@host, @port)
end
if @tls_options
context = OpenSSL::SSL::SSLContext.new
if @tls_options[:insecure]
Expand Down
10 changes: 10 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,13 @@ def flush(messages)
}
end
end

it ('support 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, timeout: 1)
expect(logger.last_error).to be_a_kind_of(Timeout::Error)
end
}
end