Class: Resolver
- Inherits:
-
Object
- Object
- Resolver
- Includes:
- GlTail::Configurable
- Defined in:
- lib/gl_tail/resolver.rb
Instance Attribute Summary collapse
-
#cache ⇒ Object
readonly
Returns the value of attribute cache.
-
#queue ⇒ Object
readonly
Returns the value of attribute queue.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize ⇒ Resolver
constructor
A new instance of Resolver.
- #lookup(ip, element) ⇒ Object
- #start ⇒ Object
Methods included from GlTail::Configurable
#config_rewrite_color, included
Constructor Details
#initialize ⇒ Resolver
Returns a new instance of Resolver.
13 14 15 16 17 18 19 |
# File 'lib/gl_tail/resolver.rb', line 13 def initialize @cache = { } @thread = nil @reverse_ip_lookups = true @reverse_timeout = 1.5 @queue = Queue.new end |
Instance Attribute Details
#cache ⇒ Object (readonly)
Returns the value of attribute cache.
21 22 23 |
# File 'lib/gl_tail/resolver.rb', line 21 def cache @cache end |
#queue ⇒ Object (readonly)
Returns the value of attribute queue.
21 22 23 |
# File 'lib/gl_tail/resolver.rb', line 21 def queue @queue end |
Class Method Details
.instance ⇒ Object
9 10 11 |
# File 'lib/gl_tail/resolver.rb', line 9 def self.instance @@instance ||= Resolver.new end |
.resolv(ip, element) ⇒ Object
63 64 65 |
# File 'lib/gl_tail/resolver.rb', line 63 def self.resolv(ip, element) instance.lookup(ip, element) end |
Instance Method Details
#lookup(ip, element) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/gl_tail/resolver.rb', line 49 def lookup(ip, element) return ip if not @reverse_ip_lookups if name = cache[ip] return name else puts "[Resolver] Pushing #{ip} for lookup" if $DBG > 0 queue.push([ip, element]) end return ip end |
#start ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/gl_tail/resolver.rb', line 23 def start @thread = Thread.new { while @reverse_ip_lookups ip, element = @queue.pop if @cache.include? ip element.name = @cache[ip] else begin timeout(@reverse_timeout.to_f) { puts "[Resolver] Looking for #{ip}" if $DBG > 0 hostname = Resolv.getname(ip) puts "[Resolver] Got #{hostname}[#{ip}]" if $DBG > 0 @cache[ip] = hostname element.name = hostname } rescue Timeout::Error puts "[Resolver] Timeout!" if $DBG > 0 rescue Resolv::ResolvError # No result, don't bother retrying @cache[ip] = ip end end end } end |