Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
def getServer(criteria):
startTime = loopEndTime = gettime()
maxTime = startTime + serverSelectionTimeoutMS/1000
if topology.lastUpdateTime - startTime > heartbeatFrequencyMS/1000:
topology.stale = true
while true:
if topology.stale:
scanReadyTime = topology.lastUpdateTime + minHeartbeatFrequencyMS/1000
if ((not serverSelectionTryOnce) && (scanReadyTime > maxTime)):
throw server selection error with details
sleepTime = scanReadyTime - loopEndTime
if sleepTime > 0:
sleep sleepTime
# rescan must update topology.lastUpdateTime
rescan all servers
if not topology.compatible:
topology.stale = true
throw invalid wire protocol range error with details
servers = all servers in topology matching criteria
if servers is not empty:
in_window = servers within the latency window
return random entry from in_window
else:
topology.stale = true
loopEndTime = gettime();
if serverSelectionTryOnce:
if topology.lastUpdateTime > startTime:
throw server selection error with details
else if loopEndTime > maxTime:
throw server selection error with details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment