Skip to content

Commit

Permalink
Make candidates() accept the full read preference, not just the mode
Browse files Browse the repository at this point in the history
  • Loading branch information
thekid committed Aug 19, 2023
1 parent 99d65a5 commit 850cc4c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/main/php/com/mongodb/io/Commands.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ private function __construct($proto, $conn) {
/** Creates an instance for reading */
public static function reading(Protocol $proto): self {
return new self($proto, $proto->establish(
$proto->candidates($proto->readPreference['mode']),
$proto->candidates($proto->readPreference),
'reading with '.$proto->readPreference['mode']
));
}
Expand Down
14 changes: 7 additions & 7 deletions src/main/php/com/mongodb/io/Protocol.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,20 +141,20 @@ public function connect() {
*
* @see https://github.com/mongodb/specifications/blob/master/source/server-selection/server-selection.rst#read-preference
* @see https://docs.mongodb.com/manual/core/read-preference-mechanics/
* @param string $rp
* @param [:var] $rp
* @return string[]
* @throws lang.IllegalArgumentException
*/
public function candidates($rp) {
if ('primary' === $rp) {
if ('primary' === $rp['mode']) {
return [$this->nodes['primary']];
} else if ('secondary' === $rp) {
} else if ('secondary' === $rp['mode']) {
return $this->nodes['secondary'];
} else if ('primaryPreferred' === $rp) {
} else if ('primaryPreferred' === $rp['mode']) {
return array_merge([$this->nodes['primary']], $this->nodes['secondary']);
} else if ('secondaryPreferred' === $rp) {
} else if ('secondaryPreferred' === $rp['mode']) {
return array_merge($this->nodes['secondary'], [$this->nodes['primary']]);
} else if ('nearest' === $rp) { // Prefer to stay on already open connections
} else if ('nearest' === $rp['mode']) { // Prefer to stay on already open connections
$connected= null;
foreach ($this->conn as $id => $conn) {
if (null === $conn->server) continue;
Expand Down Expand Up @@ -229,7 +229,7 @@ public function read(array $options, $sections) {
}

$rp= $sections['$readPreference'] ?? $this->readPreference;
return $this->establish($this->candidates($rp['mode']), 'reading with '.$rp['mode'])
return $this->establish($this->candidates($rp), 'reading with '.$rp['mode'])
->message($sections, $rp)
;
}
Expand Down

0 comments on commit 850cc4c

Please sign in to comment.