Hello,

currently the built-in dns_get_record function has support for type
SRV, which is very nice.
Yet this supports only part of the DNS SRV RFC (RFC-2782). In
particular, it will return records but won't sort them using the
weight/priority algorithm.

I won't detail the algorithm, but basically the lower priority records
must be contacted first. And server with same priority must be
contacted in a semi-random order using weight as a probability to be
selected first (the larger weight, the larger chance).

For instance, if I check Google's DNS records for their XMPP (IM) service:
-------------------
$ ring _xmpp-client._tcp.gmail.com
gmail.com. has a xmpp-client service record on tcp:
        priority  = 20
        weight = 0
        port = 5222
        target = talk3.l.google.com.
gmail.com. has a xmpp-client service record on tcp:
        priority  = 20
        weight = 0
        port = 5222
        target = talk4.l.google.com.
gmail.com. has a xmpp-client service record on tcp:
        priority  = 5
        weight = 0
        port = 5222
        target = talk.l.google.com.
gmail.com. has a xmpp-client service record on tcp:
        priority  = 20
        weight = 0
        port = 5222
        target = talk1.l.google.com.
gmail.com. has a xmpp-client service record on tcp:
        priority  = 20
        weight = 0
        port = 5222
        target = talk2.l.google.com.
-------------------
We see here that one should always contact first talk.l.google.com and
only if it fails, try the 4 remaining addresses (randomized with equal
chances).
That's like a basic/static load balancer.

Anyway! So a return of dns_get_record would return the records in the
order the DNS server responded (which is good and is what is asked: a
bare DNS query function). I propose an additional function, called for
instance dns_srv_sort which takes the return of dns_get_record, sort
it and return sorted server/port data, ready to be connected to in
this order.

Example:
$response = dns_get_record ('_xmpp-client._tcp.gmail.com', DNS_SRV);
returns:
Array ( [0] => Array ( [host] => _xmpp-client._tcp.gmail.com [type] =>
SRV [pri] => 20 [weight] => 0 [port] => 5222 [target] =>
talk1.l.google.com [class] => IN [ttl] => 9564 ) [1] => Array ( [host]
=> _xmpp-client._tcp.gmail.com [type] => SRV [pri] => 20 [weight] => 0
[port] => 5222 [target] => talk2.l.google.com [class] => IN [ttl] =>
9564 ) [2] => Array ( [host] => _xmpp-client._tcp.gmail.com [type] =>
SRV [pri] => 20 [weight] => 0 [port] => 5222 [target] =>
talk3.l.google.com [class] => IN [ttl] => 9564 ) [3] => Array ( [host]
=> _xmpp-client._tcp.gmail.com [type] => SRV [pri] => 20 [weight] => 0
[port] => 5222 [target] => talk4.l.google.com [class] => IN [ttl] =>
9564 ) [4] => Array ( [host] => _xmpp-client._tcp.gmail.com [type] =>
SRV [pri] => 5 [weight] => 0 [port] => 5222 [target] =>
talk.l.google.com [class] => IN [ttl] => 9564 ) )
$response = dns_srv_sort($response);
returns:
Array ( [0] => Array ( [port] => 5222 [server] => talk.l.google.com )
[1] => Array ( [port] => 5222 [server] => talk4.l.google.com ) [2] =>
Array ( [port] => 5222 [server] => talk3.l.google.com ) [3] => Array (
[port] => 5222 [server] => talk2.l.google.com ) [4] => Array ( [port]
=> 5222 [server] => talk2.l.google.com ) )

So this is a function I have used with success for quite some time.
Feel free to check the sorting algorithm against RFC-2782. I think
that's quite useful to have this function as a core function because
right now, either PHP users will not sort SRV records as imposed by
the RFC, or will each time rewrite the sorting function.

Also I loaded the source repository and I see most functions are
actually developed in C. Is it necessary to do so? If that's a
prerequisite and you are actually interested in this function to be
inserted as a core function, I will make the rewrite.
And finally sorry by advance, I may not be aware of all the right
procedure for proposing a patch (anyway that's not yet a patch, just a
separate file), though the "Using SVN for PHP development" said to
send patch to this ml. I hope that's fine.
Thanks!

Jehan

<<attachment: dns_srv_sort.php>>

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to