Skip to content

Commit

Permalink
Set timeout for IDLE responses
Browse files Browse the repository at this point in the history
Fixes #14
  • Loading branch information
shugo committed Mar 16, 2021
1 parent c04bf8f commit 39d39ff
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions lib/net/imap.rb
Expand Up @@ -229,6 +229,9 @@ class IMAP < Protocol
# it raises a Net::OpenTimeout exception. The default value is 30 seconds.
attr_reader :open_timeout

# Seconds to wait until an IDLE response is received.
attr_reader :idle_response_timeout

# The thread to receive exceptions.
attr_accessor :client_thread

Expand Down Expand Up @@ -1056,7 +1059,7 @@ def idle(timeout = nil, &response_handler)
unless @receiver_thread_terminating
remove_response_handler(response_handler)
put_string("DONE#{CRLF}")
response = get_tagged_response(tag, "IDLE")
response = get_tagged_response(tag, "IDLE", @idle_response_timeout)
end
end
end
Expand Down Expand Up @@ -1142,6 +1145,7 @@ def self.format_datetime(time)
# If options[:ssl] is a hash, it's passed to
# OpenSSL::SSL::SSLContext#set_params as parameters.
# open_timeout:: Seconds to wait until a connection is opened
# idle_response_timeout:: Seconds to wait until an IDLE response is received
#
# The most common errors are:
#
Expand Down Expand Up @@ -1171,6 +1175,7 @@ def initialize(host, port_or_options = {},
@tag_prefix = "RUBY"
@tagno = 0
@open_timeout = options[:open_timeout] || 30
@idle_response_timeout = options[:idle_response_timeout] || 5
@parser = ResponseParser.new
@sock = tcp_socket(@host, @port)
begin
Expand Down Expand Up @@ -1294,10 +1299,19 @@ def receive_responses
end
end

def get_tagged_response(tag, cmd)
def get_tagged_response(tag, cmd, timeout = nil)
if timeout
deadline = Time.now + timeout
end
until @tagged_responses.key?(tag)
raise @exception if @exception
@tagged_response_arrival.wait
if timeout
timeout = deadline - Time.now
if timeout <= 0
return nil
end
end
@tagged_response_arrival.wait(timeout)
end
resp = @tagged_responses.delete(tag)
case resp.name
Expand Down

0 comments on commit 39d39ff

Please sign in to comment.