Unverified Commit 9078a537 authored by Gavin Brown's avatar Gavin Brown
Browse files

update comments

parent 4c267a46
...@@ -23,6 +23,9 @@ use strict; ...@@ -23,6 +23,9 @@ use strict;
$VERSION = '0.10'; $VERSION = '0.10';
#
# default config file location
#
$CFILE = sprintf('/etc/%s/%s.conf', NAME, NAME); $CFILE = sprintf('/etc/%s/%s.conf', NAME, NAME);
# #
...@@ -157,7 +160,6 @@ exit(1); ...@@ -157,7 +160,6 @@ exit(1);
# main loop function # main loop function
# #
sub main_loop { sub main_loop {
# #
# run inside eval to catch fatal errors # run inside eval to catch fatal errors
# #
...@@ -229,15 +231,16 @@ sub main_loop { ...@@ -229,15 +231,16 @@ sub main_loop {
# send a DNS query and return the amount of time it took (or undef if no # send a DNS query and return the amount of time it took (or undef if no
# answer was received within the timeout window) # answer was received within the timeout window)
# #
# we accept multiple arguments since servers may be multi-homed. this # it accepts multiple IP addresses since servers may be multi-homed.
# function should only ever receive IP addresses as arguments, otherwise # this function should only ever receive IP addresses as arguments,
# the RTT will erroneously include the time taken to resolve the name # otherwise the RTT will erroneously include the time taken to resolve
# the name
# #
sub time_query { sub time_query {
my ($family, $proto, @servers) = @_; my ($family, $proto, @ips) = @_;
if (scalar(@servers) < 1) { if (scalar(@ips) < 1) {
warn('received an empty set of nameservers'); warn('received an empty set of IP addresses');
return undef; return undef;
} else { } else {
...@@ -245,16 +248,16 @@ sub time_query { ...@@ -245,16 +248,16 @@ sub time_query {
# construct a resolver # construct a resolver
# #
my $resolver = Net::DNS::Resolver->new( my $resolver = Net::DNS::Resolver->new(
'nameservers' => \@servers, 'nameservers' => \@ips, # IP addresses to query
'retry' => 0, 'retry' => 0, # no retries
'igntc' => 1, 'igntc' => 1, # ignore TC bit on UDP queries so we don't do a TCP query
'usevc' => ('tcp' eq $proto), 'usevc' => ('tcp' eq $proto), # enable/disable TCP
'udp_timeout' => $CONF->{'Timeout'}, 'udp_timeout' => $CONF->{'Timeout'}, # Set timeout
'tcp_timeout' => $CONF->{'Timeout'}, 'tcp_timeout' => $CONF->{'Timeout'}, # Set timeout
'persistent_udp' => 0, 'persistent_udp' => 0, # Disable persistent connections
'persistent_tcp' => 0, 'persistent_tcp' => 0, # Disable persistent connections
'force_v4' => 4 == $family, 'force_v4' => (4 == $family), # Force IPv4
'force_v6' => 6 == $family, 'force_v6' => (6 == $family), # Force IPv6
); );
my $result; my $result;
...@@ -347,12 +350,18 @@ sub update_serverlist { ...@@ -347,12 +350,18 @@ sub update_serverlist {
$GROUPS = {}; $GROUPS = {};
if ($CONF->{'Servers'}) { if ($CONF->{'Servers'}) {
#
# statically defined server list, never refresh # statically defined server list, never refresh
#
$REFRESH = -1; $REFRESH = -1;
@SERVERS = ('ARRAY' eq ref($CONF->{'Servers'}) ? @{$CONF->{'Servers'}} : split(/\s*,\s*/, $CONF->{'Servers'})); @SERVERS = ('ARRAY' eq ref($CONF->{'Servers'}) ? @{$CONF->{'Servers'}} : split(/\s*,\s*/, $CONF->{'Servers'}));
} else { } else {
#
# dynamic server list
#
@SERVERS = (); @SERVERS = ();
my $resolver = Net::DNS::Resolver->new; my $resolver = Net::DNS::Resolver->new;
...@@ -637,8 +646,8 @@ sub initialise_mysql { ...@@ -637,8 +646,8 @@ sub initialise_mysql {
# #
# resolve a server name to IPv4 or IPv6 addresses, using a cached value # resolve a server name to IPv4 or IPv6 addresses, using a cached value
# if possible will attempt to detect literal IPs to avoid doing a DNS # if possible. this function will attempt to detect literal IPs to avoid
# lookup # doing a DNS lookup
# #
sub resolve { sub resolve {
my $name = lc(shift); my $name = lc(shift);
...@@ -648,14 +657,16 @@ sub resolve { ...@@ -648,14 +657,16 @@ sub resolve {
if (($name =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/ && 'A' eq $type) || ($name =~ /^[0-9a-f:]+$/i && 'AAAA' eq $type)) { if (($name =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/ && 'A' eq $type) || ($name =~ /^[0-9a-f:]+$/i && 'AAAA' eq $type)) {
# #
# IP literal, just return it # Literal IP, just return it
# #
return ($name); return ($name);
} else { } else {
if (!defined($CACHE->{$name}->{$type}) || $CACHE->{$name}->{$type}->{'expires'} < time()) { if (!defined($CACHE->{$name}->{$type}) || $CACHE->{$name}->{$type}->{'expires'} < time()) {
my $resolver = Net::DNS::Resolver->new; my $resolver = Net::DNS::Resolver->new;
my $answer = $resolver->query($name.'.', $type); my $answer = $resolver->query($name.'.', $type);
if ($answer) { if ($answer) {
# #
# start with a high value # start with a high value
...@@ -685,18 +696,20 @@ sub resolve { ...@@ -685,18 +696,20 @@ sub resolve {
'expires' => time() + $minttl, 'expires' => time() + $minttl,
} }
} else { } elsif (!defined($CACHE->{$name}->{$type})) {
debug("unable to resolve '%s'", $name); #
# no answer and cache miss
if (!defined($CACHE->{$name}->{$type})) { #
$CACHE->{$name}->{$type} = { $CACHE->{$name}->{$type} = {
'addrs' => [], 'addrs' => [],
'expires' => time(), 'expires' => 0,
}
} }
} }
} }
#
# return whatever's in cache
#
return @{$CACHE->{$name}->{$type}->{'addrs'}}; return @{$CACHE->{$name}->{$type}->{'addrs'}};
} }
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment