mongo 2.22.0 → 2.24.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (467) hide show
  1. checksums.yaml +4 -4
  2. data/bin/mongo_console +0 -1
  3. data/lib/mongo/active_support.rb +1 -2
  4. data/lib/mongo/address/ipv4.rb +3 -6
  5. data/lib/mongo/address/ipv6.rb +6 -10
  6. data/lib/mongo/address/unix.rb +1 -4
  7. data/lib/mongo/address/validator.rb +16 -28
  8. data/lib/mongo/address.rb +30 -40
  9. data/lib/mongo/auth/aws/conversation.rb +6 -10
  10. data/lib/mongo/auth/aws/credentials.rb +0 -1
  11. data/lib/mongo/auth/aws/credentials_cache.rb +0 -1
  12. data/lib/mongo/auth/aws/credentials_retriever.rb +45 -59
  13. data/lib/mongo/auth/aws/request.rb +20 -35
  14. data/lib/mongo/auth/aws.rb +1 -2
  15. data/lib/mongo/auth/base.rb +20 -29
  16. data/lib/mongo/auth/conversation_base.rb +14 -18
  17. data/lib/mongo/auth/cr/conversation.rb +0 -3
  18. data/lib/mongo/auth/cr.rb +1 -4
  19. data/lib/mongo/auth/credential_cache.rb +0 -2
  20. data/lib/mongo/auth/gssapi/conversation.rb +3 -8
  21. data/lib/mongo/auth/gssapi.rb +1 -4
  22. data/lib/mongo/auth/ldap/conversation.rb +0 -3
  23. data/lib/mongo/auth/ldap.rb +1 -4
  24. data/lib/mongo/auth/roles.rb +16 -19
  25. data/lib/mongo/auth/sasl_conversation_base.rb +7 -11
  26. data/lib/mongo/auth/scram/conversation.rb +2 -5
  27. data/lib/mongo/auth/scram.rb +5 -10
  28. data/lib/mongo/auth/scram256/conversation.rb +2 -5
  29. data/lib/mongo/auth/scram256.rb +1 -3
  30. data/lib/mongo/auth/scram_conversation_base.rb +18 -24
  31. data/lib/mongo/auth/stringprep/profiles/sasl.rb +17 -18
  32. data/lib/mongo/auth/stringprep/tables.rb +2209 -2210
  33. data/lib/mongo/auth/stringprep/unicode_normalize/normalize.rb +36 -38
  34. data/lib/mongo/auth/stringprep/unicode_normalize/tables.rb +1142 -1150
  35. data/lib/mongo/auth/stringprep.rb +9 -12
  36. data/lib/mongo/auth/user/view.rb +3 -5
  37. data/lib/mongo/auth/user.rb +14 -24
  38. data/lib/mongo/auth/x509/conversation.rb +0 -3
  39. data/lib/mongo/auth/x509.rb +7 -9
  40. data/lib/mongo/auth.rb +18 -30
  41. data/lib/mongo/background_thread.rb +9 -17
  42. data/lib/mongo/bson.rb +0 -2
  43. data/lib/mongo/bulk_write/combineable.rb +0 -3
  44. data/lib/mongo/bulk_write/ordered_combiner.rb +1 -3
  45. data/lib/mongo/bulk_write/result.rb +11 -16
  46. data/lib/mongo/bulk_write/result_combiner.rb +9 -12
  47. data/lib/mongo/bulk_write/transformable.rb +16 -19
  48. data/lib/mongo/bulk_write/unordered_combiner.rb +1 -3
  49. data/lib/mongo/bulk_write/validatable.rb +11 -18
  50. data/lib/mongo/bulk_write.rb +76 -91
  51. data/lib/mongo/caching_cursor.rb +2 -7
  52. data/lib/mongo/client.rb +267 -276
  53. data/lib/mongo/client_encryption.rb +4 -5
  54. data/lib/mongo/cluster/periodic_executor.rb +2 -5
  55. data/lib/mongo/cluster/reapers/cursor_reaper.rb +21 -29
  56. data/lib/mongo/cluster/reapers/socket_reaper.rb +1 -6
  57. data/lib/mongo/cluster/sdam_flow.rb +136 -159
  58. data/lib/mongo/cluster/topology/base.rb +15 -18
  59. data/lib/mongo/cluster/topology/load_balanced.rb +24 -14
  60. data/lib/mongo/cluster/topology/no_replica_set_options.rb +3 -6
  61. data/lib/mongo/cluster/topology/replica_set_no_primary.rb +20 -23
  62. data/lib/mongo/cluster/topology/replica_set_with_primary.rb +0 -2
  63. data/lib/mongo/cluster/topology/sharded.rb +19 -9
  64. data/lib/mongo/cluster/topology/single.rb +24 -14
  65. data/lib/mongo/cluster/topology/unknown.rb +20 -10
  66. data/lib/mongo/cluster/topology.rb +29 -25
  67. data/lib/mongo/cluster.rb +152 -184
  68. data/lib/mongo/cluster_time.rb +14 -31
  69. data/lib/mongo/collection/helpers.rb +5 -8
  70. data/lib/mongo/collection/view/aggregation/behavior.rb +1 -1
  71. data/lib/mongo/collection/view/aggregation.rb +10 -12
  72. data/lib/mongo/collection/view/builder/aggregation.rb +6 -9
  73. data/lib/mongo/collection/view/builder/map_reduce.rb +18 -17
  74. data/lib/mongo/collection/view/builder.rb +0 -1
  75. data/lib/mongo/collection/view/change_stream/retryable.rb +3 -8
  76. data/lib/mongo/collection/view/change_stream.rb +59 -58
  77. data/lib/mongo/collection/view/explainable.rb +11 -20
  78. data/lib/mongo/collection/view/immutable.rb +1 -3
  79. data/lib/mongo/collection/view/iterable.rb +44 -35
  80. data/lib/mongo/collection/view/map_reduce.rb +20 -25
  81. data/lib/mongo/collection/view/readable.rb +96 -94
  82. data/lib/mongo/collection/view/writable.rb +104 -114
  83. data/lib/mongo/collection/view.rb +11 -8
  84. data/lib/mongo/collection.rb +103 -106
  85. data/lib/mongo/condition_variable.rb +4 -4
  86. data/lib/mongo/config/options.rb +0 -3
  87. data/lib/mongo/config/validators/option.rb +3 -5
  88. data/lib/mongo/config.rb +6 -4
  89. data/lib/mongo/crypt/auto_decryption_context.rb +9 -3
  90. data/lib/mongo/crypt/auto_encrypter.rb +34 -43
  91. data/lib/mongo/crypt/auto_encryption_context.rb +0 -3
  92. data/lib/mongo/crypt/binary.rb +5 -9
  93. data/lib/mongo/crypt/binding.rb +150 -156
  94. data/lib/mongo/crypt/context.rb +20 -17
  95. data/lib/mongo/crypt/data_key_context.rb +2 -7
  96. data/lib/mongo/crypt/encryption_io.rb +29 -39
  97. data/lib/mongo/crypt/explicit_decryption_context.rb +9 -3
  98. data/lib/mongo/crypt/explicit_encrypter.rb +1 -1
  99. data/lib/mongo/crypt/explicit_encryption_context.rb +19 -30
  100. data/lib/mongo/crypt/explicit_encryption_expression_context.rb +0 -2
  101. data/lib/mongo/crypt/handle.rb +42 -48
  102. data/lib/mongo/crypt/hooks.rb +12 -15
  103. data/lib/mongo/crypt/kms/aws/credentials.rb +12 -16
  104. data/lib/mongo/crypt/kms/aws/master_document.rb +6 -9
  105. data/lib/mongo/crypt/kms/aws.rb +0 -2
  106. data/lib/mongo/crypt/kms/azure/credentials_retriever.rb +2 -7
  107. data/lib/mongo/crypt/kms/azure/master_document.rb +15 -19
  108. data/lib/mongo/crypt/kms/azure.rb +0 -1
  109. data/lib/mongo/crypt/kms/credentials.rb +13 -27
  110. data/lib/mongo/crypt/kms/gcp/credentials.rb +12 -14
  111. data/lib/mongo/crypt/kms/gcp/credentials_retriever.rb +7 -9
  112. data/lib/mongo/crypt/kms/gcp/master_document.rb +12 -16
  113. data/lib/mongo/crypt/kms/gcp.rb +0 -2
  114. data/lib/mongo/crypt/kms/kmip/credentials.rb +7 -8
  115. data/lib/mongo/crypt/kms/kmip/master_document.rb +3 -5
  116. data/lib/mongo/crypt/kms/kmip.rb +0 -1
  117. data/lib/mongo/crypt/kms/local/credentials.rb +7 -8
  118. data/lib/mongo/crypt/kms/local/master_document.rb +2 -6
  119. data/lib/mongo/crypt/kms/local.rb +0 -1
  120. data/lib/mongo/crypt/kms/master_key_document.rb +11 -15
  121. data/lib/mongo/crypt/kms.rb +14 -16
  122. data/lib/mongo/crypt/kms_context.rb +0 -2
  123. data/lib/mongo/crypt/rewrap_many_data_key_context.rb +2 -7
  124. data/lib/mongo/crypt/rewrap_many_data_key_result.rb +2 -4
  125. data/lib/mongo/crypt/status.rb +12 -14
  126. data/lib/mongo/crypt.rb +0 -1
  127. data/lib/mongo/csot_timeout_holder.rb +3 -2
  128. data/lib/mongo/cursor/kill_spec.rb +7 -10
  129. data/lib/mongo/cursor.rb +74 -64
  130. data/lib/mongo/cursor_host.rb +8 -10
  131. data/lib/mongo/database/view.rb +23 -39
  132. data/lib/mongo/database.rb +68 -65
  133. data/lib/mongo/dbref.rb +0 -1
  134. data/lib/mongo/deprecations.rb +98 -0
  135. data/lib/mongo/distinguishing_semaphore.rb +0 -1
  136. data/lib/mongo/error/auth_error.rb +0 -2
  137. data/lib/mongo/error/bad_load_balancer_target.rb +0 -2
  138. data/lib/mongo/error/bulk_write_error.rb +7 -10
  139. data/lib/mongo/error/change_stream_resumable.rb +0 -2
  140. data/lib/mongo/error/client_closed.rb +0 -2
  141. data/lib/mongo/error/closed_stream.rb +1 -4
  142. data/lib/mongo/error/connection_check_out_timeout.rb +3 -6
  143. data/lib/mongo/error/connection_perished.rb +0 -2
  144. data/lib/mongo/error/connection_unavailable.rb +0 -2
  145. data/lib/mongo/error/credential_check_error.rb +0 -2
  146. data/lib/mongo/error/crypt_error.rb +0 -2
  147. data/lib/mongo/error/extra_file_chunk.rb +1 -4
  148. data/lib/mongo/error/failed_string_prep_validation.rb +5 -6
  149. data/lib/mongo/error/file_not_found.rb +0 -3
  150. data/lib/mongo/error/handshake_error.rb +0 -2
  151. data/lib/mongo/error/insufficient_iteration_count.rb +1 -4
  152. data/lib/mongo/error/internal_driver_error.rb +0 -2
  153. data/lib/mongo/error/invalid_address.rb +0 -2
  154. data/lib/mongo/error/invalid_application_name.rb +0 -3
  155. data/lib/mongo/error/invalid_bulk_operation.rb +1 -4
  156. data/lib/mongo/error/invalid_bulk_operation_type.rb +1 -4
  157. data/lib/mongo/error/invalid_collection_name.rb +1 -4
  158. data/lib/mongo/error/invalid_config_option.rb +0 -3
  159. data/lib/mongo/error/invalid_cursor_operation.rb +0 -2
  160. data/lib/mongo/error/invalid_database_name.rb +1 -4
  161. data/lib/mongo/error/invalid_document.rb +1 -4
  162. data/lib/mongo/error/invalid_file.rb +0 -3
  163. data/lib/mongo/error/invalid_file_revision.rb +0 -3
  164. data/lib/mongo/error/invalid_min_pool_size.rb +0 -3
  165. data/lib/mongo/error/invalid_nonce.rb +0 -3
  166. data/lib/mongo/error/invalid_read_concern.rb +2 -4
  167. data/lib/mongo/error/invalid_read_option.rb +0 -3
  168. data/lib/mongo/error/invalid_replacement_document.rb +2 -5
  169. data/lib/mongo/error/invalid_server_auth_host.rb +0 -2
  170. data/lib/mongo/error/invalid_server_auth_response.rb +0 -2
  171. data/lib/mongo/error/invalid_server_preference.rb +7 -16
  172. data/lib/mongo/error/invalid_session.rb +1 -4
  173. data/lib/mongo/error/invalid_signature.rb +0 -3
  174. data/lib/mongo/error/invalid_transaction_operation.rb +5 -8
  175. data/lib/mongo/error/invalid_txt_record.rb +0 -2
  176. data/lib/mongo/error/invalid_update_document.rb +2 -5
  177. data/lib/mongo/error/invalid_uri.rb +1 -4
  178. data/lib/mongo/error/invalid_write_concern.rb +2 -5
  179. data/lib/mongo/error/kms_error.rb +0 -2
  180. data/lib/mongo/error/labelable.rb +0 -3
  181. data/lib/mongo/error/lint_error.rb +0 -2
  182. data/lib/mongo/error/max_bson_size.rb +8 -11
  183. data/lib/mongo/error/max_message_size.rb +2 -5
  184. data/lib/mongo/error/mismatched_domain.rb +0 -2
  185. data/lib/mongo/error/missing_connection.rb +0 -2
  186. data/lib/mongo/error/missing_file_chunk.rb +0 -3
  187. data/lib/mongo/error/missing_password.rb +0 -2
  188. data/lib/mongo/error/missing_resume_token.rb +1 -4
  189. data/lib/mongo/error/missing_scram_server_signature.rb +2 -4
  190. data/lib/mongo/error/missing_service_id.rb +0 -2
  191. data/lib/mongo/error/mongocryptd_spawn_error.rb +0 -2
  192. data/lib/mongo/error/multi_index_drop.rb +0 -3
  193. data/lib/mongo/error/need_primary_server.rb +0 -2
  194. data/lib/mongo/error/no_server_available.rb +3 -8
  195. data/lib/mongo/error/no_service_connection_available.rb +1 -3
  196. data/lib/mongo/error/no_srv_records.rb +0 -2
  197. data/lib/mongo/error/notable.rb +8 -16
  198. data/lib/mongo/error/operation_failure.rb +22 -35
  199. data/lib/mongo/error/parser.rb +33 -75
  200. data/lib/mongo/error/pool_cleared_error.rb +1 -3
  201. data/lib/mongo/error/pool_closed_error.rb +0 -3
  202. data/lib/mongo/error/pool_error.rb +0 -3
  203. data/lib/mongo/error/pool_paused_error.rb +0 -2
  204. data/lib/mongo/error/raise_original_error.rb +1 -3
  205. data/lib/mongo/error/read_write_retryable.rb +14 -17
  206. data/lib/mongo/error/sdam_error_detection.rb +3 -5
  207. data/lib/mongo/error/server_api_conflict.rb +0 -2
  208. data/lib/mongo/error/server_certificate_revoked.rb +0 -2
  209. data/lib/mongo/error/server_not_usable.rb +0 -2
  210. data/lib/mongo/error/session_ended.rb +1 -3
  211. data/lib/mongo/error/session_not_materialized.rb +1 -3
  212. data/lib/mongo/error/sessions_not_supported.rb +1 -4
  213. data/lib/mongo/error/snapshot_session_invalid_server_version.rb +1 -4
  214. data/lib/mongo/error/snapshot_session_transaction_prohibited.rb +1 -4
  215. data/lib/mongo/error/socket_error.rb +0 -2
  216. data/lib/mongo/error/socket_timeout_error.rb +0 -2
  217. data/lib/mongo/error/transactions_not_supported.rb +3 -6
  218. data/lib/mongo/error/unchangeable_collection_option.rb +1 -4
  219. data/lib/mongo/error/unexpected_chunk_length.rb +0 -3
  220. data/lib/mongo/error/unexpected_response.rb +1 -4
  221. data/lib/mongo/error/unknown_payload_type.rb +0 -3
  222. data/lib/mongo/error/unmet_dependency.rb +0 -2
  223. data/lib/mongo/error/unsupported_array_filters.rb +3 -24
  224. data/lib/mongo/error/unsupported_collation.rb +3 -24
  225. data/lib/mongo/error/unsupported_features.rb +0 -2
  226. data/lib/mongo/error/unsupported_message_type.rb +0 -2
  227. data/lib/mongo/error/unsupported_option.rb +19 -21
  228. data/lib/mongo/error/write_retryable.rb +0 -2
  229. data/lib/mongo/error.rb +10 -24
  230. data/lib/mongo/event/base.rb +0 -2
  231. data/lib/mongo/event/listeners.rb +0 -3
  232. data/lib/mongo/event/publisher.rb +0 -3
  233. data/lib/mongo/event/subscriber.rb +0 -4
  234. data/lib/mongo/event.rb +4 -6
  235. data/lib/mongo/grid/file/chunk.rb +7 -10
  236. data/lib/mongo/grid/file/info.rb +20 -24
  237. data/lib/mongo/grid/file.rb +7 -8
  238. data/lib/mongo/grid/fs_bucket.rb +40 -48
  239. data/lib/mongo/grid/stream/read.rb +25 -35
  240. data/lib/mongo/grid/stream/write.rb +17 -22
  241. data/lib/mongo/grid/stream.rb +2 -4
  242. data/lib/mongo/grid.rb +0 -1
  243. data/lib/mongo/id.rb +0 -1
  244. data/lib/mongo/index/view.rb +68 -58
  245. data/lib/mongo/index.rb +7 -10
  246. data/lib/mongo/lint.rb +31 -37
  247. data/lib/mongo/loggable.rb +5 -8
  248. data/lib/mongo/logger.rb +1 -7
  249. data/lib/mongo/monitoring/cmap_log_subscriber.rb +0 -2
  250. data/lib/mongo/monitoring/command_log_subscriber.rb +25 -33
  251. data/lib/mongo/monitoring/event/cmap/base.rb +0 -2
  252. data/lib/mongo/monitoring/event/cmap/connection_check_out_failed.rb +1 -4
  253. data/lib/mongo/monitoring/event/cmap/connection_check_out_started.rb +0 -3
  254. data/lib/mongo/monitoring/event/cmap/connection_checked_in.rb +1 -4
  255. data/lib/mongo/monitoring/event/cmap/connection_checked_out.rb +2 -5
  256. data/lib/mongo/monitoring/event/cmap/connection_closed.rb +1 -4
  257. data/lib/mongo/monitoring/event/cmap/connection_created.rb +1 -4
  258. data/lib/mongo/monitoring/event/cmap/connection_ready.rb +1 -4
  259. data/lib/mongo/monitoring/event/cmap/pool_cleared.rb +0 -3
  260. data/lib/mongo/monitoring/event/cmap/pool_closed.rb +1 -4
  261. data/lib/mongo/monitoring/event/cmap/pool_created.rb +1 -4
  262. data/lib/mongo/monitoring/event/cmap/pool_ready.rb +1 -4
  263. data/lib/mongo/monitoring/event/cmap.rb +0 -1
  264. data/lib/mongo/monitoring/event/command_failed.rb +5 -9
  265. data/lib/mongo/monitoring/event/command_started.rb +8 -12
  266. data/lib/mongo/monitoring/event/command_succeeded.rb +7 -15
  267. data/lib/mongo/monitoring/event/secure.rb +15 -20
  268. data/lib/mongo/monitoring/event/server_closed.rb +1 -4
  269. data/lib/mongo/monitoring/event/server_description_changed.rb +4 -8
  270. data/lib/mongo/monitoring/event/server_heartbeat_failed.rb +5 -10
  271. data/lib/mongo/monitoring/event/server_heartbeat_started.rb +1 -4
  272. data/lib/mongo/monitoring/event/server_heartbeat_succeeded.rb +3 -8
  273. data/lib/mongo/monitoring/event/server_opening.rb +1 -4
  274. data/lib/mongo/monitoring/event/topology_changed.rb +2 -5
  275. data/lib/mongo/monitoring/event/topology_closed.rb +1 -4
  276. data/lib/mongo/monitoring/event/topology_opening.rb +1 -4
  277. data/lib/mongo/monitoring/event.rb +0 -1
  278. data/lib/mongo/monitoring/publishable.rb +20 -30
  279. data/lib/mongo/monitoring/sdam_log_subscriber.rb +0 -2
  280. data/lib/mongo/monitoring/server_closed_log_subscriber.rb +0 -3
  281. data/lib/mongo/monitoring/server_description_changed_log_subscriber.rb +0 -3
  282. data/lib/mongo/monitoring/server_opening_log_subscriber.rb +0 -3
  283. data/lib/mongo/monitoring/topology_changed_log_subscriber.rb +5 -8
  284. data/lib/mongo/monitoring/topology_closed_log_subscriber.rb +0 -3
  285. data/lib/mongo/monitoring/topology_opening_log_subscriber.rb +0 -3
  286. data/lib/mongo/monitoring/unified_sdam_log_subscriber.rb +1 -3
  287. data/lib/mongo/monitoring.rb +38 -39
  288. data/lib/mongo/operation/aggregate/op_msg.rb +0 -2
  289. data/lib/mongo/operation/aggregate/result.rb +3 -6
  290. data/lib/mongo/operation/aggregate.rb +0 -2
  291. data/lib/mongo/operation/collections_info/result.rb +0 -3
  292. data/lib/mongo/operation/collections_info.rb +0 -2
  293. data/lib/mongo/operation/command/op_msg.rb +1 -4
  294. data/lib/mongo/operation/command.rb +0 -2
  295. data/lib/mongo/operation/context.rb +13 -16
  296. data/lib/mongo/operation/count/op_msg.rb +2 -4
  297. data/lib/mongo/operation/count.rb +0 -2
  298. data/lib/mongo/operation/create/op_msg.rb +2 -5
  299. data/lib/mongo/operation/create.rb +4 -2
  300. data/lib/mongo/operation/create_index/op_msg.rb +3 -7
  301. data/lib/mongo/operation/create_index.rb +0 -2
  302. data/lib/mongo/operation/create_user/op_msg.rb +2 -4
  303. data/lib/mongo/operation/create_user.rb +0 -2
  304. data/lib/mongo/operation/delete/bulk_result.rb +2 -3
  305. data/lib/mongo/operation/delete/op_msg.rb +3 -10
  306. data/lib/mongo/operation/delete/result.rb +0 -3
  307. data/lib/mongo/operation/delete.rb +1 -5
  308. data/lib/mongo/operation/distinct/op_msg.rb +2 -5
  309. data/lib/mongo/operation/distinct.rb +0 -2
  310. data/lib/mongo/operation/drop/op_msg.rb +0 -2
  311. data/lib/mongo/operation/drop.rb +0 -2
  312. data/lib/mongo/operation/drop_database/op_msg.rb +0 -2
  313. data/lib/mongo/operation/drop_database.rb +0 -2
  314. data/lib/mongo/operation/drop_index/op_msg.rb +4 -6
  315. data/lib/mongo/operation/drop_index.rb +0 -2
  316. data/lib/mongo/operation/explain/op_msg.rb +0 -2
  317. data/lib/mongo/operation/explain/result.rb +0 -3
  318. data/lib/mongo/operation/explain.rb +0 -2
  319. data/lib/mongo/operation/find/builder/command.rb +4 -12
  320. data/lib/mongo/operation/find/builder/flags.rb +9 -15
  321. data/lib/mongo/operation/find/builder/modifiers.rb +1 -4
  322. data/lib/mongo/operation/find/builder.rb +0 -1
  323. data/lib/mongo/operation/find/op_msg.rb +4 -12
  324. data/lib/mongo/operation/find/result.rb +0 -3
  325. data/lib/mongo/operation/find.rb +0 -2
  326. data/lib/mongo/operation/get_more/command_builder.rb +1 -6
  327. data/lib/mongo/operation/get_more/op_msg.rb +10 -4
  328. data/lib/mongo/operation/get_more/result.rb +0 -3
  329. data/lib/mongo/operation/get_more.rb +0 -2
  330. data/lib/mongo/operation/indexes/op_msg.rb +0 -2
  331. data/lib/mongo/operation/indexes/result.rb +1 -5
  332. data/lib/mongo/operation/indexes.rb +0 -2
  333. data/lib/mongo/operation/insert/bulk_result.rb +2 -6
  334. data/lib/mongo/operation/insert/op_msg.rb +7 -6
  335. data/lib/mongo/operation/insert/result.rb +0 -3
  336. data/lib/mongo/operation/insert.rb +2 -5
  337. data/lib/mongo/operation/kill_cursors/command_builder.rb +0 -3
  338. data/lib/mongo/operation/kill_cursors/op_msg.rb +1 -3
  339. data/lib/mongo/operation/kill_cursors.rb +0 -2
  340. data/lib/mongo/operation/list_collections/op_msg.rb +4 -6
  341. data/lib/mongo/operation/list_collections/result.rb +1 -4
  342. data/lib/mongo/operation/list_collections.rb +0 -2
  343. data/lib/mongo/operation/map_reduce/op_msg.rb +0 -2
  344. data/lib/mongo/operation/map_reduce/result.rb +3 -6
  345. data/lib/mongo/operation/map_reduce.rb +0 -2
  346. data/lib/mongo/operation/op_msg_base.rb +0 -1
  347. data/lib/mongo/operation/parallel_scan/op_msg.rb +4 -5
  348. data/lib/mongo/operation/parallel_scan/result.rb +2 -5
  349. data/lib/mongo/operation/parallel_scan.rb +0 -2
  350. data/lib/mongo/operation/remove_user/op_msg.rb +2 -4
  351. data/lib/mongo/operation/remove_user.rb +0 -2
  352. data/lib/mongo/operation/result.rb +38 -48
  353. data/lib/mongo/operation/shared/bypass_document_validation.rb +3 -7
  354. data/lib/mongo/operation/shared/causal_consistency_supported.rb +0 -3
  355. data/lib/mongo/operation/shared/executable.rb +29 -31
  356. data/lib/mongo/operation/shared/executable_no_validate.rb +0 -3
  357. data/lib/mongo/operation/shared/executable_transaction_label.rb +0 -2
  358. data/lib/mongo/operation/shared/idable.rb +3 -6
  359. data/lib/mongo/operation/shared/limited.rb +0 -3
  360. data/lib/mongo/operation/shared/object_id_generator.rb +0 -3
  361. data/lib/mongo/operation/shared/op_msg_executable.rb +0 -2
  362. data/lib/mongo/operation/shared/polymorphic_lookup.rb +0 -2
  363. data/lib/mongo/operation/shared/polymorphic_result.rb +2 -4
  364. data/lib/mongo/operation/shared/read_preference_supported.rb +10 -15
  365. data/lib/mongo/operation/shared/response_handling.rb +13 -26
  366. data/lib/mongo/operation/shared/result/aggregatable.rb +12 -13
  367. data/lib/mongo/operation/shared/sessions_supported.rb +87 -99
  368. data/lib/mongo/operation/shared/specifiable.rb +37 -59
  369. data/lib/mongo/operation/shared/write.rb +12 -17
  370. data/lib/mongo/operation/shared/write_concern_supported.rb +4 -7
  371. data/lib/mongo/operation/update/bulk_result.rb +13 -17
  372. data/lib/mongo/operation/update/op_msg.rb +2 -5
  373. data/lib/mongo/operation/update/result.rb +5 -5
  374. data/lib/mongo/operation/update.rb +1 -5
  375. data/lib/mongo/operation/update_user/op_msg.rb +2 -4
  376. data/lib/mongo/operation/update_user.rb +0 -2
  377. data/lib/mongo/operation/users_info/op_msg.rb +2 -4
  378. data/lib/mongo/operation/users_info/result.rb +1 -4
  379. data/lib/mongo/operation/users_info.rb +0 -2
  380. data/lib/mongo/operation/write_command/op_msg.rb +2 -10
  381. data/lib/mongo/operation/write_command.rb +0 -2
  382. data/lib/mongo/operation.rb +9 -14
  383. data/lib/mongo/options/mapper.rb +8 -15
  384. data/lib/mongo/options/redacted.rb +7 -9
  385. data/lib/mongo/options.rb +0 -1
  386. data/lib/mongo/protocol/bit_vector.rb +3 -5
  387. data/lib/mongo/protocol/caching_hash.rb +2 -7
  388. data/lib/mongo/protocol/compressed.rb +5 -10
  389. data/lib/mongo/protocol/get_more.rb +2 -8
  390. data/lib/mongo/protocol/kill_cursors.rb +2 -8
  391. data/lib/mongo/protocol/message.rb +103 -105
  392. data/lib/mongo/protocol/msg.rb +48 -63
  393. data/lib/mongo/protocol/query.rb +32 -41
  394. data/lib/mongo/protocol/registry.rb +2 -5
  395. data/lib/mongo/protocol/reply.rb +10 -16
  396. data/lib/mongo/protocol/serializers.rb +41 -59
  397. data/lib/mongo/protocol.rb +0 -1
  398. data/lib/mongo/query_cache.rb +7 -15
  399. data/lib/mongo/retryable/backpressure.rb +31 -0
  400. data/lib/mongo/retryable/base_worker.rb +39 -13
  401. data/lib/mongo/retryable/read_worker.rb +77 -21
  402. data/lib/mongo/retryable/retry_policy.rb +59 -0
  403. data/lib/mongo/retryable/write_worker.rb +155 -56
  404. data/lib/mongo/retryable.rb +70 -9
  405. data/lib/mongo/search_index/view.rb +30 -10
  406. data/lib/mongo/semaphore.rb +0 -1
  407. data/lib/mongo/server/app_metadata/environment.rb +3 -3
  408. data/lib/mongo/server/app_metadata/platform.rb +17 -4
  409. data/lib/mongo/server/app_metadata.rb +4 -5
  410. data/lib/mongo/server/connection.rb +79 -61
  411. data/lib/mongo/server/connection_base.rb +43 -53
  412. data/lib/mongo/server/connection_common.rb +41 -64
  413. data/lib/mongo/server/connection_pool/generation_manager.rb +6 -11
  414. data/lib/mongo/server/connection_pool/populator.rb +1 -4
  415. data/lib/mongo/server/connection_pool.rb +195 -167
  416. data/lib/mongo/server/description/features.rb +51 -59
  417. data/lib/mongo/server/description/load_balancer.rb +0 -2
  418. data/lib/mongo/server/description.rb +117 -138
  419. data/lib/mongo/server/monitor/app_metadata.rb +3 -4
  420. data/lib/mongo/server/monitor/connection.rb +28 -35
  421. data/lib/mongo/server/monitor.rb +65 -60
  422. data/lib/mongo/server/pending_connection.rb +70 -71
  423. data/lib/mongo/server/push_monitor/connection.rb +0 -3
  424. data/lib/mongo/server/push_monitor.rb +21 -29
  425. data/lib/mongo/server/round_trip_time_calculator.rb +11 -17
  426. data/lib/mongo/server.rb +62 -94
  427. data/lib/mongo/server_selector/base.rb +133 -157
  428. data/lib/mongo/server_selector/nearest.rb +2 -5
  429. data/lib/mongo/server_selector/primary.rb +1 -5
  430. data/lib/mongo/server_selector/primary_preferred.rb +2 -6
  431. data/lib/mongo/server_selector/secondary.rb +2 -6
  432. data/lib/mongo/server_selector/secondary_preferred.rb +1 -5
  433. data/lib/mongo/server_selector.rb +3 -4
  434. data/lib/mongo/session/server_session.rb +6 -7
  435. data/lib/mongo/session/session_pool.rb +20 -34
  436. data/lib/mongo/session.rb +334 -199
  437. data/lib/mongo/socket/ocsp_cache.rb +8 -13
  438. data/lib/mongo/socket/ocsp_verifier.rb +69 -70
  439. data/lib/mongo/socket/ssl.rb +44 -43
  440. data/lib/mongo/socket/tcp.rb +5 -8
  441. data/lib/mongo/socket/unix.rb +0 -4
  442. data/lib/mongo/socket.rb +80 -102
  443. data/lib/mongo/srv/monitor.rb +10 -11
  444. data/lib/mongo/srv/resolver.rb +15 -24
  445. data/lib/mongo/srv/result.rb +25 -21
  446. data/lib/mongo/srv.rb +0 -1
  447. data/lib/mongo/timeout.rb +4 -11
  448. data/lib/mongo/topology_version.rb +8 -13
  449. data/lib/mongo/tracing/open_telemetry/command_tracer.rb +320 -0
  450. data/lib/mongo/tracing/open_telemetry/operation_tracer.rb +227 -0
  451. data/lib/mongo/tracing/open_telemetry/tracer.rb +236 -0
  452. data/lib/mongo/{error/server_api_not_supported.rb → tracing/open_telemetry.rb} +10 -10
  453. data/lib/mongo/tracing.rb +42 -0
  454. data/lib/mongo/uri/options_mapper.rb +135 -126
  455. data/lib/mongo/uri/srv_protocol.rb +34 -42
  456. data/lib/mongo/uri.rb +95 -139
  457. data/lib/mongo/utils.rb +5 -12
  458. data/lib/mongo/version.rb +1 -1
  459. data/lib/mongo/write_concern/acknowledged.rb +0 -2
  460. data/lib/mongo/write_concern/base.rb +6 -6
  461. data/lib/mongo/write_concern/unacknowledged.rb +0 -2
  462. data/lib/mongo/write_concern.rb +14 -15
  463. data/lib/mongo.rb +4 -3
  464. data/mongo.gemspec +17 -17
  465. metadata +11 -5
  466. data/lib/mongo/operation/shared/result/use_legacy_error_parser.rb +0 -32
  467. data/lib/mongo/operation/shared/validatable.rb +0 -87
