@@ -160,8 +160,9 @@ module Net
160160 # Use paginated or limited versions of commands whenever possible.
161161 #
162162 # Use #add_response_handler to handle responses after each one is received.
163- # Use #extract_responses, #clear_responses, or #responses (with a block) to
164- # prune responses.
163+ # Use the +response_handlers+ argument to ::new to assign response handlers
164+ # before the receiver thread is started. Use #extract_responses,
165+ # #clear_responses, or #responses (with a block) to prune responses.
165166 #
166167 # == Errors
167168 #
@@ -1996,6 +1997,11 @@ def idle_done
19961997 # end
19971998 # }
19981999 #
2000+ # Response handlers can also be added when the client is created before the
2001+ # receiver thread is started, by the +response_handlers+ argument to ::new.
2002+ # This ensures every server response is handled, including the #greeting.
2003+ #
2004+ # Related: #remove_response_handler, #response_handlers
19992005 def add_response_handler ( handler = nil , &block )
20002006 raise ArgumentError , "two Procs are passed" if handler && block
20012007 @response_handlers . push ( block || handler )
@@ -2031,6 +2037,12 @@ def remove_response_handler(handler)
20312037 # OpenSSL::SSL::SSLContext#set_params as parameters.
20322038 # open_timeout:: Seconds to wait until a connection is opened
20332039 # idle_response_timeout:: Seconds to wait until an IDLE response is received
2040+ # response_handlers:: A list of response handlers to be added before the
2041+ # receiver thread is started. This ensures every server
2042+ # response is handled, including the #greeting. Note
2043+ # that the greeting is handled in the current thread,
2044+ # but all other responses are handled in the receiver
2045+ # thread.
20342046 #
20352047 # The most common errors are:
20362048 #
@@ -2073,6 +2085,7 @@ def initialize(host, port_or_options = {},
20732085 @responses = Hash . new ( [ ] . freeze )
20742086 @tagged_responses = { }
20752087 @response_handlers = [ ]
2088+ options [ :response_handlers ] &.each do |h | add_response_handler ( h ) end
20762089 @tagged_response_arrival = new_cond
20772090 @continued_command_tag = nil
20782091 @continuation_request_arrival = new_cond
@@ -2089,6 +2102,7 @@ def initialize(host, port_or_options = {},
20892102 if @greeting . name == "BYE"
20902103 raise ByeResponseError , @greeting
20912104 end
2105+ @response_handlers . each do |handler | handler . call ( @greeting ) end
20922106
20932107 @client_thread = Thread . current
20942108 @receiver_thread = Thread . start {
0 commit comments