Project

General

Profile

Feature #5100 ยป 0001-lib-resolv.rb-Resolv-DNS-implement-configurable-time.patch

RFC/PATCH for Resolv::DNS#timeouts= method - normalperson (Eric Wong), 07/27/2011 08:45 AM

View differences:

lib/resolv.rb
336 336
      @initialized = nil
337 337
    end
338 338

  
339
    # Sets the resolver timeouts.  This may be a single positive number
340
    # or an array of positive numbers representing timeouts in seconds.
341
    # If an array is specified, a DNS request will retry and wait for
342
    # each successive interval in the array until a successful response
343
    # is received.  Specifying +nil+ reverts to the default timeouts:
344
    # [ 5, second = 5 * 2 / nameserver_count, 2 * second, 4 * second ]
345
    #
346
    # Example:
347
    #
348
    #   dns.timeouts = 3
349
    #
350
    def timeouts=(values)
351
      @config.timeouts = values
352
    end
353

  
339 354
    def lazy_initialize # :nodoc:
340 355
      @mutex.synchronize {
341 356
        unless @initialized
......
851 866
        @mutex = Mutex.new
852 867
        @config_info = config_info
853 868
        @initialized = nil
869
        @timeouts = nil
870
      end
871

  
872
      def timeouts=(values)
873
        if values
874
          values = Array(values)
875
          values.each do |t|
876
            Numeric === t or raise ArgumentError, "#{t.inspect} is not numeric"
877
            t > 0.0 or raise Argument, "timeout=#{t} must be postive"
878
          end
879
          @timeouts = values
880
        else
881
          @timeouts = nil
882
        end
854 883
      end
855 884

  
856 885
      def Config.parse_resolv_conf(filename)
......
1013 1042

  
1014 1043
      def resolv(name)
1015 1044
        candidates = generate_candidates(name)
1016
        timeouts = generate_timeouts
1045
        timeouts = @timeouts || generate_timeouts
1017 1046
        begin
1018 1047
          candidates.each {|candidate|
1019 1048
            begin
test/resolv/test_dns.rb
105 105
    }
106 106
  end
107 107

  
108
  def test_query_ipv4_address_timeout
109
    with_udp('127.0.0.1', 0) {|u|
110
      _, port , _, host = u.addr
111
      start = nil
112
      rv = Resolv::DNS.open(:nameserver_port => [[host, port]]) {|dns|
113
        dns.timeouts = 0.1
114
        start = Time.now
115
        dns.getresources("foo.example.org", Resolv::DNS::Resource::IN::A)
116
      }
117
      diff = Time.now - start
118
      assert rv.empty?, "unexpected: #{rv.inspect} (expected empty)"
119
      assert_in_delta 0.1, diff, 0.05
120

  
121
      rv = Resolv::DNS.open(:nameserver_port => [[host, port]]) {|dns|
122
        dns.timeouts = [ 0.1, 0.2 ]
123
        start = Time.now
124
        dns.getresources("foo.example.org", Resolv::DNS::Resource::IN::A)
125
      }
126
      diff = Time.now - start
127
      assert rv.empty?, "unexpected: #{rv.inspect} (expected empty)"
128
      assert_in_delta 0.3, diff, 0.05
129
    }
130
  end
131

  
108 132
  def test_no_server
109 133
    u = UDPSocket.new
110 134
    u.bind("127.0.0.1", 0)