data/lib/mongo/socket.rb CHANGED
@@ -1,5 +1,4 @@
1
1
  # frozen_string_literal: true
2
- # rubocop:todo all
3
2
 
4
3
  # Copyright (C) 2014-2020 MongoDB Inc.
5
4
  #
@@ -22,7 +21,6 @@ require 'mongo/socket/ocsp_verifier'
22
21
  require 'mongo/socket/ocsp_cache'
23
22
 
24
23
  module Mongo
25
-
26
24
  # Provides additional data around sockets for the driver's use.
27
25
  #
28
26
  # @since 2.0.0
@@ -34,23 +32,23 @@ module Mongo
34
32
  #
35
33
  # @since 2.0.0
36
34
  # @deprecated
37
- SSL_ERROR = 'MongoDB may not be configured with TLS support'.freeze
35
+ SSL_ERROR = 'MongoDB may not be configured with TLS support'
38
36
 
39
37
  # Error message for timeouts on socket calls.
40
38
  #
41
39
  # @since 2.0.0
42
40
  # @deprecated
43
- TIMEOUT_ERROR = 'Socket request timed out'.freeze
41
+ TIMEOUT_ERROR = 'Socket request timed out'
44
42
 
45
43
  # The pack directive for timeouts.
46
44
  #
47
45
  # @since 2.0.0
48
- TIMEOUT_PACK = 'l_2'.freeze
46
+ TIMEOUT_PACK = 'l_2'
49
47
 
50
48
  # Write data to the socket in chunks of this size.
51
49
  #
52
50
  # @api private
53
- WRITE_CHUNK_SIZE = 65536
51
+ WRITE_CHUNK_SIZE = 65_536
54
52
 
55
53
  # Initializes common socket attributes.
56
54
  #
@@ -120,13 +118,17 @@ module Mongo
120
118
  #
121
119
  # @api private
122
120
  def summary
123
- fileno = @socket&.fileno rescue '<no socket>' || '<no socket>'
121
+ fileno = begin
122
+ @socket&.fileno
123
+ rescue StandardError
124
+ '<no socket>'
125
+ end
124
126
  if monitor?
125
127
  indicator = if options[:push]
126
- 'pm'
127
- else
128
- 'm'
129
- end
128
+ 'pm'
129
+ else
130
+ 'm'
131
+ end
130
132
  "#{connection_address};#{indicator};fd=#{fileno}"
131
133
  else
132
134
  "#{connection_address};c:#{connection_generation};fd=#{fileno}"
@@ -143,7 +145,7 @@ module Mongo
143
145
  # @deprecated Use #connectable? on the connection instead.
144
146
  def alive?
145
147
  sock_arr = [ @socket ]
146
- if Kernel::select(sock_arr, nil, sock_arr, 0)
148
+ if Kernel.select(sock_arr, nil, sock_arr, 0)
147
149
  # The eof? call is supposed to return immediately since select
148
150
  # indicated the socket is readable. However, if @socket is a TLS
149
151
  # socket, eof? can block anyway - see RUBY-2140.
@@ -173,7 +175,7 @@ module Mongo
173
175
  ::Timeout.timeout(5) do
174
176
  @socket&.close
175
177
  end
176
- rescue
178
+ rescue StandardError
177
179
  # Silence all errors
178
180
  end
179
181
  true
@@ -213,9 +215,8 @@ module Mongo
213
215
  #
214
216
  # @since 2.0.0
215
217
  def read(length, socket_timeout: nil, timeout: nil)
216
- if !socket_timeout.nil? && !timeout.nil?
217
- raise ArgumentError, 'Both timeout and socket_timeout cannot be set'
218
- end
218
+ raise ArgumentError, 'Both timeout and socket_timeout cannot be set' if !socket_timeout.nil? && !timeout.nil?
219
+
219
220
  if !socket_timeout.nil? || timeout.nil?
220
221
  read_without_timeout(length, socket_timeout)
221
222
  else
@@ -286,13 +287,11 @@ module Mongo
286
287
  String.new.tap do |data|
287
288
  while data.length < length
288
289
  socket_timeout = deadline - Utils.monotonic_time
289
- if socket_timeout <= 0
290
- raise Mongo::Error::TimeoutError
291
- end
290
+ raise Mongo::Error::TimeoutError if socket_timeout <= 0
291
+
292
292
  chunk = read_from_socket(length - data.length, socket_timeout: socket_timeout, csot: true)
293
- unless chunk.length > 0
294
- raise IOError, "Expected to read > 0 bytes but read 0 bytes"
295
- end
293
+ raise IOError, 'Expected to read > 0 bytes but read 0 bytes' unless chunk.length > 0
294
+
296
295
  data << chunk
297
296
  end
298
297
  end
@@ -312,16 +311,14 @@ module Mongo
312
311
  String.new.tap do |data|
313
312
  while data.length < length
314
313
  chunk = read_from_socket(length - data.length, socket_timeout: socket_timeout)
315
- unless chunk.length > 0
316
- raise IOError, "Expected to read > 0 bytes but read 0 bytes"
317
- end
314
+ raise IOError, 'Expected to read > 0 bytes but read 0 bytes' unless chunk.length > 0
315
+
318
316
  data << chunk
319
317
  end
320
318
  end
321
319
  end
322
320
  end
323
321
 
324
-
325
322
  # Reads the +length+ bytes from the socket. The read operation may involve
