Class: Mongo::Client
- Inherits:
-
Object
- Object
- Mongo::Client
- Extended by:
- Forwardable
- Includes:
- Loggable
- Defined in:
- lib/mongo/client.rb
Overview
The client is the entry point to the driver and is the main object that will be interacted with.
Constant Summary collapse
- CRUD_OPTIONS =
The options that do not affect the behavior of a cluster and its subcomponents.
[ :auto_encryption_options, :database, :read, :read_concern, :write, :write_concern, :retry_reads, :max_read_retries, :read_retry_interval, :retry_writes, :max_write_retries, :max_adaptive_retries, :enable_overload_retargeting, :timeout_ms, # Options which cannot currently be here: # # :server_selection_timeout # Server selection timeout is used by cluster constructor to figure out # how long to wait for initial scan in compatibility mode, but once # the cluster is initialized it no longer uses this timeout. # Unfortunately server selector reads server selection timeout out of # the cluster, and this behavior is required by Cluster#next_primary # which takes no arguments. When next_primary is removed we can revisit # using the same cluster object with different server selection timeouts. ].freeze
- VALID_OPTIONS =
Valid client options.
%i[ app_name auth_mech auth_mech_properties auth_source auto_encryption_options bg_error_backtrace cleanup compressors direct_connection enable_overload_retargeting connect connect_timeout database heartbeat_frequency id_generator load_balanced local_threshold logger log_prefix max_adaptive_retries max_connecting max_idle_time max_pool_size max_read_retries max_write_retries min_pool_size monitoring monitoring_io password platform populator_io read read_concern read_retry_interval replica_set resolv_options retry_reads retry_writes scan sdam_proc server_api server_monitoring_mode server_selection_timeout socket_timeout srv_max_hosts srv_service_name ssl ssl_ca_cert ssl_ca_cert_object ssl_ca_cert_string ssl_cert ssl_cert_object ssl_cert_string ssl_key ssl_key_object ssl_key_pass_phrase ssl_key_string ssl_verify ssl_verify_certificate ssl_verify_hostname ssl_verify_ocsp_endpoint timeout_ms tracing truncate_logs user wait_queue_timeout wrapping_libraries write write_concern zlib_compression_level ].freeze
- VALID_COMPRESSORS =
The compression algorithms supported by the driver.
[ Mongo::Protocol::Compressed::ZSTD, Mongo::Protocol::Compressed::SNAPPY, Mongo::Protocol::Compressed::ZLIB ].freeze
- VALID_SERVER_API_VERSIONS =
The known server API versions.
%w[ 1 ].freeze
Constants included from Loggable
Instance Attribute Summary collapse
-
#cluster ⇒ Mongo::Cluster
readonly
Cluster The cluster of servers for the client.
-
#database ⇒ Mongo::Database
readonly
Database The database the client is operating on.
-
#encrypter ⇒ Mongo::Crypt::AutoEncrypter
readonly
The object that encapsulates auto-encryption behavior.
-
#options ⇒ Hash
readonly
Options The configuration options.
-
#retry_policy ⇒ Mongo::Retryable::RetryPolicy
readonly
private
The retry policy for backpressure and adaptive retries.
Class Method Summary collapse
-
.canonicalize_ruby_options(options) ⇒ Object
private
Lowercases auth mechanism properties, if given, in the specified options, then converts the options to an instance of Options::Redacted.
Instance Method Summary collapse
-
#==(other) ⇒ true, false
(also: #eql?)
Determine if this client is equivalent to another object.
-
#[](collection_name, options = {}) ⇒ Mongo::Collection
Get a collection object for the provided collection name.
-
#close ⇒ true
Close all connections.
-
#close_encrypter ⇒ true
Close encrypter and clean up auto-encryption resources.
- #closed? ⇒ Boolean
- #cluster_options ⇒ Object private
-
#database_names(filter = {}, opts = {}) ⇒ Array<String>
Get the names of all databases.
-
#encrypted_fields_map ⇒ Hash | nil
private
Returns encrypted field map hash if provided when creating the client.
-
#get_session(options = {}) ⇒ Session | nil
private
Returns a session to use for operations if possible.
-
#hash ⇒ Integer
Get the hash value of the client.
-
#initialize(addresses_or_uri, options = nil) ⇒ Client
constructor
Instantiate a new driver client.
-
#inspect ⇒ String
Get an inspection of the client as a string.
-
#list_databases(filter = {}, name_only = false, opts = {}) ⇒ Array<Hash>
Get info for each database.
-
#list_mongo_databases(filter = {}, opts = {}) ⇒ Array<Mongo::Database>
Returns a list of Mongo::Database objects.
-
#max_read_retries ⇒ Integer
private
Get the maximum number of times the client can retry a read operation when using legacy read retries.
-
#max_write_retries ⇒ Integer
private
Get the maximum number of times the client can retry a write operation when using legacy write retries.
-
#monitoring ⇒ Monitoring
private
Monitoring The monitoring.
-
#read_concern ⇒ Hash
Get the read concern for this client.
-
#read_preference ⇒ BSON::Document
Get the read preference from the options passed to the client.
-
#read_retry_interval ⇒ Float
private
Get the interval, in seconds, in which read retries when using legacy read retries.
-
#reconnect ⇒ true
Reconnect the client.
-
#reset_cluster!(monitoring: nil) ⇒ Object
private
Replaces this client's cluster with a fresh instance built from the client's current options.
-
#reset_database! ⇒ Object
private
Replaces this client's database with a fresh instance built from the client's current options.
-
#server_selector ⇒ Mongo::ServerSelector
Get the server selector.
-
#start_session(options = {}) ⇒ Session
Start a session.
-
#summary ⇒ String
Get a summary of the client state.
-
#timeout_ms ⇒ Integer | nil
private
Value of timeout_ms option if set.
-
#timeout_sec ⇒ Float | nil
private
Value of timeout_ms option converted to seconds.
-
#tracer ⇒ Tracing::Tracer | nil
Get the tracer configured for this client.
-
#update_options(new_options) ⇒ Hash
private
Updates this client's options from new_options, validating all options.
-
#use(name) ⇒ Mongo::Client
Creates a new client configured to use the database with the provided name, and using the other options configured in this client.
-
#watch(pipeline = [], options = {}) ⇒ ChangeStream
Allows users to request that notifications are sent for all changes that occur in the client's cluster.
-
#with(new_options = nil) ⇒ Mongo::Client
Creates a new client with the passed options merged over the existing options of this client.
-
#with_session(options = {}) ⇒ Object
private
Creates a session to use for operations if possible and yields it to the provided block.
-
#write_concern ⇒ Mongo::WriteConcern
Get the write concern for this client.
Methods included from Loggable
#log_debug, #log_error, #log_fatal, #log_info, #log_warn, #logger
Constructor Details
#initialize(addresses_or_uri, options = nil) ⇒ Client
Instantiate a new driver client.
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 |
# File 'lib/mongo/client.rb', line 551 def initialize(addresses_or_uri, = nil) = ? .dup : {} processed = process_addresses(addresses_or_uri, ) uri = processed[:uri] addresses = processed[:addresses] = processed[:options] # If the URI is an SRV URI, note this so that we can start # SRV polling if the topology is a sharded cluster. srv_uri = uri if uri.is_a?(URI::SRVProtocol) = self.class.() # The server API version is specified to be a string. # However, it is very annoying to always provide the number 1 as a string, # therefore cast to the string type here. if (server_api = [:server_api]) && server_api.is_a?(Hash) server_api = Options::Redacted.new(server_api) if (version = server_api[:version]).is_a?(Integer) [:server_api] = server_api.merge(version: version.to_s) end end # Special handling for sdam_proc as it is only used during client # construction sdam_proc = .delete(:sdam_proc) # For gssapi service_name, the default option is given in a hash # (one level down from the top level). = () .each do |k, v| default_v = [k] v = default_v.merge(v) if default_v.is_a?(Hash) [k] = v end = .keys.each do |k| .delete(k) if [k].nil? end @options = () # WriteConcern object support # if @options[:write_concern].is_a?(WriteConcern::Base) # # Cache the instance so that we do not needlessly reconstruct it. # @write_concern = @options[:write_concern] # @options[:write_concern] = @write_concern.options # end @options.freeze (addresses, is_srv: uri.is_a?(URI::SRVProtocol)) = @options.dup .delete(:server_api) @database = Database.new(self, @options[:database], ) # Temporarily set monitoring so that event subscriptions can be # set up without there being a cluster @monitoring = Monitoring.new(@options) sdam_proc.call(self) if sdam_proc @connect_lock = Mutex.new @retry_policy = build_retry_policy @connect_lock.synchronize do @cluster = Cluster.new( addresses, @monitoring, .merge(srv_uri: srv_uri) ) end begin # Unset monitoring, it will be taken out of cluster from now on remove_instance_variable(:@monitoring) if @options[:auto_encryption_options] @connect_lock.synchronize do build_encrypter end end # ScriptError is rescued in addition to StandardError because # Mongo::Crypt::Binding raises a LoadError when libmongocrypt cannot be # found. Without it the cluster built above would be left open, leaking # its monitoring and connection pool threads. rescue StandardError, ScriptError begin @cluster.close rescue StandardError => e log_warn("Error closing cluster in client constructor's exception handler: #{e.class}: #{e}") # Drop this exception so that the original exception is raised end raise end return unless block_given? begin yield(self) ensure close end end |
Instance Attribute Details
#cluster ⇒ Mongo::Cluster (readonly)
Returns cluster The cluster of servers for the client.
143 144 145 |
# File 'lib/mongo/client.rb', line 143 def cluster @cluster end |
#database ⇒ Mongo::Database (readonly)
Returns database The database the client is operating on.
146 147 148 |
# File 'lib/mongo/client.rb', line 146 def database @database end |
#encrypter ⇒ Mongo::Crypt::AutoEncrypter (readonly)
Returns The object that encapsulates auto-encryption behavior.
153 154 155 |
# File 'lib/mongo/client.rb', line 153 def encrypter @encrypter end |
#options ⇒ Hash (readonly)
Returns options The configuration options.
149 150 151 |
# File 'lib/mongo/client.rb', line 149 def @options end |
#retry_policy ⇒ Mongo::Retryable::RetryPolicy (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns The retry policy for backpressure and adaptive retries.
158 159 160 |
# File 'lib/mongo/client.rb', line 158 def retry_policy @retry_policy end |
Class Method Details
.canonicalize_ruby_options(options) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Lowercases auth mechanism properties, if given, in the specified options, then converts the options to an instance of Options::Redacted.
1235 1236 1237 1238 1239 1240 1241 1242 |
# File 'lib/mongo/client.rb', line 1235 def () Options::Redacted.new(Hash[.map do |k, v| if [ :auth_mech_properties, 'auth_mech_properties' ].include?(k) && v v = Hash[v.map { |pk, pv| [ pk.downcase, pv ] }] end [ k, v ] end]) end |
Instance Method Details
#==(other) ⇒ true, false Also known as: eql?
Determine if this client is equivalent to another object.
186 187 188 189 190 |
# File 'lib/mongo/client.rb', line 186 def ==(other) return false unless other.is_a?(Client) cluster == other.cluster && == other. end |
#[](collection_name, options = {}) ⇒ Mongo::Collection
Get a collection object for the provided collection name.
204 205 206 |
# File 'lib/mongo/client.rb', line 204 def [](collection_name, = {}) database[collection_name, ] end |
#close ⇒ true
Close all connections.
936 937 938 939 940 941 942 |
# File 'lib/mongo/client.rb', line 936 def close @connect_lock.synchronize do @closed = true do_close end true end |
#close_encrypter ⇒ true
Close encrypter and clean up auto-encryption resources.
947 948 949 950 951 |
# File 'lib/mongo/client.rb', line 947 def close_encrypter @encrypter.close if @encrypter true end |
#closed? ⇒ Boolean
927 928 929 |
# File 'lib/mongo/client.rb', line 927 def closed? !!@closed end |
#cluster_options ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 |
# File 'lib/mongo/client.rb', line 658 def # We share clusters when a new client with different CRUD_OPTIONS # is requested; therefore, cluster should not be getting any of these # options upon instantiation .reject do |key, _value| CRUD_OPTIONS.include?(key.to_sym) end.merge( # but need to put the database back in for auth... database: [:database], # Put these options in for legacy compatibility, but note that # their values on the client and the cluster do not have to match - # applications should read these values from client, not from cluster max_read_retries: [:max_read_retries], read_retry_interval: [:read_retry_interval], tracer: tracer ).tap do || # If the client has a cluster already, forward srv_uri to the new # cluster to maintain SRV monitoring. If the client is brand new, # its constructor sets srv_uri manually. .update(srv_uri: cluster.[:srv_uri]) if cluster end end |
#database_names(filter = {}, opts = {}) ⇒ Array<String>
Get the names of all databases.
1005 1006 1007 |
# File 'lib/mongo/client.rb', line 1005 def database_names(filter = {}, opts = {}) list_databases(filter, true, opts).collect { |info| info['name'] } end |
#encrypted_fields_map ⇒ Hash | nil
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns encrypted field map hash if provided when creating the client.
1249 1250 1251 |
# File 'lib/mongo/client.rb', line 1249 def encrypted_fields_map @encrypted_fields_map ||= @options.fetch(:auto_encryption_options, {})[:encrypted_fields_map] end |
#get_session(options = {}) ⇒ Session | nil
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a session to use for operations if possible.
If :session option is set, validates that session and returns it. Otherwise, if deployment supports sessions, creates a new session and returns it. When a new session is created, the session will be implicit (lifecycle is managed by the driver) if the :implicit option is given, otherwise the session will be explicit (lifecycle managed by the application). If deployment does not support session, returns nil.
1193 1194 1195 1196 1197 |
# File 'lib/mongo/client.rb', line 1193 def get_session( = {}) get_session!() rescue Error::SessionsNotSupported nil end |
#hash ⇒ Integer
Get the hash value of the client.
216 217 218 |
# File 'lib/mongo/client.rb', line 216 def hash [ cluster, ].hash end |
#inspect ⇒ String
Get an inspection of the client as a string.
720 721 722 |
# File 'lib/mongo/client.rb', line 720 def inspect "#<Mongo::Client:0x#{object_id} cluster=#{cluster.summary}>" end |
#list_databases(filter = {}, name_only = false, opts = {}) ⇒ Array<Hash>
Get info for each database.
See https://mongodb.com/docs/manual/reference/command/listDatabases/
for more information and usage.
1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 |
# File 'lib/mongo/client.rb', line 1036 def list_databases(filter = {}, name_only = false, opts = {}) cmd = { listDatabases: 1 } cmd[:nameOnly] = !!name_only cmd[:filter] = filter unless filter.empty? cmd[:authorizedDatabases] = true if opts[:authorized_databases] use(Database::ADMIN) .database .read_command(cmd, opts.merge(op_name: 'listDatabases')) .first[Database::DATABASES] end |
#list_mongo_databases(filter = {}, opts = {}) ⇒ Array<Mongo::Database>
Returns a list of Mongo::Database objects.
1062 1063 1064 1065 1066 |
# File 'lib/mongo/client.rb', line 1062 def list_mongo_databases(filter = {}, opts = {}) database_names(filter, opts).collect do |name| Database.new(self, name, ) end end |
#max_read_retries ⇒ Integer
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Get the maximum number of times the client can retry a read operation when using legacy read retries.
688 689 690 |
# File 'lib/mongo/client.rb', line 688 def max_read_retries [:max_read_retries] || Cluster::MAX_READ_RETRIES end |
#max_write_retries ⇒ Integer
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Get the maximum number of times the client can retry a write operation when using legacy write retries.
708 709 710 |
# File 'lib/mongo/client.rb', line 708 def max_write_retries [:max_write_retries] || Cluster::MAX_WRITE_RETRIES end |
#monitoring ⇒ Monitoring
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns monitoring The monitoring.
168 169 170 171 172 173 174 |
# File 'lib/mongo/client.rb', line 168 def monitoring if cluster cluster.monitoring else @monitoring end end |
#read_concern ⇒ Hash
Get the read concern for this client.
910 911 912 |
# File 'lib/mongo/client.rb', line 910 def read_concern [:read_concern] end |
#read_preference ⇒ BSON::Document
Get the read preference from the options passed to the client.
768 769 770 |
# File 'lib/mongo/client.rb', line 768 def read_preference @read_preference ||= [:read] end |
#read_retry_interval ⇒ Float
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Get the interval, in seconds, in which read retries when using legacy read retries.
698 699 700 |
# File 'lib/mongo/client.rb', line 698 def read_retry_interval [:read_retry_interval] || Cluster::READ_RETRY_INTERVAL end |
#reconnect ⇒ true
Reconnect the client.
961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 |
# File 'lib/mongo/client.rb', line 961 def reconnect addresses = cluster.addresses.map(&:to_s) @connect_lock.synchronize do begin do_close rescue StandardError nil end @cluster = Cluster.new(addresses, monitoring, ) build_encrypter if @options[:auto_encryption_options] @closed = false end true end |
#reset_cluster!(monitoring: nil) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Replaces this client's cluster with a fresh instance built from the client's current options. Used by #with so a reconfigured client does not share its cluster with the client it was cloned from.
894 895 896 897 898 899 900 |
# File 'lib/mongo/client.rb', line 894 def reset_cluster!(monitoring: nil) @cluster = Cluster.new( cluster.addresses.map(&:to_s), monitoring || Monitoring.new, ) end |
#reset_database! ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Replaces this client's database with a fresh instance built from the client's current options. Used by #with so a reconfigured client does not share its database with the client it was cloned from.
882 883 884 |
# File 'lib/mongo/client.rb', line 882 def reset_database! @database = Database.new(self, [:database], ) end |
#server_selector ⇒ Mongo::ServerSelector
Get the server selector. It either uses the read preference defined in the client options or defaults to a Primary server selector.
746 747 748 749 750 751 752 |
# File 'lib/mongo/client.rb', line 746 def server_selector @server_selector ||= if read_preference ServerSelector.get(read_preference) else ServerSelector.primary end end |
#start_session(options = {}) ⇒ Session
A Session cannot be used by multiple threads at once; session objects are not thread-safe.
Start a session.
If the deployment does not support sessions, raises Mongo::Error::InvalidSession. This exception can also be raised when the driver is not connected to a data-bearing server, for example during failover.
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 |
# File 'lib/mongo/client.rb', line 1087 def start_session( = {}) session = get_session!(.merge(implicit: false)) if block_given? begin yield session ensure session.end_session end else session end end |
#summary ⇒ String
The exact format and layout of the returned summary string is not part of the driver's public API and may be changed at any time.
Get a summary of the client state.
732 733 734 |
# File 'lib/mongo/client.rb', line 732 def summary "#<Client cluster=#{cluster.summary}>" end |
#timeout_ms ⇒ Integer | nil
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns Value of timeout_ms option if set.
1255 1256 1257 |
# File 'lib/mongo/client.rb', line 1255 def timeout_ms @options[:timeout_ms] end |
#timeout_sec ⇒ Float | nil
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns Value of timeout_ms option converted to seconds.
1261 1262 1263 1264 1265 1266 1267 |
# File 'lib/mongo/client.rb', line 1261 def timeout_sec if timeout_ms.nil? nil else timeout_ms / 1_000.0 end end |
#tracer ⇒ Tracing::Tracer | nil
Get the tracer configured for this client.
1272 1273 1274 1275 1276 1277 1278 1279 |
# File 'lib/mongo/client.rb', line 1272 def tracer tracing_opts = @options[:tracing] || {} @tracer ||= Tracing.create_tracer( enabled: tracing_opts[:enabled], query_text_max_length: tracing_opts[:query_text_max_length], otel_tracer: tracing_opts[:tracer] ) end |
#update_options(new_options) ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Updates this client's options from new_options, validating all options.
The new options may be transformed according to various rules. The final hash of options actually applied to the client is returned.
If options fail validation, this method may warn or raise an exception. If this method raises an exception, the client should be discarded (similarly to if a constructor raised an exception).
833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 |
# File 'lib/mongo/client.rb', line 833 def () = @options = self.class.( || {}) ().tap do |opts| # Our options are frozen = @options.dup .delete(:write) if [:write] && opts[:write_concern] .delete(:write_concern) if [:write_concern] && opts[:write] .update(opts) @options = .freeze # The retry policy is built from the options, so a client created by # #with needs its own policy when the option changed. if @options[:max_adaptive_retries] != [:max_adaptive_retries] @retry_policy = build_retry_policy end = @options[:auto_encryption_options] != [:auto_encryption_options] # If there are new auto_encryption_options, create a new encrypter. # Otherwise, allow the new client to share an encrypter with the # original client. # # If auto_encryption_options are nil, set @encrypter to nil, but do not # close the encrypter because it may still be used by the original client. if @options[:auto_encryption_options] && @connect_lock.synchronize do build_encrypter end elsif @options[:auto_encryption_options].nil? @connect_lock.synchronize do @encrypter = nil end end end end |
#use(name) ⇒ Mongo::Client
The new client shares the cluster with the original client, and as a result also shares the monitoring instance and monitoring event subscribers.
Creates a new client configured to use the database with the provided name, and using the other options configured in this client.
787 788 789 |
# File 'lib/mongo/client.rb', line 787 def use(name) with(database: name) end |
#watch(pipeline = [], options = {}) ⇒ ChangeStream
A change stream only allows 'majority' read concern.
This helper method is preferable to running a raw aggregation with a $changeStream stage, for the purpose of supporting resumability.
Allows users to request that notifications are sent for all changes that occur in the client's cluster.
1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 |
# File 'lib/mongo/client.rb', line 1162 def watch(pipeline = [], = {}) return use(Database::ADMIN).watch(pipeline, ) unless database.name == Database::ADMIN = .dup [:cursor_type] = :tailable_await if [:max_await_time_ms] Mongo::Collection::View::ChangeStream.new( Mongo::Collection::View.new(self["#{Database::COMMAND}.aggregate"], {}, ), pipeline, Mongo::Collection::View::ChangeStream::CLUSTER, ) end |
#with(new_options = nil) ⇒ Mongo::Client
Depending on options given, the returned client may share the cluster with the original client or be created with a new cluster. If a new cluster is created, the monitoring event subscribers on the new client are set to the default event subscriber set and none of the subscribers on the original client are copied over.
Creates a new client with the passed options merged over the existing options of this client. Useful for one-offs to change specific options without altering the original client.
809 810 811 812 813 814 815 816 817 |
# File 'lib/mongo/client.rb', line 809 def with( = nil) clone.tap do |client| opts = client.( || Options::Redacted.new) client.reset_database! # We can't use the same cluster if some options that would affect it # have changed. client.reset_cluster!(monitoring: opts[:monitoring]) if (opts) end end |
#with_session(options = {}) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Creates a session to use for operations if possible and yields it to the provided block.
If :session option is set, validates that session and uses it. Otherwise, if deployment supports sessions, creates a new session and uses it. When a new session is created, the session will be implicit (lifecycle is managed by the driver) if the :implicit option is given, otherwise the session will be explicit (lifecycle managed by the application). If deployment does not support session, yields nil to the block.
When the block finishes, if the session was created and was implicit, or if an implicit session was passed in, the session is ended which returns it to the pool of available sessions.
1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 |
# File 'lib/mongo/client.rb', line 1219 def with_session( = {}) # RUBY-3174 will re-enable this guard; see #assert_not_closed. # assert_not_closed session = get_session() yield session ensure session.end_session if session && session.implicit? end |
#write_concern ⇒ Mongo::WriteConcern
Get the write concern for this client. If no option was provided, then a default single server acknowledgement will be used.
923 924 925 |
# File 'lib/mongo/client.rb', line 923 def write_concern @write_concern ||= WriteConcern.get([:write_concern] || [:write]) end |