13 require_once dirname(__FILE__).
'/DSN.inc';
40 private $_currentIndex = -1;
51 $this->_pool = Array();
52 foreach ($dsn_arr as $dsn) {
53 $this->_pool[] =
new DSN($dsn);
70 if (is_null($index)) {
71 $index = $this->_currentIndex;
74 if (isset($this->_pool[$index])) {
75 $this->_pool[$index]->setFailure(TRUE);
92 if (!isset($this->_pool[$index])) {
97 $this->_currentIndex = $index;
104 return $this->_pool[$index]->getDSN();
119 $dsn_weights = $this->_getActiveDSNWeights();
121 $weight_index = $this->_getWeightedRandomIndex($dsn_weights[
'weights']);
123 $index = $weight_index == -1 ? -1 : $dsn_weights[
'indexes'][$weight_index];
136 private function _getActiveDSNWeights()
140 foreach ($this->_pool as $index => $dsn) {
141 if ($dsn->getFailure() !== TRUE) {
142 $weights[] = $dsn->getWeight();
147 return Array(
'weights' => $weights,
'indexes' => $indexes);
163 private function _getWeightedRandomIndex($weights)
165 switch (count($weights)) {
175 $total_weights = array_sum($weights);
176 $random_number = mt_rand(1, $total_weights);
178 foreach ($weights as $index => $weight) {
180 if ($n >= $random_number) {