326
323
  # multiple socket reads, each read is limited to +timeout+ second,
327
324
  # if the parameter is provided.
@@ -333,11 +330,9 @@ module Mongo
333
330
  # @return [ Object ] The data from the socket.
334
331
  def read_from_socket(length, socket_timeout: nil, csot: false)
335
332
  # Just in case
336
- if length == 0
337
- return ''.force_encoding('BINARY')
338
- end
333
+ return ''.force_encoding('BINARY') if length == 0
339
334
 
340
- _timeout = socket_timeout || self.timeout
335
+ _timeout = socket_timeout || timeout
341
336
  if _timeout
342
337
  if _timeout > 0
343
338
  deadline = Utils.monotonic_time + _timeout
@@ -355,9 +350,7 @@ module Mongo
355
350
 
356
351
  # If we want to read less than the buffer size, just allocate the
357
352
  # memory that is necessary
358
- if length < buf_size
359
- buf_size = length
360
- end
353
+ buf_size = length if length < buf_size
361
354
 
362
355
  # The binary encoding is important, otherwise Ruby performs encoding
363
356
  # conversions of some sort during the write into the buffer which
@@ -367,25 +360,19 @@ module Mongo
367
360
  begin
368
361
  while retrieved < length
369
362
  retrieve = length - retrieved
370
- if retrieve > buf_size
371
- retrieve = buf_size
372
- end
363
+ retrieve = buf_size if retrieve > buf_size
373
364
  chunk = @socket.read_nonblock(retrieve, buf)
374
365
 
375
366
  # If we read the entire wanted length in one operation,
376
367
  # return the data as is which saves one memory allocation and
377
368
  # one copy per read
378
- if retrieved == 0 && chunk.length == length
379
- return chunk
380
- end
369
+ return chunk if retrieved == 0 && chunk.length == length
381
370
 
382
371
  # If we are here, we are reading the wanted length in
383
372
  # multiple operations. Allocate the total buffer here rather
384
373
  # than up front so that the special case above won't be
385
374
  # allocating twice
386
- if data.nil?
387
- data = allocate_string(length)
388
- end
375
+ data = allocate_string(length) if data.nil?
389
376
 
390
377
  # ... and we need to copy the chunks at this point
391
378
  data[retrieved, chunk.length] = chunk
@@ -393,40 +380,36 @@ module Mongo
393
380
  end
394
381
  # As explained in https://ruby-doc.com/core-trunk/IO.html#method-c-select,
395
382
  # reading from a TLS socket may require writing which may raise WaitWritable
396
- rescue IO::WaitReadable, IO::WaitWritable => exc
383
+ rescue IO::WaitReadable, IO::WaitWritable => e
397
384
  if deadline
398
385
  select_timeout = deadline - Utils.monotonic_time
399
- if select_timeout <= 0
400
- raise_timeout_error!("Took more than #{_timeout} seconds to receive data", csot)
401
- end
402
- end
403
- if exc.is_a?(IO::WaitReadable)
404
- if pipe
405
- select_args = [[@socket, pipe], nil, [@socket, pipe], select_timeout]
406
- else
407
- select_args = [[@socket], nil, [@socket], select_timeout]
408
- end
409
- else
410
- select_args = [nil, [@socket], [@socket], select_timeout]
386
+ raise_timeout_error!("Took more than #{_timeout} seconds to receive data", csot) if select_timeout <= 0
411
387
  end
388
+ select_args = if e.is_a?(IO::WaitReadable)
389
+ if pipe
390
+ [ [ @socket, pipe ], nil, [ @socket, pipe ], select_timeout ]
391
+ else
392
+ [ [ @socket ], nil, [ @socket ], select_timeout ]
393
+ end
394
+ else
395
+ [ nil, [ @socket ], [ @socket ], select_timeout ]
396
+ end
412
397
 
413
398
  rv = Kernel.select(*select_args)
414
- if Lint.enabled?
415
- if pipe && rv&.include?(pipe)
416
- # If the return value of select is the read end of the pipe, and
417
- # an IOError is not raised, then that means the socket is still
418
- # open. Select is interrupted be closing the write end of the
419
- # pipe, which either returns the pipe if the socket is open, or
420
- # raises an IOError if it isn't. Select is interrupted after all
421
- # of the pending and checked out connections have been interrupted
422
- # and closed, and this only happens once the pool is cleared with
423
- # interrupt_in_use connections flag. This means that in order for
424
- # the socket to still be open when the select is interrupted, and
425
- # that socket is being read from, that means after clear was
426
- # called, a connection from the previous generation was checked
427
- # out of the pool, for reading on its socket. This should be impossible.
428
- raise Mongo::LintError, "Select interrupted for live socket. This should be impossible."
429
- end
399
+ if Lint.enabled? && pipe && rv&.include?(pipe)
400
+ # If the return value of select is the read end of the pipe, and
401
+ # an IOError is not raised, then that means the socket is still
402
+ # open. Select is interrupted be closing the write end of the
403
+ # pipe, which either returns the pipe if the socket is open, or
404
+ # raises an IOError if it isn't. Select is interrupted after all
405
+ # of the pending and checked out connections have been interrupted
406
+ # and closed, and this only happens once the pool is cleared with
407
+ # interrupt_in_use connections flag. This means that in order for
408
+ # the socket to still be open when the select is interrupted, and
409
+ # that socket is being read from, that means after clear was
410
+ # called, a connection from the previous generation was checked
411
+ # out of the pool, for reading on its socket. This should be impossible.
412
+ raise Mongo::LintError, 'Select interrupted for live socket. This should be impossible.'
430
413
  end
431
414
 
432
415
  if BSON::Environment.jruby?
@@ -437,9 +420,7 @@ module Mongo
437
420
  # Check the deadline ourselves.
438
421
  if deadline
439
422
  select_timeout = deadline - Utils.monotonic_time
440
- if select_timeout <= 0
441
- raise_timeout_error!("Took more than #{_timeout} seconds to receive data", csot)
442
- end
423
+ raise_timeout_error!("Took more than #{_timeout} seconds to receive data", csot) if select_timeout <= 0
443
424
  end
444
425
  elsif rv.nil?
445
426
  raise_timeout_error!("Took more than #{_timeout} seconds to receive data (select call timed out)", csot)
@@ -451,20 +432,20 @@ module Mongo
451
432
  end
452
433
 
453
434
  def allocate_string(capacity)
454
- String.new('', :capacity => capacity, :encoding => 'BINARY')
435
+ String.new('', capacity: capacity, encoding: 'BINARY')
455
436
  end
456
437
 
457
438
  def read_buffer_size
458
439
  # Buffer size for non-TLS reads
459
440
  # 64kb
460
- 65536
441
+ 65_536
461
442
  end
462
443
 
463
444
  # Writes data to the socket instance.
464
445
  #
465
446
  # This is a separate method from +write+ for ease of mocking in the tests.
466
447
  # This method should not perform any exception mapping, upstream code
467
- # sholud map exceptions.
448
+ # should map exceptions.
468
449
  #
469
450
  # @param [ Array<Object> ] args The data to be written.
470
451
  # @param [ Numeric ] :timeout The total timeout to the whole write operation.
@@ -478,7 +459,7 @@ module Mongo
478
459
  end
479
460
  end
480
461
 
481
- # Writes data to to the socket.
462
+ # Writes data to the socket.
482
463
  #
483
464
  # @param [ Array<Object> ] args The data to be written.
484
465
  #
@@ -506,7 +487,7 @@ module Mongo
506
487
  end
507
488
  end
508
489
 
509
- # Writes data to to the socket, the write duration is limited to +timeout+.
490
+ # Writes data to the socket, the write duration is limited to +timeout+.
510
491
  #
511
492
  # @param [ Array<Object> ] args The data to be written.
512
493
  # @param [ Numeric ] :timeout The total timeout to the whole write operation.
@@ -514,6 +495,7 @@ module Mongo
514
495
  # @return [ Integer ] The length of bytes written to the socket.
