-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(read-preference): unify means of read preference resolution #1738
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -496,60 +496,57 @@ function applyWriteConcern(target, sources, options) { | |
} | ||
|
||
/** | ||
* Ensures provided read preference is properly converted into an object | ||
* @param {(ReadPreference|string|object)} readPreference the user provided read preference | ||
* @return {ReadPreference} | ||
* Resolves a read preference based on well-defined inheritance rules. This method will not only | ||
* determine the read preference (if there is one), but will also ensure the returned value is a | ||
* properly constructed instance of `ReadPreference`. | ||
* | ||
* @param {Object} options The options passed into the method, potentially containing a readPreference | ||
* @param {Object} sources Sources where we can inherit default read preference from | ||
* @returns {ReadPreference} the resolved read preference, if found | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "the" should be capitalized |
||
*/ | ||
function convertReadPreference(readPreference) { | ||
if (readPreference) { | ||
if (typeof readPreference === 'string') { | ||
return new ReadPreference(readPreference); | ||
} else if ( | ||
readPreference && | ||
!(readPreference instanceof ReadPreference) && | ||
typeof readPreference === 'object' | ||
) { | ||
const mode = readPreference.mode || readPreference.preference; | ||
if (mode && typeof mode === 'string') { | ||
return new ReadPreference(mode, readPreference.tags, { | ||
maxStalenessSeconds: readPreference.maxStalenessSeconds | ||
}); | ||
} | ||
} else if (!(readPreference instanceof ReadPreference)) { | ||
throw new TypeError('Invalid read preference: ' + readPreference); | ||
} | ||
} | ||
function resolveReadPreference(options, sources) { | ||
options = options || {}; | ||
sources = sources || {}; | ||
|
||
return readPreference; | ||
} | ||
const db = sources.db; | ||
const coll = sources.collection; | ||
const defaultReadPreference = sources.default; | ||
|
||
// Figure out the read preference | ||
function getReadPreference(coll, options, db) { | ||
let r = null; | ||
let readPreference; | ||
if (options.readPreference) { | ||
r = options.readPreference; | ||
} else if (coll.s.readPreference) { | ||
r = coll.s.readPreference; | ||
} else if (db.s.readPreference) { | ||
r = db.s.readPreference; | ||
} else { | ||
return options; | ||
} | ||
|
||
if (typeof r === 'string') { | ||
options.readPreference = new ReadPreference(r); | ||
} else if (r && !(r instanceof ReadPreference) && typeof r === 'object') { | ||
const mode = r.mode || r.preference; | ||
readPreference = options.readPreference; | ||
} else if (coll && coll.s.readPreference) { | ||
readPreference = coll.s.readPreference; | ||
} else if (db && db.s.readPreference) { | ||
readPreference = db.s.readPreference; | ||
} else if (defaultReadPreference) { | ||
readPreference = defaultReadPreference; | ||
} | ||
|
||
// do we even have a read preference? | ||
if (readPreference == null) { | ||
return null; | ||
} | ||
|
||
// now attempt to convert the read preference if necessary | ||
if (typeof readPreference === 'string') { | ||
readPreference = new ReadPreference(readPreference); | ||
} else if ( | ||
readPreference && | ||
!(readPreference instanceof ReadPreference) && | ||
typeof readPreference === 'object' | ||
) { | ||
const mode = readPreference.mode || readPreference.preference; | ||
if (mode && typeof mode === 'string') { | ||
options.readPreference = new ReadPreference(mode, r.tags, { | ||
maxStalenessSeconds: r.maxStalenessSeconds | ||
readPreference = new ReadPreference(mode, readPreference.tags, { | ||
maxStalenessSeconds: readPreference.maxStalenessSeconds | ||
}); | ||
} | ||
} else if (!(r instanceof ReadPreference)) { | ||
throw new TypeError('Invalid read preference: ' + r); | ||
} else if (!(readPreference instanceof ReadPreference)) { | ||
throw new TypeError('Invalid read preference: ' + readPreference); | ||
} | ||
|
||
return options; | ||
return readPreference; | ||
} | ||
|
||
/** | ||
|
@@ -625,9 +622,8 @@ module.exports = { | |
translateReadPreference, | ||
executeOperation, | ||
applyWriteConcern, | ||
convertReadPreference, | ||
resolveReadPreference, | ||
isPromiseLike, | ||
getReadPreference, | ||
decorateWithCollation, | ||
decorateWithReadConcern | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"Sources from which we can inherit default read preference"