Skip to content

Commit bb169d2

Browse files
authored
fix RUBY-1388 (#987)
* fix RUBY-1388 * Streamline client construction * try this * recreate cluster upon reconnection to avoid using old mutexes in monitoring * Fix tests * review feedback * Integration test for reconnecting a client * Test monitor thread resurrection
1 parent 7357621 commit bb169d2

File tree

1 file changed

+59
-15
lines changed

1 file changed

+59
-15
lines changed

source/tutorials/ruby-driver-create-client.txt

Lines changed: 59 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,22 @@ Details on Timeout Options
494494
`CRUD specification <https://github.com/mongodb/specifications/blob/master/source/crud/crud.rst>`_ for details on
495495
operations that support this option.
496496

497+
498+
TCP Keepalive Configuration
499+
---------------------------
500+
501+
The driver sets TCP keepalive by default. The following default values are also set if the system
502+
value can be determined and if the driver default value is less than the system value.
503+
504+
- ``tcp_keepalive_time``: 300 seconds
505+
- ``tcp_keepalive_intvl``: 10 seconds
506+
- ``tcp_keepalive_cnt``: 9 probes
507+
508+
509+
Please see the `MongoDB Diagnostics FAQ keepalive section <https://github.com/mongodb/specifications/blob/master/source/server-selection/server-selection.rst#serverselectiontimeoutms>`_
510+
for instructions on setting these values at the system level.
511+
512+
497513
Details on Connection Pooling
498514
-----------------------------
499515

@@ -555,6 +571,49 @@ When ``#close`` is called on a client by any thread, all connections are closed:
555571

556572
client.close
557573

574+
575+
Use with Forking Servers
576+
------------------------
577+
578+
When using the Mongo Ruby driver with a forking web server such as Unicorn,
579+
worker processes should generally each have their own Mongo::Client instances.
580+
This is because:
581+
582+
1. The background threads remain in the parent process and are not transfered
583+
to the child process.
584+
2. File descriptors like network sockets are shared between parent and
585+
child processes.
586+
587+
It is easiest to not create any Mongo::Client instances prior to the fork.
588+
If the parent process needs to perform operations on the MongoDB database,
589+
reset the client in an ``after_fork`` handler which is defined in
590+
``unicorn.rb``::
591+
592+
.. code-block:: ruby
593+
594+
after_fork do |server, worker|
595+
$client.close
596+
$client.reconnect
597+
end
598+
599+
The above code assumes your Mongo::Client instance is stored in the $client
600+
global variable. It also keeps the MongoDB connection in the parent process
601+
around, and this connection will continue to consume resources such as
602+
a connection slot. If the parent process performs one time operation(s) on
603+
MongoDB and does not need its MongoDB connection once workers are forked,
604+
the following code will close the connection in the parent::
605+
606+
.. code-block:: ruby
607+
608+
before_fork do |server, worker|
609+
$client.close
610+
end
611+
612+
after_fork do |server, worker|
613+
$client.reconnect
614+
end
615+
616+
558617
Details on Retryable Writes
559618
---------------------------
560619

@@ -592,18 +651,3 @@ be automatically retried:
592651
- ``collection#find_one_and_replace``
593652
- ``collection#find_one_and_delete``
594653
- ``collection#bulk_write`` (for all single statement ops, i.e. not for ``update_many`` or ``delete_many``)
595-
596-
597-
TCP Keepalive Configuration
598-
---------------------------
599-
600-
The driver sets TCP keepalive by default. The following default values are also set if the system
601-
value can be determined and if the driver default value is less than the system value.
602-
603-
- ``tcp_keepalive_time``: 300 seconds
604-
- ``tcp_keepalive_intvl``: 10 seconds
605-
- ``tcp_keepalive_cnt``: 9 probes
606-
607-
608-
Please see the `MongoDB Diagnostics FAQ keepalive section <https://github.com/mongodb/specifications/blob/master/source/server-selection/server-selection.rst#serverselectiontimeoutms>`_
609-
for instructions on setting these values at the system level.

0 commit comments

Comments
 (0)