Mongo Cursor time out in laravel 5.2

426 views
Skip to first unread message

Mukesh Kumar

unread,
Nov 17, 2016, 9:17:25 AM11/17/16
to mongodb-user
Hello,

We are Getting below exception in laravel 5.2 for mongodb

  exception 'MongoCursorTimeoutException' with message 'localhost:27017: Read timed out after reading 0 bytes, waited for 30.000000 seconds' in /var/www/html/hello-php-admin/test/jenssegers/mongodb/src/Jenssegers/Mongodb/Query/Builder.php:259

We don't to give cursor time out in each query, we want to put global connection timeout in laravel 5.2, not found any solution in stackoverflow or other places.

Jeremy Mikola

unread,
Nov 17, 2016, 10:03:48 AM11/17/16
to mongod...@googlegroups.com
Based on the exception message, I assume you're using an older version of the jenssegers/laravel-mongodb package, which utilizes the legacy PHP driver. 30 seconds is the default socket timeout (see "socketTimeoutMS" in the MongoClient::__construct() documentation), which you can specify globally via the constructor. Alternatively, it can be set on the cursor (which the package presumably configures via the timeout() query builder method) or as an option on some commands (I'm not sure how/if that is exposed in the Laravel package).

The default connection timeout is 60 seconds, which you likely don't need to change, as that only applies to the initial connection when the socket is established.

That said, an occasionally long-running query is not a reason to disable socket timeouts across the board. It would be preferable to only increase timeouts as-needed. And generally, MongoDB's "maxTimeMS" option for queries and commands is preferable to hard socket timeouts. It will allow the server to gracefully abort long-running queries, while a socket timeout still leaves the connection open and running on the server until the server realizes the client has abandoned it. This blog post goes into more detail on the subject and discusses both maxTimeMS and socketTimeoutMS.

If you have specific questions about getting timeouts to work with the Laravel package, I would suggest opening an issue on the repository. A cursory search revealed that there are numerous threads discussing timeouts in various forms (both socket and connection).


Reply all
Reply to author
Forward
0 new messages