515
496
  def write_with_timeout(*args, timeout:)
516
497
  raise ArgumentError, 'timeout cannot be nil' if timeout.nil?
498
+
517
499
  raise_timeout_error!("Negative timeout #{timeout} given to socket", true) if timeout < 0
518
500
 
519
501
  written = 0
@@ -537,7 +519,7 @@ module Mongo
537
519
  begin
538
520
  written += @socket.write_nonblock(chunk[written..-1])
539
521
  rescue IO::WaitWritable, Errno::EINTR
540
- if !wait_for_socket_to_be_writable(deadline)
522
+ unless wait_for_socket_to_be_writable(deadline)
541
523
  raise_timeout_error!("Took more than #{timeout} seconds to receive data", true)
542
524
  end
543
525
 
@@ -550,7 +532,7 @@ module Mongo
550
532
 
551
533
  def wait_for_socket_to_be_writable(deadline)
552
534
  select_timeout = deadline - Utils.monotonic_time
553
- rv = Kernel.select(nil, [@socket], nil, select_timeout)
535
+ rv = Kernel.select(nil, [ @socket ], nil, select_timeout)
554
536
 
555
537
  if BSON::Environment.jruby?
556
538
  # Ignore the return value of Kernel.select.
@@ -583,7 +565,7 @@ module Mongo
583
565
  set_option(sock, :TCP_KEEPCNT, DEFAULT_TCP_KEEPCNT)
584
566
  set_option(sock, :TCP_KEEPIDLE, DEFAULT_TCP_KEEPIDLE)
585
567
  set_option(sock, :TCP_USER_TIMEOUT, DEFAULT_TCP_USER_TIMEOUT)
586
- rescue
568
+ rescue StandardError
587
569
  # JRuby 9.2.13.0 and lower do not define TCP_KEEPINTVL etc. constants.
588
570
  # JRuby 9.2.14.0 defines the constants but does not allow to get or
589
571
  # set them with this error:
@@ -591,12 +573,12 @@ module Mongo
591
573
  end
592
574
 
593
575
  def set_option(sock, option, default)
594
- if Socket.const_defined?(option)
595
- system_default = sock.getsockopt(IPPROTO_TCP, option).int
596
- if system_default > default
597
- sock.setsockopt(IPPROTO_TCP, option, default)
598
- end
599
- end
576
+ return unless Socket.const_defined?(option)
577
+
578
+ system_default = sock.getsockopt(IPPROTO_TCP, option).int
579
+ return unless system_default > default
580
+
581
+ sock.setsockopt(IPPROTO_TCP, option, default)
600
582
  end
601
583
 
602
584
  def set_socket_options(sock)
@@ -605,15 +587,13 @@ module Mongo
605
587
  end
606
588
 
607
589
  def map_exceptions
608
- begin
609
- yield
610
- rescue Errno::ETIMEDOUT => e
611
- raise Error::SocketTimeoutError, "#{e.class}: #{e} (for #{human_address})"
612
- rescue IOError, SystemCallError, ::SocketError => e
613
- raise Error::SocketError, "#{e.class}: #{e} (for #{human_address})"
614
- rescue OpenSSL::SSL::SSLError => e
615
- raise Error::SocketError, "#{e.class}: #{e} (for #{human_address})"
616
- end
590
+ yield
591
+ rescue Errno::ETIMEDOUT => e
592
+ raise Error::SocketTimeoutError, "#{e.class}: #{e} (for #{human_address})"
593
+ rescue IOError, SystemCallError, ::SocketError => e
594
+ raise Error::SocketError, "#{e.class}: #{e} (for #{human_address})"
595
+ rescue OpenSSL::SSL::SSLError => e
596
+ raise Error::SocketError, "#{e.class}: #{e} (for #{human_address})"
617
597
  end
618
598
 
619
599
  def human_address
@@ -621,11 +601,9 @@ module Mongo
621
601
  end
622
602
 
623
603
  def raise_timeout_error!(message = nil, csot = false)
624
- if csot
625
- raise Mongo::Error::TimeoutError
626
- else
627
- raise Errno::ETIMEDOUT, message
628
- end
604
+ raise Mongo::Error::TimeoutError if csot
605
+
606
+ raise Errno::ETIMEDOUT, message
629
607
  end
630
608
  end
631
609
  end
@@ -1,5 +1,4 @@
1
1
  # frozen_string_literal: true
2
- # rubocop:todo all
3
2
 
4
3
  # Copyright (C) 2014-2020 MongoDB Inc.
5
4
  #
@@ -17,7 +16,6 @@
17
16
 
18
17
  module Mongo
19
18
  module Srv
20
-
21
19
  # Periodically retrieves SRV records for the cluster's SRV URI, and
22
20
  # sets the cluster's server list to the SRV lookup result.
23
21
  #
@@ -44,18 +42,15 @@ module Mongo
44
42
  # Options to pass through to Resolv::DNS constructor for SRV lookups.
45
43
  def initialize(cluster, **opts)
46
44
  @cluster = cluster
47
- unless @srv_uri = opts.delete(:srv_uri)
48
- raise ArgumentError, 'SRV URI is required'
49
- end
45
+ raise ArgumentError, 'SRV URI is required' unless @srv_uri = opts.delete(:srv_uri)
46
+
50
47
  @options = opts.freeze
51
48
  @resolver = Srv::Resolver.new(**opts)
52
49
  @last_result = @srv_uri.srv_result
53
50
  @stop_semaphore = Semaphore.new
54
51
  end
55
52
 
56
- attr_reader :options
57
-
58
- attr_reader :cluster
53
+ attr_reader :options, :cluster
59
54
 
60
55
  # @return [ Srv::Result ] Last known SRV lookup result. Used for
61
56
  # determining intervals between SRV lookups, which depend on SRV DNS
@@ -72,7 +67,11 @@ module Mongo
72
67
  def scan!
73
68
  begin
74
69
  last_result = Timeout.timeout(timeout) do
75
- @resolver.get_records(@srv_uri.query_hostname)
70
+ @resolver.get_records(
71
+ @srv_uri.query_hostname,
72
+ @srv_uri.uri_options[:srv_service_name] || options[:srv_service_name],
73
+ @srv_uri.uri_options[:srv_max_hosts] || @options[:srv_max_hosts]
74
+ )
76
75
  end
77
76
  rescue Resolv::ResolvTimeout => e
78
77
  log_warn("SRV monitor: timed out trying to resolve hostname #{@srv_uri.query_hostname}: #{e.class}: #{e}")
@@ -95,11 +94,11 @@ module Mongo
95
94
 
96
95
  def scan_interval
97
96
  if last_result.empty?
98
- [cluster.heartbeat_interval, MIN_SCAN_INTERVAL].min
97
+ [ cluster.heartbeat_interval, MIN_SCAN_INTERVAL ].min
99
98
  elsif last_result.min_ttl.nil?
100
99
  MIN_SCAN_INTERVAL
101
100
  else
102
- [last_result.min_ttl, MIN_SCAN_INTERVAL].max
101
+ [ last_result.min_ttl, MIN_SCAN_INTERVAL ].max
103
102
  end
104
103
  end
105
104
 
@@ -1,5 +1,4 @@
1
1
  # frozen_string_literal: true
2
- # rubocop:todo all
3
2
 
4
3
  # Copyright (C) 2017-2020 MongoDB Inc.
5
4
  #
@@ -17,7 +16,6 @@
17
16
 
18
17
  module Mongo
19
18
  module Srv
20
-
21
19
  # Encapsulates the necessary behavior for querying SRV records as
22
20
  # required by the driver.
23
21
  #
@@ -27,7 +25,7 @@ module Mongo
27
25
 
28
26
  # @return [ String ] RECORD_PREFIX The prefix prepended to each hostname
29
27
  # before querying SRV records.
30
- RECORD_PREFIX = '_mongodb._tcp.'.freeze
28
+ RECORD_PREFIX = '_mongodb._tcp.'
31
29
 
32
30
  # Generates the record prefix with a custom SRV service name if it is
