Skip to content

Commit 39d39ff

Browse files
committed
Set timeout for IDLE responses
Fixes #14
1 parent c04bf8f commit 39d39ff

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

Diff for: lib/net/imap.rb

+17-3
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,9 @@ class IMAP < Protocol
229229
# it raises a Net::OpenTimeout exception. The default value is 30 seconds.
230230
attr_reader :open_timeout
231231

232+
# Seconds to wait until an IDLE response is received.
233+
attr_reader :idle_response_timeout
234+
232235
# The thread to receive exceptions.
233236
attr_accessor :client_thread
234237

@@ -1056,7 +1059,7 @@ def idle(timeout = nil, &response_handler)
10561059
unless @receiver_thread_terminating
10571060
remove_response_handler(response_handler)
10581061
put_string("DONE#{CRLF}")
1059-
response = get_tagged_response(tag, "IDLE")
1062+
response = get_tagged_response(tag, "IDLE", @idle_response_timeout)
10601063
end
10611064
end
10621065
end
@@ -1142,6 +1145,7 @@ def self.format_datetime(time)
11421145
# If options[:ssl] is a hash, it's passed to
11431146
# OpenSSL::SSL::SSLContext#set_params as parameters.
11441147
# open_timeout:: Seconds to wait until a connection is opened
1148+
# idle_response_timeout:: Seconds to wait until an IDLE response is received
11451149
#
11461150
# The most common errors are:
11471151
#
@@ -1171,6 +1175,7 @@ def initialize(host, port_or_options = {},
11711175
@tag_prefix = "RUBY"
11721176
@tagno = 0
11731177
@open_timeout = options[:open_timeout] || 30
1178+
@idle_response_timeout = options[:idle_response_timeout] || 5
11741179
@parser = ResponseParser.new
11751180
@sock = tcp_socket(@host, @port)
11761181
begin
@@ -1294,10 +1299,19 @@ def receive_responses
12941299
end
12951300
end
12961301

1297-
def get_tagged_response(tag, cmd)
1302+
def get_tagged_response(tag, cmd, timeout = nil)
1303+
if timeout
1304+
deadline = Time.now + timeout
1305+
end
12981306
until @tagged_responses.key?(tag)
12991307
raise @exception if @exception
1300-
@tagged_response_arrival.wait
1308+
if timeout
1309+
timeout = deadline - Time.now
1310+
if timeout <= 0
1311+
return nil
1312+
end
1313+
end
1314+
@tagged_response_arrival.wait(timeout)
13011315
end
13021316
resp = @tagged_responses.delete(tag)
13031317
case resp.name

0 commit comments

Comments
 (0)