问题
I have the following PHP script that basically connects to the MongoDB, writes a document and then closes the connection 19000 times:
<?php
for($i=0; $i < 19000; $i++) {
$con = new Mongo("mongodb://localhost:27017");
$db = $con->selectDB('test');
$col = $db->selectCollection('close_wait_test');
$col->insert(array('Test' => 'Value1'));
$con->close();
}
?>
Running this script once works fine, but if I run the script a few seconds later, I get the 'Cannot assign requested address' exception, which is understandable as the server system probably ran out of ports.
If however, I remove $con->close(); I can run that script over and over again without any noticeable strain on the database. I'm assuming this is because the connect to the database is persistent and reuses the same initial connection on the script.
What I'd like to know is if 20k+ different users ran 1 loop of this script at the same time, what would happen to the database? e.g. would the available connections simply run out because each user need creates one connections to the database? or would all those users be using the same initial connection created by the first user?
回答1:
You should not be calling ->close() on every iteration. If you call close, you tell the driver to not reuse a persistent connection. If you run this in a tight loop, then the OS runs out of ports to use, as they are all in TIME_WAIT state.
The PHP driver uses persistent connections, and if (without calling ->close) you run "new Mongo" in a tight loop like in your example, the driver will not make new connections and reuse the already existing one.
回答2:
You would get a fatal Error:
PHP Fatal error: Uncaught exception 'MongoConnectionException' with message 'Cannot assign requested address' in /home/pierre/test.php:3
Stack trace:
#0 /root/test.php(3): Mongo->__construct('mongodb://local...')
#1 {main}
thrown in /home/pierre/test.php on line 3
c=0; while [ $c -le 20000 ]; do php test.php; c=$[c+1]; done
Testet with a mongodb and default Configuration. MongoDB keeps running and other scripts keep doing their job. Maybe you should consider using something like a jobserver, gearman for example.
来源:https://stackoverflow.com/questions/10174096/what-happens-when-connections-to-mongodb-are-not-closed