* @param topology - Topology to update. * @param incomingServerDescription - New server description.
(topology: Topology, incomingServerDescription?: ServerDescription)
| 860 | * @param incomingServerDescription - New server description. |
| 861 | */ |
| 862 | function updateServers(topology: Topology, incomingServerDescription?: ServerDescription) { |
| 863 | // update the internal server's description |
| 864 | if (incomingServerDescription && topology.s.servers.has(incomingServerDescription.address)) { |
| 865 | const server = topology.s.servers.get(incomingServerDescription.address); |
| 866 | if (server) { |
| 867 | server.s.description = incomingServerDescription; |
| 868 | if ( |
| 869 | incomingServerDescription.error instanceof MongoError && |
| 870 | incomingServerDescription.error.hasErrorLabel(MongoErrorLabel.ResetPool) |
| 871 | ) { |
| 872 | const interruptInUseConnections = incomingServerDescription.error.hasErrorLabel( |
| 873 | MongoErrorLabel.InterruptInUseConnections |
| 874 | ); |
| 875 | |
| 876 | server.pool.clear({ interruptInUseConnections }); |
| 877 | } else if (incomingServerDescription.error == null) { |
| 878 | const newTopologyType = topology.s.description.type; |
| 879 | const shouldMarkPoolReady = |
| 880 | incomingServerDescription.isDataBearing || |
| 881 | (incomingServerDescription.type !== ServerType.Unknown && |
| 882 | newTopologyType === TopologyType.Single); |
| 883 | if (shouldMarkPoolReady) { |
| 884 | server.pool.ready(); |
| 885 | } |
| 886 | } |
| 887 | } |
| 888 | } |
| 889 | |
| 890 | // add new servers for all descriptions we currently don't know about locally |
| 891 | for (const serverDescription of topology.description.servers.values()) { |
| 892 | if (!topology.s.servers.has(serverDescription.address)) { |
| 893 | const server = createAndConnectServer(topology, serverDescription); |
| 894 | topology.s.servers.set(serverDescription.address, server); |
| 895 | } |
| 896 | } |
| 897 | |
| 898 | // for all servers no longer known, remove their descriptions and destroy their instances |
| 899 | for (const entry of topology.s.servers) { |
| 900 | const serverAddress = entry[0]; |
| 901 | if (topology.description.hasServer(serverAddress)) { |
| 902 | continue; |
| 903 | } |
| 904 | |
| 905 | if (!topology.s.servers.has(serverAddress)) { |
| 906 | continue; |
| 907 | } |
| 908 | |
| 909 | const server = topology.s.servers.get(serverAddress); |
| 910 | topology.s.servers.delete(serverAddress); |
| 911 | |
| 912 | // prepare server for garbage collection |
| 913 | if (server) { |
| 914 | closeServer(server, topology); |
| 915 | } |
| 916 | } |
| 917 | } |
| 918 | |
| 919 | function drainWaitQueue(queue: List<ServerSelectionRequest>, drainError: MongoDriverError) { |
no test coverage detected