( readPreference: ReadPreference, topologyDescription: TopologyDescription, servers: ServerDescription[], deprioritized: DeprioritizedServers )
| 283 | } |
| 284 | |
| 285 | function secondarySelector( |
| 286 | readPreference: ReadPreference, |
| 287 | topologyDescription: TopologyDescription, |
| 288 | servers: ServerDescription[], |
| 289 | deprioritized: DeprioritizedServers |
| 290 | ) { |
| 291 | const mode = readPreference.mode; |
| 292 | switch (mode) { |
| 293 | case 'primary': |
| 294 | // Note: no need to filter for deprioritized servers. A replica set has only one primary; that means that |
| 295 | // we are in one of two scenarios: |
| 296 | // 1. deprioritized servers is empty - return the primary. |
| 297 | // 2. deprioritized servers contains the primary - return the primary. |
| 298 | return servers.filter(primaryFilter); |
| 299 | case 'primaryPreferred': { |
| 300 | const primary = servers.filter(primaryFilter); |
| 301 | |
| 302 | // If there is a primary and it is not deprioritized, use the primary. Otherwise, |
| 303 | // check for secondaries. |
| 304 | const eligiblePrimary = primary.filter(isDeprioritizedFactory(deprioritized)); |
| 305 | if (eligiblePrimary.length) { |
| 306 | return eligiblePrimary; |
| 307 | } |
| 308 | |
| 309 | // If we make it here, we either have: |
| 310 | // 1. a deprioritized primary |
| 311 | // 2. no eligible primary |
| 312 | // secondaries take precedence of deprioritized primaries. |
| 313 | const secondaries = tagSetReducer( |
| 314 | readPreference, |
| 315 | maxStalenessReducer(readPreference, topologyDescription, servers.filter(secondaryFilter)) |
| 316 | ); |
| 317 | |
| 318 | const eligibleSecondaries = secondaries.filter(isDeprioritizedFactory(deprioritized)); |
| 319 | if (eligibleSecondaries.length) { |
| 320 | return latencyWindowReducer(topologyDescription, eligibleSecondaries); |
| 321 | } |
| 322 | |
| 323 | // if we make it here, we have no primaries or secondaries that not deprioritized. |
| 324 | // prefer the primary (which may not exist, if the topology has no primary). |
| 325 | // otherwise, return the secondaries (which also may not exist, but there is nothing else to check here). |
| 326 | return primary.length ? primary : latencyWindowReducer(topologyDescription, secondaries); |
| 327 | } |
| 328 | case 'nearest': { |
| 329 | const eligible = filterDeprioritized( |
| 330 | tagSetReducer( |
| 331 | readPreference, |
| 332 | maxStalenessReducer(readPreference, topologyDescription, servers.filter(nearestFilter)) |
| 333 | ), |
| 334 | deprioritized |
| 335 | ); |
| 336 | return latencyWindowReducer(topologyDescription, eligible); |
| 337 | } |
| 338 | case 'secondary': |
| 339 | case 'secondaryPreferred': { |
| 340 | const secondaries = tagSetReducer( |
| 341 | readPreference, |
| 342 | maxStalenessReducer(readPreference, topologyDescription, servers.filter(secondaryFilter)) |
no test coverage detected