22require_relative 'client/connection'
33
44module RabbitMQ
5+
6+ # A {Client} holds a connection to a RabbitMQ server and has facilities
7+ # for sending events to and handling received events from that server.
8+ #
9+ # A {Client} is not threadsafe; both the {Client} and any {Channel}s linked
10+ # to it should not be shared between threads. If they are shared without
11+ # appropriate locking mechanisms, the behavior is undefined and might result
12+ # in catastrophic process failures like segmentation faults in the underlying
13+ # C library. A {Client} can be safely used in a multithreaded application by
14+ # only passing control and message data between threads.
15+ #
516 class Client
6- DEFAULT_PROTOCOL_TIMEOUT = 30 # seconds
717
818 # Create a new {Client} instance with the given properties.
919 # There are several ways to convey connection info:
@@ -69,6 +79,7 @@ def destroy
6979 # By default, this has the value of {DEFAULT_PROTOCOL_TIMEOUT}.
7080 # When set, it affects operations like {#fetch_response} and {#run_loop!}.
7181 attr_accessor :protocol_timeout
82+ DEFAULT_PROTOCOL_TIMEOUT = 30 # seconds
7283
7384 def user ; @conn . options . fetch ( :user ) ; end
7485 def password ; @conn . options . fetch ( :password ) ; end
@@ -142,6 +153,7 @@ def on_event(channel_id, method, callable=nil, &block)
142153 # non-exception event received on any channel. Other handlers or
143154 # response fetchings that match the event will still be processed,
144155 # as the block does not consume the event or replace the handlers.
156+ # @return [undefined] assume no value - reserved for future use.
145157 #
146158 def run_loop! ( timeout : protocol_timeout , &block )
147159 timeout = Float ( timeout ) if timeout
@@ -178,13 +190,15 @@ def channel(id=nil)
178190 Channel . new ( self , @conn , id , finalizer )
179191 end
180192
193+ # Open the specified channel.
181194 private def open_channel ( id )
182195 Util . error_check :"opening a new channel" ,
183196 @conn . send_method ( id , :channel_open )
184197
185198 fetch_response ( id , :channel_open_ok )
186199 end
187200
201+ # Re-open the specified channel after unexpected closure.
188202 private def reopen_channel ( id )
189203 Util . error_check :"acknowledging server-initated channel closure" ,
190204 @conn . send_method ( id , :channel_close_ok )
@@ -195,6 +209,7 @@ def channel(id=nil)
195209 fetch_response ( id , :channel_open_ok )
196210 end
197211
212+ # Verify or choose a channel id number that is available for use.
198213 private def allocate_channel ( id = nil )
199214 if id
200215 id = Integer ( id )
@@ -215,19 +230,21 @@ def channel(id=nil)
215230 id
216231 end
217232
233+ # Release the given channel id to be reused later and clear its handlers.
218234 private def release_channel ( id )
219235 @open_channels . delete ( id )
220236 @event_handlers . delete ( id )
221237 @released_channels [ id ] = true
222238 end
223239
240+ # Release all channel ids to be reused later.
224241 private def release_all_channels
225242 @open_channels . clear
226243 @event_handlers . clear
227244 @released_channels . clear
228245 end
229246
230- # Execute the handler for this type of event, if any
247+ # Execute the handler for this type of event, if any.
231248 private def handle_incoming_event ( event )
232249 if ( handlers = @event_handlers [ event . fetch ( :channel ) ] )
233250 if ( handler = ( handlers [ event . fetch ( :method ) ] ) )
@@ -266,6 +283,7 @@ def channel(id=nil)
266283 end
267284 end
268285
286+ # Internal implementation of the {#run_loop!} method.
269287 private def fetch_events ( timeout = protocol_timeout , start = Time . now )
270288 @conn . garbage_collect
271289
@@ -277,6 +295,7 @@ def channel(id=nil)
277295 end
278296 end
279297
298+ # Internal implementation of the {#fetch_response} method.
280299 private def fetch_response_internal ( channel_id , methods , timeout = protocol_timeout , start = Time . now )
281300 methods . each { |method |
282301 found = @incoming_events [ channel_id ] . delete ( method )
0 commit comments