FAQ¶
On this page
What Is the Difference Between "connectTimeoutMS", "socketTimeoutMS" and "maxTimeMS"?“connectTimeoutMS”、“socketTimeoutMS”和“maxTimeMS”之间有什么区别?How Can I Prevent the Driver From Hanging During Connection or From Spending Too Long Trying to Reach Unreachable Replica Sets?如何防止驱动程序在连接期间挂起或花费太长时间试图访问无法访问的副本集?Should I Use "socketTimeoutMS" as a Way of Preventing Long-Running Operations From Slowing Down the Server?我应该使用“socketTimeoutMS”来防止长时间运行的操作降低服务器的速度吗?How Can I Prevent Sockets From Timing out Before They Become Active?如何防止套接字在激活之前超时?What Does a Value of "0" mean for "connectTimeoutMS" and "socketTimeoutMS"?值“0”对于“connectTimeoutMS”和“socketTimeoutMS”意味着什么?How Can I Prevent Long-Running Operations From Slowing Down the Server?如何防止长时间运行的操作降低服务器速度?What Does the "keepAlive" Setting Do?“keepAlive”设置的作用是什么?What Can I Do If I'm Experiencing Unexpected Network Behavior?如果遇到意外的网络行为,我该怎么办?What Can I Do If I'm Getting "ECONNRESET" When Calling "client.connect()"?如果调用“client.connect()
”时遇到“ECONNRESET”,该怎么办?How Can I Prevent a Slow Operation From Delaying Other Operations?如何防止缓慢的操作延迟其他操作?How Can I Ensure My Connection String Is Valid for a Replica Set?如何确保我的连接字符串对副本集有效?
Frequently Asked Questions
What Is the Difference Between "connectTimeoutMS", "socketTimeoutMS" and "maxTimeMS"?“connectTimeoutMS”、“socketTimeoutMS”和“maxTimeMS”之间有什么区别?¶
connectTimeoutMS
|
Tip
|
socketTimeoutMS
| socketTimeoutMS |
maxTimeMS
| maxTimeMS
|
To specify the optional settings for your 要指定MongoClient
, declare one or more available settings in the options
object of the constructor as follows:MongoClient
的可选设置,请在构造函数的options
对象中声明一个或多个可用设置,如下所示:
const client = new MongoClient(uri, {
connectTimeoutMS: <integer value>,
socketTimeoutMS: <integer value>
});
To see all the available settings, see the MongoClientOptions API Documentation.要查看所有可用设置,请参阅MongoClient API文档。
How Can I Prevent the Driver From Hanging During Connection or From Spending Too Long Trying to Reach Unreachable Replica Sets?如何防止驱动程序在连接期间挂起或花费太长时间试图访问无法访问的副本集?¶
To prevent the driver from hanging during connection or to prevent the driver from spending too long trying to reach unreachable replica sets, you can set the 要防止驱动程序在连接过程中挂起,或防止驱动程序花费太长时间尝试访问无法访问的副本集,可以设置连接选项的connectTimeoutMS
option of your connection options. connectTimeoutMS
选项。Generally, you should ensure that the 通常,您应该确保connectTimeoutMS
setting is not lower than the longest network latency you have to a member of the set. connectTimeoutMS
设置不低于您对集合成员的最长网络延迟。If one of the secondary members is on the other side of the planet and has a latency of 10000 milliseconds, setting the 如果其中一个辅助成员位于地球的另一端,且延迟为10000毫秒,则将connectTimeoutMS
to anything lower will prevent the driver from ever connecting to that member.connectTimeoutMS
设置为任何更低的值都将阻止驱动程序连接到该成员。
Should I Use "socketTimeoutMS" as a Way of Preventing Long-Running Operations From Slowing Down the Server?我应该使用“socketTimeoutMS”来防止长时间运行的操作降低服务器的速度吗?¶
No, you should not use 您不应该使用socketTimeoutMS
to end operations that may run for too long and slow down the application. socketTimeoutMS
来结束可能运行太长时间的操作并降低应用程序的速度。Attempting to do so may not achieve the intended result.不,试图这样做可能无法达到预期的结果。
Closing the socket causes a reconnect of the driver's connection pool, introducing latency to any other queued up operations. 关闭套接字会导致重新连接驱动程序的连接池,从而导致任何其他排队操作的延迟。Chronically slow operations will, therefore, cause a large number of reconnect requests, negatively impacting throughput and performance.因此,长期缓慢的操作将导致大量重新连接请求,对吞吐量和性能产生负面影响。
Also, closing the socket does not terminate the operation; it will continue to run on the MongoDB server, which could cause data inconsistencies if the application retries the operation on failure.此外,关闭插座不会终止操作;它将继续在MongoDB服务器上运行,如果应用程序在失败时重试该操作,可能会导致数据不一致。
However, there are important use cases for 但是,对于socketTimeoutMS
- consider the following cases:socketTimeoutMS
有重要的使用案例——考虑以下情况:
A MongoDB process erroring outMongoDB进程出错A misconfigured firewall causing a socket connection without sending a配置错误的防火墙导致套接字连接而不发送FIN
packetFIN
数据包
In those cases, there is no way to detect that the connection has died. 在这些情况下,无法检测连接是否已断开。Setting the 设置socketTimeoutMS
is essential to ensure that the sockets are closed correctly. socketTimeoutMS
对于确保插座正确关闭至关重要。A good rule of thumb is to set 一个好的经验法则是将socketTimeoutMS
to two to three times the length of the slowest operation that runs through the driver.socketTimeoutMS
设置为通过驱动程序运行的最慢操作长度的两到三倍。
How Can I Prevent Sockets From Timing out Before They Become Active?如何防止套接字在激活之前超时?¶
Having a large connection pool does not always reduce reconnection requests. 拥有大型连接池并不总是能够减少重新连接请求。Consider the following example:考虑下面的例子:
An application has a connection pool size of 5 sockets and has the 应用程序的连接池大小为5个套接字,并且将socketTimeoutMS
option set to 5000 milliseconds. socketTimeoutMS
选项设置为5000毫秒。Operations occur, on average, every 3000 milliseconds, and reconnection requests are frequent. 操作平均每3000毫秒发生一次,并且重新连接请求频繁。Each socket times out after 5000 milliseconds, which means that all sockets must do something during those 5000 milliseconds to avoid closing.每个套接字在5000毫秒后超时,这意味着所有套接字必须在这5000毫秒内执行某些操作以避免关闭。
One message every 3000 milliseconds is not enough to keep the sockets active, so several of the sockets will time out after 5000 milliseconds. 每3000毫秒发送一条消息不足以使套接字保持活动状态,因此有几个套接字将在5000毫秒后超时。Reduce the 减少连接设置中的poolSize
in the connection settings to fix the problem.poolSize
以修复此问题。
To specify the optional 要为poolSize
setting for your MongoClient
, declare it in the options
object of the constructor as follows:MongoClient
指定可选的poolSize
设置,请在构造函数的options
对象中声明它,如下所示:
const client = new MongoClient(uri, {
poolSize: <integervalue>,
});
What Does a Value of "0" mean for "connectTimeoutMS" and "socketTimeoutMS"?值“0”对于“connectTimeoutMS”和“socketTimeoutMS”意味着什么?¶
If you set the value of 如果将connectTimeoutMS
or socketTimeoutMS
to 0
, your application will use the operating system's default socket timeout value.connectTimeoutMS
或socketTimeoutMS
的值设置为0
,则应用程序将使用操作系统的默认套接字超时值。
How Can I Prevent Long-Running Operations From Slowing Down the Server?如何防止长时间运行的操作降低服务器速度?¶
You can prevent long-running operations from slowing down the server by specifying a timeout value. 通过指定超时值,可以防止长时间运行的操作降低服务器速度。You can use the 您可以使用maxTimeMS
option setting in your MongoClient
constructor to apply this to all the operations called by the client, or chain the maxTimeMS()
method to an operation that returns a Cursor
to apply to that specific one.MongoClient
构造函数中的maxTimeMS
选项设置将其应用于客户端调用的所有操作,或者将maxTimeMS()
方法链接到一个操作,该操作返回一个光标以应用于该特定操作。
To specify the optional 要为maxTimeMS
setting for your MongoClient
, declare it in the options
object of the constructor as follows:MongoClient
指定可选的maxTimeMS
设置,请在构造函数的options
对象中声明它,如下所示:
const client = new MongoClient(uri, {
connectTimeoutMS: <integer value>,
socketTimeoutMS: <integer value>,
maxTimeMS: <integer value>,
});
The following example shows how you can chain the 以下示例显示如何将maxTimeMS()
method to an operation that returns a Cursor
:maxTimeMS()
方法链接到返回Cursor
的操作:
// Execute a find command
await collection
.find({ $where: "sleep(100) || true" })
.maxTimeMS(50)
.count((err, count) => {});
What Does the "keepAlive" Setting Do?“keepAlive”设置的作用是什么?¶
keepAlive
is a connection-setting
that sets the number of milliseconds to wait before initiating a TLS keepAlive on a TCP Socket. keepAlive
是一种connection-setting
,用于设置在TCP套接字上启动TLS keepAlive之前等待的毫秒数。The keepAlive
option will keep a socket alive by sending periodic probes to MongoDB. keepAlive
选项将通过定期向MongoDB发送探测来保持套接字的活动状态。However, this only works if the operating system supports 但是,这只有在操作系统支持SO_KEEPALIVE
.SO_KEEPALIVE
时才有效。
What Can I Do If I'm Experiencing Unexpected Network Behavior?如果遇到意外的网络行为,我该怎么办?¶
Internal firewalls that exist between application servers and MongoDB are often misconfigured and are overly aggressive in their removal of socket connections.存在于应用服务器和MongoDB之间的内部防火墙通常配置错误,并且在移除套接字连接时过于激进。
If you experience unexpected network behavior, here are some things to check:如果您遇到意外的网络行为,请检查以下事项:
The firewall should send a防火墙应在关闭套接字时发送FIN数据包,使驱动程序能够检测到套接字已关闭。FIN packet
when closing a socket,allowing the driver to detect that the socket is closed.The firewall should allow防火墙应允许keepAlive
probes.keepAlive
探测。
What Can I Do If I'm Getting "ECONNRESET" When Calling "client.connect()"?如果调用“client.connect()
”时遇到“ECONNRESET”,该怎么办?¶
In most operating systems, each connection is associated with a file descriptor. 在大多数操作系统中,每个连接都与一个文件描述符相关联。There is typically a limit set by the operating system on the number of file descriptors used by a single process. 操作系统通常会对单个进程使用的文件描述符的数量设置限制。An 如果连接池大小超过ECONNRESET
error can occur if the connection pool size surpasses the limit of file descriptors
.file descriptors
的限制,则可能发生ECONNRESET
错误。
Consider the following operation:考虑以下操作:
1const uri = "mongodb://localhost:27017/test?maxPoolSize=5000";
2// create a new MongoClient
3const client = new MongoClient(uri);
4
5await client.connect(err=> {
6 // connection
7});
If this operation causes an 如果此操作导致ECONNRESET
error, you may have run into the file descriptor
limit for your Node.js process. ECONNRESET
错误,则您可能遇到Node.js进程的file descriptor
限制。In that case you must increase the number of <在这种情况下,必须增加Node.js进程的文件描述符数量。/ins>file descriptors
for the Node.js process. On MacOS and Linux you do this with the ulimit shell command.在MacOS和Linux上,可以使用ulimit shell命令执行此操作。
ulimit -n 6000
This sets the maximum number of 这将进程的最大file descriptors
for the process to 6000, allowing Node.js to connect with a pool size of 5000 sockets.file descriptors
数设置为6000,允许Node.js连接5000个套接字大小的池。
How Can I Prevent a Slow Operation From Delaying Other Operations?如何防止缓慢的操作延迟其他操作?¶
A slow operation may delay your other operations that occur after it, if the 如果未在连接选项中设置poolSize
has not been set in the connection options. poolSize
,则慢速操作可能会延迟之后发生的其他操作。MongoDB is synchronous and uses a single execution thread per socket, meaning that MongoDB will execute one single operation per socket at any point in time. MongoDB是同步的,每个套接字使用一个执行线程,这意味着MongoDB将在任何时间点对每个套接字执行一个操作。Any other operation sent to that socket will have to wait until the current operation is finished. 发送到该套接字的任何其他操作都必须等待当前操作完成。If you have a slow-running operation that holds up other operations, the best solution is to create a separate connection pool for the slow operation, isolating it from other, faster operations.如果您有一个运行缓慢的操作会阻塞其他操作,那么最好的解决方案是为该慢操作创建一个单独的连接池,将其与其他更快的操作隔离开来。
If the number of operations is greater than the set 如果操作数大于设置的poolSize
and a slow operation occurs, subsequent operations will be delayed.poolSize
且操作速度较慢,则后续操作将延迟。
To create a separate connection pool, instantiate another 要创建单独的连接池,请实例化另一个MongoClient
call the connect()
method on it. MongoClient
调用其上的connect()
方法。See the following example for the syntax you can use to create two clients, each with its own connection pool:有关可用于创建两个客户端(每个客户端都有自己的连接池)的语法,请参阅以下示例:
const clientA = new MongoClient(uri, options);
clientA.connect(); // any method calls on clientA use clientA's connection pool对clientA的任何方法调用都使用clientA的连接池
const clientB = new MongoClient(uri, options);
clientB.connect(); // any method calls on clientB use clientB's connection pool对clientB的任何方法调用都使用clientB的连接池
How Can I Ensure My Connection String Is Valid for a Replica Set?如何确保我的连接字符串对副本集有效?¶
The connection string passed to the driver must use exact hostnames for the servers as set in the Replica Set Config. 传递给驱动程序的连接字符串必须使用副本集配置中设置的服务器的确切主机名。Given the following configuration settings for your Replica Set, in order for the Replica Set discovery and failover to work the driver should be able to reach 给定副本集的以下配置设置,为了使副本集发现和故障切换工作,驱动程序应该能够访问server1
, server2
, and server3
.server1
、server2
和server3
。
{
"_id": "testSet",
"version": 1,
"protocolVersion": 1,
"members": [
{
"_id": 1,
"host": "server1:31000"
},
{
"_id": 2,
"host": "server2:31001"
},
{
"_id": 3,
"host": "server3:31002"
}
]
}
If you are unable to find the answer to your question here, try our forums and support channels listed in the Issues and Help section.如果您无法在此处找到问题的答案,请尝试问题和帮助部分列出的论坛和支持渠道。