lib/irrc/client.rb in irrc-0.1.0 vs lib/irrc/client.rb in irrc-0.2.0

- old
+ new

@@ -35,32 +35,32 @@ # :protocol - :ipv4, :ipv6 or [:ipv4, :ipv6] # A String or Symbol of protcol name is acceptable. (optional) # # Examples # - # client.query(:jpirr, 'AS-JPNIC', source: :jpirr, protocol: :ipv4) - # client.query(:jpirr, 'AS-JPNIC', source: [:jpirr, :radb]) + # client.query :jpirr, 'AS-JPNIC', source: :jpirr, protocol: :ipv4 + # client.query :jpirr, 'AS-JPNIC', source: [:jpirr, :radb] def query(host, objects, options={}) - raise ArgumentError, 'host required.' unless host + raise ArgumentError, 'host is required.' unless host fqdn = Irrc::Irr.host(host) || host - queues[fqdn] ||= Queue.new Array(objects).map {|object| - queues[fqdn] << Irrc::Query.new(object, options) + queue(fqdn) << Irrc::Query.new(object, options) } end # Public: Run the query threads. # # Returns Raw level Array of Queries. def run done = [] queues.each_with_object([]) {|(fqdn, queue), workers| - @thread_limit.times.map { + @thread_limit.times.map.with_index {|i| workers << Thread.start { - done.push *worker_class(fqdn).new(fqdn, queues[fqdn], &@block).run + Thread.current[:id] = i+1 + done.push *worker_class(fqdn).new(fqdn, queue(fqdn), cache(fqdn), &@block).run(@thread_limit) } } }.each {|t| t.join } done @@ -88,10 +88,22 @@ private def queues - @_queues ||= {} + @queues ||= {} + end + + def queue(fqdn) + queues[fqdn] ||= Queue.new + end + + def caches + @caches ||= {} + end + + def cache(fqdn) + caches[fqdn] ||= {} end def worker_class(fqdn) type = Irrc::Irr.type(fqdn) or raise "Unknown type of IRR for '#{fqdn}'." Module.const_get("Irrc::#{type.capitalize}::Client")