MediaWiki REL1_35
DnsSrvDiscoverer.php
Go to the documentation of this file.
1<?php
30 private $domain;
31
35 public function __construct( $domain ) {
36 $this->domain = $domain;
37 }
38
44 public function getServers() {
45 $result = [];
46 foreach ( $this->getDnsRecords() as $record ) {
47 $result[] = [
48 'target' => $record['target'],
49 'port' => $record['port'],
50 'pri' => $record['pri'],
51 'weight' => $record['weight'],
52 ];
53 }
54
55 return $result;
56 }
57
65 public function pickServer( array $servers ) {
66 if ( !$servers ) {
67 return false;
68 }
69
70 $srvsByPrio = [];
71 foreach ( $servers as $server ) {
72 $srvsByPrio[$server['pri']][] = $server;
73 }
74
75 $min = min( array_keys( $srvsByPrio ) );
76 if ( count( $srvsByPrio[$min] ) == 1 ) {
77 return $srvsByPrio[$min][0];
78 } else {
79 // Choose randomly
80 $rand = mt_rand( 0, count( $srvsByPrio[$min] ) - 1 );
81
82 return $srvsByPrio[$min][$rand];
83 }
84 }
85
91 public function removeServer( $server, array $servers ) {
92 foreach ( $servers as $i => $srv ) {
93 if ( $srv['target'] === $server['target'] && $srv['port'] === $server['port'] ) {
94 unset( $servers[$i] );
95 break;
96 }
97 }
98
99 return array_values( $servers );
100 }
101
105 protected function getDnsRecords() {
106 return dns_get_record( $this->domain, DNS_SRV );
107 }
108}
removeServer( $server, array $servers)
pickServer(array $servers)
Pick a server according to the priority fields.
getServers()
Fetch the servers with a DNS SRV request.