33
31
  # provided.
@@ -35,8 +33,8 @@ module Mongo
35
33
  # @option srv_service_name [ String | nil ] The SRV service name to use
36
34
  # in the record prefix.
37
35
  # @return [ String ] The generated record prefix.
38
- def record_prefix(srv_service_name=nil)
39
- return srv_service_name ? "_#{srv_service_name}._tcp." : RECORD_PREFIX
36
+ def record_prefix(srv_service_name = nil)
37
+ srv_service_name ? "_#{srv_service_name}._tcp." : RECORD_PREFIX
40
38
  end
41
39
 
42
40
  # Creates a new Resolver.
@@ -81,7 +79,7 @@ module Mongo
81
79
  # option is true and no records are found.
82
80
  #
83
81
  # @return [ Mongo::Srv::Result ] SRV lookup result.
84
- def get_records(hostname, srv_service_name=nil, srv_max_hosts=nil)
82
+ def get_records(hostname, srv_service_name = nil, srv_max_hosts = nil)
85
83
  query_name = record_prefix(srv_service_name) + hostname
86
84
  resources = @resolver.getresources(query_name, Resolv::DNS::Resource::IN::SRV)
87
85
 
@@ -91,30 +89,25 @@ module Mongo
91
89
  # added to the Result object.
92
90
  result = Srv::Result.new(hostname)
93
91
  resources.each do |record|
94
- begin
95
- result.add_record(record)
96
- rescue Error::MismatchedDomain => e
97
- if raise_on_invalid?
98
- raise
99
- else
100
- log_warn(e.message)
101
- end
102
- end
92
+ result.add_record(record)
93
+ rescue Error::MismatchedDomain => e
94
+ raise if raise_on_invalid?
95
+
96
+ log_warn(e.message)
103
97
  end
104
98
 
105
99
  # If no records are found, either raise an error or log a warning
106
100
  # based on the Resolver's :raise_on_invalid option.
107
101
  if result.empty?
108
- if raise_on_invalid?
109
- raise Error::NoSRVRecords.new(URI::SRVProtocol::NO_SRV_RECORDS % hostname)
110
- else
111
- log_warn(URI::SRVProtocol::NO_SRV_RECORDS % hostname)
112
- end
102
+ raise Error::NoSRVRecords.new(URI::SRVProtocol::NO_SRV_RECORDS % hostname) if raise_on_invalid?
103
+
104
+ log_warn(URI::SRVProtocol::NO_SRV_RECORDS % hostname)
105
+
113
106
  end
114
107
 
115
108
  # if srv_max_hosts is in [1, #addresses)
116
109
  if (1...result.address_strs.length).include? srv_max_hosts
117
- sampled_records = resources.shuffle.first(srv_max_hosts)
110
+ sampled_records = resources.sample(srv_max_hosts)
118
111
  result = Srv::Result.new(hostname)
119
112
  sampled_records.each { |record| result.add_record(record) }
120
113
  end
@@ -131,9 +124,7 @@ module Mongo
131
124
  # @raise [ Mongo::Error::InvalidTXTRecord ] If more than one TXT record is found.
132
125
  def get_txt_options_string(hostname)
133
126
  records = @resolver.getresources(hostname, Resolv::DNS::Resource::IN::TXT)
134
- if records.empty?
135
- return nil
136
- end
127
+ return nil if records.empty?
137
128
 
138
129
  if records.length > 1
139
130
  msg = "Only one TXT record is allowed: querying hostname #{hostname} returned #{records.length} records"
@@ -1,5 +1,4 @@
1
1
  # frozen_string_literal: true
2
- # rubocop:todo all
3
2
 
4
3
  # Copyright (C) 2017-2020 MongoDB Inc.
5
4
  #
@@ -17,7 +16,6 @@
17
16
 
18
17
  module Mongo
19
18
  module Srv
20
-
21
19
  # SRV record lookup result.
22
20
  #
23
21
  # Contains server addresses that the query resolved to, and minimum TTL
@@ -29,8 +27,8 @@ module Mongo
29
27
 
30
28
  # @return [ String ] MISMATCHED_DOMAINNAME Error message format string indicating that an SRV
31
29
  # record found does not match the domain of a hostname.
32
- MISMATCHED_DOMAINNAME = "Parent domain name in SRV record result (%s) does not match " +
33
- "that of the hostname (%s)".freeze
30
+ MISMATCHED_DOMAINNAME = 'Parent domain name in SRV record result (%s) does not match ' +
31
+ 'that of the hostname (%s)'
34
32
 
35
33
  # @return [ String ] query_hostname The hostname pointing to the DNS records.
36
34
  attr_reader :query_hostname
@@ -68,18 +66,18 @@ module Mongo
68
66
  validate_hostname!(record_host)
69
67
  validate_same_origin!(record_host)
70
68
  address_str = if record_host.index(':')
71
- # IPV6 address
72
- "[#{record_host}]:#{port}"
73
- else
74
- "#{record_host}:#{port}"
75
- end
69
+ # IPV6 address
70
+ "[#{record_host}]:#{port}"
71
+ else
72
+ "#{record_host}:#{port}"
73
+ end
76
74
  @address_strs << address_str
77
75
 
78
- if @min_ttl.nil?
79
- @min_ttl = record.ttl
80
- else
81
- @min_ttl = [@min_ttl, record.ttl].min
82
- end
76
+ @min_ttl = if @min_ttl.nil?
77
+ record.ttl
78
+ else
79
+ [ @min_ttl, record.ttl ].min
80
+ end
83
81
 
84
82
  nil
85
83
  end
@@ -99,9 +97,7 @@ module Mongo
99
97
  # @param [ String ] host Hostname to transform.
100
98
  def normalize_hostname(host)
101
99
  host = host.downcase
102
- unless host.end_with?('..')
103
- host = host.sub(/\.\z/, '')
104
- end
100
+ host = host.delete_suffix('.') unless host.end_with?('..')
105
101
  host
106
102
  end
107
103
 
@@ -111,16 +107,24 @@ module Mongo
111
107
  # parts after the first. For example, the hostname 'foo.bar.baz'
112
108
  # has the domain name 'bar.baz'.
113
109
  #
110
+ # If the hostname has less than three parts, its domain name is the hostname itself.
111
+ #
114
112
  # @param [ String ] record_host The host of the SRV record.
115
113
  #
116
114
  # @raise [ Mongo::Error::MismatchedDomain ] If the record's domain name doesn't match that of
117
115
  # the hostname.
118
116
  def validate_same_origin!(record_host)
119
- domain_name ||= query_hostname.split('.')[1..-1]
120
- host_parts = record_host.split('.')
117
+ srv_host_domain = query_hostname.split('.')
118
+ srv_is_less_than_three_parts = srv_host_domain.length < 3
119
+ srv_host_domain = srv_host_domain[1..-1] unless srv_is_less_than_three_parts
120
+ record_host_parts = record_host.split('.')
121
+
122
+ if srv_is_less_than_three_parts && record_host_parts.length <= srv_host_domain.length
123
+ raise Error::MismatchedDomain.new(format(MISMATCHED_DOMAINNAME, record_host, srv_host_domain))
124
+ end
121
125
 
122
- unless (host_parts.size > domain_name.size) && (domain_name == host_parts[-domain_name.length..-1])
123
- raise Error::MismatchedDomain.new(MISMATCHED_DOMAINNAME % [record_host, domain_name])
126
+ unless (record_host_parts.size > srv_host_domain.size) && (srv_host_domain == record_host_parts[-srv_host_domain.size..-1])
127
+ raise Error::MismatchedDomain.new(format(MISMATCHED_DOMAINNAME, record_host, srv_host_domain))
124
128
  end
125
129
  end
126
130
  end
data/lib/mongo/srv.rb CHANGED
@@ -1,5 +1,4 @@
1
1
  # frozen_string_literal: true
2
- # rubocop:todo all
3
2
 
4
3
  # Copyright (C) 2019-2020 MongoDB Inc.
5
4
  #