Skip to content

Instantly share code, notes, and snippets.

@dagolden
Last active August 29, 2015 14:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dagolden/af74fd30576b7b8f601c to your computer and use it in GitHub Desktop.
Save dagolden/af74fd30576b7b8f601c to your computer and use it in GitHub Desktop.
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