+
+#
+# Allocate each origin to a cluster
+#
+sub allocate_clusters
+{
+ my @mappings = sort { compare_mappings($a, $b) } @_;
+
+ # Loop over the mappings, trying to assign each origin to the
+ # nearest cluster, but subject to the request limits
+ while (my $mapping = shift @mappings)
+ {
+ my @group;
+
+ push @group, $mapping;
+
+ while (@mappings && compare_mappings($mapping, $mappings[0]) == 0)
+ {
+ push @group, shift @mappings;
+ }
+
+ for my $mapping (sort compare_requests @group)
+ {
+ my $origin = $mapping->{origin};
+ my $cluster = $mapping->{cluster};
+
+ if (!exists($origin->{cluster}) &&
+ $cluster->{requests_used} + $origin->{requests} <= $cluster->{requests_limit})
+ {
+ $origin->{cluster} = $cluster;
+ $cluster->{requests_used} = $cluster->{requests_used} + $origin->{requests};
+ }
+ }
+ }
+
+ return;
+}
+
+#
+# Compare two mappings to decide which to use
+#
+sub compare_mappings
+{
+ my $a = shift;
+ my $b = shift;
+
+ return $b->{priority} <=> $a->{priority} ||
+ $a->{distance} <=> $b->{distance};
+}
+
+#
+# Compare two mappings to decide which to try first
+#
+sub compare_requests
+{
+ my $a_used = ( $a->{cluster}->{requests_used} * 100.0 ) / ( $a->{cluster}->{requests_limit} * 1.0 );
+ my $b_used = ( $b->{cluster}->{requests_used} * 100.0 ) / ( $b->{cluster}->{requests_limit} * 1.0 );
+
+ return $a_used <=> $b_used;
+}
+
+#
+# Output DNS records for a server
+#
+sub output_server
+{
+ my $zonefile = shift;
+ my $name = shift;
+ my $cluster = shift;
+ my $all = shift;
+
+ while (my($index,$server) = each @{$cluster->{servers}})
+ {
+ if ($all || $server->{status} eq "up")
+ {
+ if ($server->{ipv4})
+ {
+ $zonefile->printf(" A(\"${name}\", \"$server->{ipv4}\", TTL(\"10m\")),\n", $index + 1);
+ }
+
+ if ($server->{ipv6})
+ {
+ $zonefile->printf(" AAAA(\"${name}\", \"$server->{ipv6}\", TTL(\"10m\")),\n", $index + 1);
+ }
+ }
+ }
+
+ return;
+}