Skip to content

Commit 9e329d1

Browse files
Watson1978daipom
andauthored
Add timeout option (#104)
Fix #103 --------- Signed-off-by: Shizuo Fujita <[email protected]> Signed-off-by: Daijiro Fukuda <[email protected]> Co-authored-by: Daijiro Fukuda <[email protected]>
1 parent cbdfb65 commit 9e329d1

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@ Use nonblocking write(`IO#write_nonblock`) instead of normal write(`IO#write`).
148148

149149
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`
150150

151+
#### timeout (Integer)
152+
153+
Specify a timeout in seconds for connecting. Default: `0` (Disabled)
154+
151155
#### buffer_overflow_handler (Proc)
152156

153157
Pass callback for handling buffer overflow with pending data. See "Buffer overflow" section.

lib/fluent/logger/fluent_logger.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
require 'monitor'
2323
require 'logger'
2424
require 'json'
25+
require 'timeout'
2526

2627
module Fluent
2728
module Logger
@@ -115,6 +116,8 @@ def initialize(tag_prefix = nil, *args)
115116
@wait_writeable = true
116117
@wait_writeable = options[:wait_writeable] if options.key?(:wait_writeable)
117118

119+
@timeout = options[:timeout] || 0
120+
118121
@last_error = {}
119122

120123
begin
@@ -170,7 +173,9 @@ def create_socket!
170173
if @socket_path
171174
@con = UNIXSocket.new(@socket_path)
172175
else
173-
@con = TCPSocket.new(@host, @port)
176+
@con = Timeout.timeout(@timeout) do
177+
TCPSocket.new(@host, @port)
178+
end
174179
if @tls_options
175180
context = OpenSSL::SSL::SSLContext.new
176181
if @tls_options[:insecure]

spec/fluent_logger_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
require 'logger'
77
require 'stringio'
88
require 'fluent/logger/fluent_logger/cui'
9+
require 'timeout'
910

1011
describe Fluent::Logger::FluentLogger do
1112
let(:fluentd) {
@@ -434,4 +435,13 @@ def flush(messages)
434435
}
435436
end
436437
end
438+
439+
it ('support timeout') {
440+
Timeout::timeout(5) do
441+
# Use invalid IP address to make sure that the connection will timeout.
442+
# (192.0.2.0 is a special IP address that can be used in only documentation. Ref. RFC 5737)
443+
logger = Fluent::Logger::FluentLogger.new(nil, host: '192.0.2.0', port: fluentd.port, timeout: 1)
444+
expect(logger.last_error).to be_a_kind_of(Timeout::Error)
445+
end
446+
}
437447
end

0 commit comments

Comments
 (0)