Class: Dnsruby::ResolverRuby
- Inherits:
-
Object
- Object
- Dnsruby::ResolverRuby
- Defined in:
- lib/Dnsruby/Resolver.rb
Overview
This class implements the I/O using pure Ruby, with no dependencies.
Instance Method Summary collapse
-
#close ⇒ Object
Close the Resolver.
-
#generate_timeouts ⇒ Object
:nodoc: all.
-
#handle_error_response(select_queue, query_id, error, response) ⇒ Object
:nodoc: all.
-
#handle_queue_event(queue, id) ⇒ Object
This method is called by the SelectThread (in the select thread) when the queue has a new item on it.
-
#handle_response(select_queue, query_id, response) ⇒ Object
:nodoc: all.
-
#initialize(parent) ⇒ ResolverRuby
constructor
:nodoc: all.
-
#reset_attributes ⇒ Object
:nodoc: all.
-
#send_async(*args) ⇒ Object
msg, client_queue, client_query_id=nil).
-
#send_result_and_close(client_queue, client_query_id, select_queue, msg, error) ⇒ Object
MUST BE CALLED IN A SYNCHRONIZED BLOCK! .
-
#tick ⇒ Object
This method is called ten times a second from the select loop, in the select thread.
Constructor Details
#initialize(parent) ⇒ ResolverRuby
:nodoc: all
722 723 724 725 |
# File 'lib/Dnsruby/Resolver.rb', line 722 def initialize(parent) reset_attributes @parent=parent end |
Instance Method Details
#close ⇒ Object
Close the Resolver. Unfinished queries are terminated with OtherResolvError.
807 808 809 810 811 812 813 814 |
# File 'lib/Dnsruby/Resolver.rb', line 807 def close @mutex.synchronize { @query_list.each do |client_query_id, values| msg, client_queue, q, outstanding = values send_result_and_close(client_queue, client_query_id, q, nil, OtherResolvError.new("Resolver closing!")) end } end |
#generate_timeouts ⇒ Object
:nodoc: all
795 796 797 798 799 800 801 802 803 804 |
# File 'lib/Dnsruby/Resolver.rb', line 795 def generate_timeouts() #:nodoc: all # Create the timeouts for the query from the retry_times and retry_delay attributes. # These are created at the same time in case the parameters change during the life of the query. # # These should be absolute, rather than relative # The first value should be Time.now[ time_now = Time.now timeouts=@parent.generate_timeouts(time_now) return timeouts end |
#handle_error_response(select_queue, query_id, error, response) ⇒ Object
:nodoc: all
921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 |
# File 'lib/Dnsruby/Resolver.rb', line 921 def handle_error_response(select_queue, query_id, error, response) #:nodoc: all #Handle an error @mutex.synchronize{ TheLog.debug("handling error #{error.class}, #{error}") # Check what sort of error it was : resolver, msg, client_query_id, retry_count = query_id msg, client_queue, select_queue, outstanding = @query_list[client_query_id] if (error.kind_of?(ResolvTimeout)) # - if it was a timeout, then check which number it was, and how many retries are expected on that server # - if it was the last retry, on the last server, then return a timeout to the client (and clean up) # - otherwise, continue # Do we have any more packets to send to this resolver? timeouts = @timeouts[client_query_id] if (outstanding.empty? && timeouts[1].values.empty?) TheLog.debug("Sending timeout to client") send_result_and_close(client_queue, client_query_id, select_queue, response, error) end elsif (error.kind_of?NXDomain) # - if it was an NXDomain, then return that to the client, and stop all new queries (and clean up) send_result_and_close(client_queue, client_query_id, select_queue, response, error) else # - if it was any other error, then remove that server from the list for that query # If a Too Many Open Files error, then don't remove, but let retry work. timeouts = @timeouts[client_query_id] if (!(error.to_s=~/Errno::EMFILE/)) TheLog.debug("Removing #{resolver.server} from resolver list for this query") timeouts[1].each do |key, value| res = value[0] if (res == resolver) timeouts[1].delete(key) end end else TheLog.debug("NOT Removing #{resolver.server} due to Errno::EMFILE") end # - if it was the last server, then return an error to the client (and clean up) if (outstanding.empty? && timeouts[1].values.empty?) # if (outstanding.empty?) TheLog.debug("Sending error to client") send_result_and_close(client_queue, client_query_id, select_queue, response, error) end end #@TODO@ If we're still sending packets for this query, but none are outstanding, then #jumpstart the next query? } end |
#handle_queue_event(queue, id) ⇒ Object
This method is called by the SelectThread (in the select thread) when the queue has a new item on it. The queue interface is used to separate producer/consumer threads, but we’re using it here in one thread. It’s probably a good idea to create a new “worker thread” to take items from the select thread queue and call this method in the worker thread.
Time to process a new queue event.
879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 |
# File 'lib/Dnsruby/Resolver.rb', line 879 def handle_queue_event(queue, id) #:nodoc: all # If we get a callback for an ID we don't know about, don't worry - # just ignore it. It may be for a query we've already completed. # # So, get the next response from the queue (presuming there is one!) # # @TODO@ Tick could poll the queue and then call this method if needed - no need for observer interface. # @TODO@ Currently, tick and handle_queue_event called from select_thread - could have thread chuck events in to tick_queue. But then, clients would have to call in on other thread! # if (queue.empty?) TheLog.fatal("Queue empty in handle_queue_event!") raise RuntimeError.new("Severe internal error - Queue empty in handle_queue_event") end event_id, response, error = queue.pop # We should remove this packet from the list of outstanding packets for this query resolver, msg, client_query_id, retry_count = id if (id != event_id) TheLog.error("Serious internal error!! #{id} expected, #{event_id} received") raise RuntimeError.new("Serious internal error!! #{id} expected, #{event_id} received") end @mutex.synchronize{ if (@query_list[client_query_id]==nil) TheLog.debug("Ignoring response for dead query") return end msg, client_queue, select_queue, outstanding = @query_list[client_query_id] if (!outstanding.include?id) TheLog.error("Query id not on outstanding list! #{outstanding.length} items. #{id} not on #{outstanding}") raise RuntimeError.new("Query id not on outstanding!") end outstanding.delete(id) } # if (event.kind_of?(Exception)) if (error != nil) handle_error_response(queue, event_id, error, response) else # if (event.kind_of?(Message)) handle_response(queue, event_id, response) # else # TheLog.error("Random object #{event.class} returned through queue to Resolver") end end |
#handle_response(select_queue, query_id, response) ⇒ Object
:nodoc: all
968 969 970 971 972 973 974 975 976 977 978 979 980 |
# File 'lib/Dnsruby/Resolver.rb', line 968 def handle_response(select_queue, query_id, response) #:nodoc: all # Handle a good response TheLog.debug("Handling good response") resolver, msg, client_query_id, retry_count = query_id @mutex.synchronize{ query, client_queue, s_queue, outstanding = @query_list[client_query_id] if (s_queue != select_queue) TheLog.error("Serious internal error : expected select queue #{s_queue}, got #{select_queue}") raise RuntimeError.new("Serious internal error : expected select queue #{s_queue}, got #{select_queue}") end send_result_and_close(client_queue, client_query_id, select_queue, response, nil) } end |
#reset_attributes ⇒ Object
:nodoc: all
726 727 728 729 730 731 |
# File 'lib/Dnsruby/Resolver.rb', line 726 def reset_attributes #:nodoc: all # data structures @mutex=Mutex.new @query_list = {} @timeouts = {} end |
#send_async(*args) ⇒ Object
msg, client_queue, client_query_id=nil)
732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 |
# File 'lib/Dnsruby/Resolver.rb', line 732 def send_async(*args) # msg, client_queue, client_query_id=nil) msg=args[0] client_queue=nil client_query_id=nil client_queue=args[1] if (args.length > 2) client_query_id = args[2] end # This is the whole point of the Resolver class. # We want to use multiple SingleResolvers to run a query. # So we kick off a system with select_thread where we send # a query with a queue, but log ourselves as observers for that # queue. When a new response is pushed on to the queue, then the # select thread will call this class' handler method IN THAT THREAD. # When the final response is known, this class then sticks it in # to the client queue. q = Queue.new if (client_query_id==nil) client_query_id = Time.now + rand(10000) end if (!client_queue.kind_of?Queue) TheLog.error("Wrong type for client_queue in Resolver#send_async") client_queue.push([client_query_id, ArgumentError.new("Wrong type of client_queue passed to Dnsruby::Resolver#send_async - should have been Queue, was #{client_queue.class}")]) return end if (!msg.kind_of?Message) TheLog.error("Wrong type for msg in Resolver#send_async") client_queue.push([client_query_id, ArgumentError.new("Wrong type of msg passed to Dnsruby::Resolver#send_async - should have been Message, was #{msg.class}")]) return end tick_needed=false # add to our data structures @mutex.synchronize{ tick_needed = true if @query_list.empty? if (@query_list.has_key?client_query_id) TheLog.error("Duplicate query id requested (#{client_query_id}") client_queue.push([client_query_id, ArgumentError.new("Client query ID already in use")]) return end outstanding = [] @query_list[client_query_id]=[msg, client_queue, q, outstanding] query_timeout = Time.now+@parent.query_timeout if (@parent.query_timeout == 0) query_timeout = Time.now+31536000 # a year from now end @timeouts[client_query_id]=[query_timeout, generate_timeouts()] } # Now do querying stuff using SingleResolver # All this will be handled by the tick method (if we have 0 as the first timeout) st = SelectThread.instance st.add_observer(q, self) tick if tick_needed return client_query_id end |
#send_result_and_close(client_queue, client_query_id, select_queue, msg, error) ⇒ Object
MUST BE CALLED IN A SYNCHRONIZED BLOCK!
Send the result back to the client, and close the socket for that query by removing the query from the select thread.
820 821 822 823 824 825 826 827 828 829 830 831 |
# File 'lib/Dnsruby/Resolver.rb', line 820 def send_result_and_close(client_queue, client_query_id, select_queue, msg, error) #:nodoc: all # We might still get some callbacks, which we should ignore st = SelectThread.instance st.remove_observer(select_queue, self) # @mutex.synchronize{ # Remove the query from all of the data structures @timeouts.delete(client_query_id) @query_list.delete(client_query_id) # } # Return the response to the client client_queue.push([client_query_id, msg, error]) end |
#tick ⇒ Object
This method is called ten times a second from the select loop, in the select thread. It should arguably be called from another worker thread… Each tick, we check if any timeouts have occurred. If so, we take the appropriate action : Return a timeout to the client, or send a new query
837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 |
# File 'lib/Dnsruby/Resolver.rb', line 837 def tick #:nodoc: all # Handle the tick # Do we have any retries due to be sent yet? @mutex.synchronize{ time_now = Time.now @timeouts.keys.each do |client_query_id| msg, client_queue, select_queue, outstanding = @query_list[client_query_id] query_timeout, timeouts = @timeouts[client_query_id] if (query_timeout < Time.now) #Time the query out send_result_and_close(client_queue, client_query_id, select_queue, nil, ResolvTimeout.new("Query timed out")) next end timeouts_done = [] timeouts.keys.sort.each do |timeout| if (timeout < time_now) # Send the next query res, retry_count = timeouts[timeout] id = [res, msg, client_query_id, retry_count] TheLog.debug("Sending msg to #{res.server}") # We should keep a list of the queries which are outstanding outstanding.push(id) timeouts_done.push(timeout) timeouts.delete(timeout) res.send_async(msg, select_queue, id) else break end end timeouts_done.each do |t| timeouts.delete(t) end end } end |