Plugin Directory

Changeset 427261


Ignore:
Timestamp:
08/22/2011 06:49:28 PM (15 years ago)
Author:
Jehan
Message:
  • SRV DNS request is now saved in cache and reused.

I use the minimum ttl of all SRV records, minored in a week (as of RFC-1035,
too big a ttl should not be used. A week is the example in the RFC. I used this
for my implementation).

  • Small code improvement in dns results.

As I am not sure of the result in absence of SRV records (FALSE as an error
or empty array?), I test emptiness of result instead as boolean.

Location:
xmpp-auth/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • xmpp-auth/trunk/dns.php

    r426617 r427261  
    3737    $targets = array();
    3838    $recs = array ();
     39    // 1 week in second. Too long ttls should be avoided.
     40    // See RFC-1035, section 7.3 (Processing Responses).
     41    $ttl = 604800;
    3942    foreach ($records as $rr)
    4043    {
     
    4649        $rec['port'] = $rr['port'];
    4750        $rec['weight'] = $rr['weight'];
    48         $rec['ttl'] = $rr['ttl'];
     51        //$rec['ttl'] = $rr['ttl'];
    4952        $recs[$rr['pri']][] = $rec;
     53        // I want to use a single ttl for all records, which is the minimum ttl.
     54        // I indeed consider that if one record terminate, I should probably check the whole query again.
     55        $ttl = min($ttl, $rr['ttl']);
    5056    }
     57    $ttl += time(); // Convert the ttl from relative to absolute for caching.
    5158
    5259    ksort($recs);
     
    5562        if (count($rrs) == 1)
    5663            $targets[] = array(
    57                     'port' => $rrs[0]['port'],
    58                     'server' => $rrs[0]['target']);
     64                'port' => $rrs[0]['port'],
     65                'server' => $rrs[0]['target'],
     66                'ttl' => $ttl);
    5967        else
    6068        {
     
    8997                                'port' => $rr['port'],
    9098                                'server' => $rr['target'],
    91                                 'ttl' => $rr['ttl']);
     99                                'ttl' => $ttl);
    92100                        unset($rrs[$k]);
    93101                        break;
  • xmpp-auth/trunk/xmpp_stream.php

    r427193 r427261  
    8080        if (empty($port) && empty($server))
    8181        {
    82             if (class_exists("NET_DNS_Resolver"))
     82            $cache = get_option('imauth_dns_srv_cache');
     83            if (isset($cache[$domain]) && $cache[$domain][0]['ttl'] >= time())
     84            {
     85                // I make the check of the domain, because the domain may have changed.
     86                // Also I know (from the way I built the cache) that if it exists, it will always have at least one value,
     87                // at index 0, and that if there are several values, the 'ttl' will be the same for all of them.
     88                $this->targets = $cache[$domain];
     89            }
     90            elseif (class_exists("NET_DNS_Resolver"))
    8391            {
    8492                $resolver = new Net_DNS_Resolver();
    8593                $response = $resolver->query('_xmpp-client._tcp.' . $this->domain, 'SRV');
    86                 if ($response)
     94                if (!empty($response))
    8795                {
    8896                    $recs = array ();
     
    102110                    }
    103111                    $this->targets = dns_srv_sort($recs);
     112                    $cache = array($domain => $this->targets);
     113                    update_option('imauth_dns_srv_cache', $cache);
    104114                }
    105115                else
     
    111121            {
    112122                $response = dns_get_record('_xmpp-client._tcp.' . $this->domain, DNS_SRV);
    113                 if ($response)
     123                if (!empty($response))
     124                {
    114125                    $this->targets = dns_srv_sort($response);
     126                    $cache = array($domain => $this->targets);
     127                    update_option('imauth_dns_srv_cache', $cache);
     128                }
    115129                else
    116130                    $this->targets[] = array(
Note: See TracChangeset for help on using the changeset viewer.