类:Mongo::Server::ConnectionPool
- 继承:
-
对象
- 对象
- Mongo::Server::ConnectionPool
- 扩展方式:
- 可转发
- 定义于:
- lib/ Mongo/ 服务器/connection_pool.rb 、
lib/ Mongo/ 服务器/connection_pool/populator.rb、
lib/ Mongo/ 服务器/connection_pool/Generation_manager.rb
Overview
表示服务器连接的连接池。
在命名空间下定义
类: GenerationManager , Populator
常量摘要折叠
- DEFAULT_MAX_SIZE =
连接池的默认最大大小。
20
- DEFAULT_MIN_SIZE =
连接池的默认最小大小。
0
- DEFAULT_MAX_CONNECTING =
任何给定时间可以连接的默认最大连接数。
2
- DEFAULT_WAIT_TIMEOUT =
等待连接的默认超时时间(以秒为单位)。
此超时适用于流中线程等待后台线程建立连接(因此它们必须在分配的时间内进行连接、握手和身份验证)的情况。
目前设置为10秒。 默认连接超时为10秒,但设置较大的超时可能会给应用程序带来麻烦,如果反向代理导致应用程序的请求超时,因此超过15秒的任何操作都存在潜在危险。
10.冻结
Loggable中包含的常量
实例属性摘要折叠
-
#Generation_manager ⇒ 整数
只读
private
生成 队列当前使用的连接的生成。
- # max_connecting ⇒ 对象 只读 private
-
# options ⇒ 哈希
只读
选项 池选项。
-
#populate_semaphore ⇒ 对象
只读
条件变量在池大小更改时进行广播以唤醒填充器。
- #填充器⇒ 对象 只读 private
- # server ⇒ 对象 只读 private
包含在Monitoring::Publishable 中的属性
类方法摘要折叠
实例方法摘要折叠
-
# available_count ⇒ 整数
池中的可用连接数。
-
# check_in (connection) ⇒ 对象
将连接重新签入池中。
-
# check_out (connection_global_id: nil, context: nil) ⇒ Mongo::Server::Connection
从池中检查连接。
-
# clear (options = nil) ⇒ true
关闭池中的所有空闲连接,并安排在当前检出的连接重新检入池中时将其关闭。
-
# close (options = nil) ⇒ true
将池标记为已关闭,关闭池中的所有空闲连接,并安排在当前签出的连接重新签入池中时将其关闭。
-
# close_idle_sockets ⇒ 对象
如果设置了该选项,则关闭打开时间超过最大空闲时间的套接字。
-
#已关闭? ⇒ true | false
池是否已关闭。
-
#断开连接! (options = nil) ⇒ 对象
private
断开与池的连接。
-
# do_check_in (connection) ⇒ 对象
在获取锁后执行签入。
- # do_clear (options = nil) ⇒ 对象
-
# do_pause ⇒ 对象
private
在不获取锁的情况下将连接池标记为已暂停。
-
#initialize (server, options = {}) ⇒ ConnectionPool
构造函数
创建新的连接池。
-
#检查⇒ string
为池获取打印美观的string检查。
-
# max_idle_time ⇒ Float | nil
套接字自签入池以来可以保持空闲状态的最大秒数(如果已设置)。
-
# max_size ⇒ 整数
获取连接池的最大大小。
-
# min_size ⇒ 整数
获取连接池的最小大小。
-
# pause ⇒ 对象
将连接池标记为已暂停。
-
#已暂停? ⇒ true | false
如果连接池未关闭且未准备就绪,则连接池将暂停。
-
#填充="true" | false
private
此方法执行三件事: 1 。
-
#准备就绪⇒ 对象
指示池创建并返回连接。
-
#准备好了吗? ⇒ true | false
池是否已准备就绪。
-
#size ⇒ Integer
连接池的大小。
-
# stop_populator ⇒ 对象
private
停止后台填充器线程并清理已创建但尚未连接的所有连接。
- #摘要⇒ 对象
-
# Unavailable_connections ="Integer"(整数)
private
池中不可用连接的数量。
-
#wait_timeout(context = nil) ⇒ Float
等待连接变为可用的时间(以秒为单位)。
-
# with_connection (connection_global_id: nil, context: nil) ⇒ 对象
在处理签入/签出逻辑时,将区块让出给连接。
Monitoring::Publishable 中包含的方法
#publish_cmap_event 、 #publish_event 、 #publish_sdam_event
Loggable中包含的方法
#log_debug 、 #log_error 、 #log_ Fatal 、 #log_info 、 #log_warn 、 #logger
构造函数详情
#initialize (服务器, options = {}) ⇒ ConnectionPool
创建新的连接池。
注意:此外,此池创建的连接的选项应
be included in the options passed here, and they will be forwarded to
any connections created by the pool.
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/ Mongo/ 服务器/connection_pool.rb', line 102 def 初始化(server, = {}) 除非 server.is_a?(服务器) 提高 ArgumentError, '第一个参数必须是服务器实例' end = .dup if [:min_size] && [:min_pool_size] && [:min_size] != [:min_pool_size] 提高 ArgumentError, "最小大小#{ [ :min_size ] }与最小池大小#{ [ :min_pool_size ] }不同" end if [:max_size] && [:max_pool_size] && [:max_size] != [:max_pool_size] 提高 ArgumentError, " Max size #{ [ :max_size ] }与 max pool size #{ [ :max_pool_size ] }不同" end if [:wait_timeout] && [:wait_queue_timeout] && [:wait_timeout] != [:wait_queue_timeout] 提高 ArgumentError, "等待超时#{ [ :wait_timeout ] }与等待队列超时#{ [ :wait_queue_timeout ] }不同" end [:min_size] ||= [:min_pool_size] .删除(:min_pool_size) [:max_size] ||= [:max_pool_size] .删除(:max_pool_size) if [:min_size] && [:max_size] && ([:max_size] != 0 && [:min_size] > [:max_size]) then 提高 ArgumentError, "最小大小#{ [ :min_size ] }不能超过最大大小#{ [ :max_size ] } " end if [:wait_queue_timeout] [:wait_timeout] ||= [:wait_queue_timeout] end .删除(:wait_queue_timeout) @server = server @options = .冻结 @generation_manager = GenerationManager.new(服务器: server) @ready = false @close = false # 此池拥有的连接应位于 # available 连接大量(用作堆栈) # 或在已签出的连接设立。 @available_connections = available_connections = [] @checked_out_connections = 集.new @pending_connections = 集.new @interrupt_connections = [] # 用于同步访问 @available_connections 和 # @checked_out_connections. 池对象是线程安全的,因此 # 通常检索或修改实例变量的所有方法 # 必须在此锁下执行此操作。 锁 = 互斥锁.new # 后台线程负责维护 # 池化至少为 min_size @populator = 填充器.new(self, ) @populate_semaphore = 信号量.new # 在 check_out: max_pool_size 中实施首次检查的条件变量。 # 该条件变量应在以下情况下发出信号: # 不可用连接数减少 (pending + pending_connections + #checked_out_connections)。 @size_cv = mongo::ConditionVariable.new(锁) # 这表示已超过 size_cv 的线程数 # Gate,但尚未获取可添加到待连接的连接 # 设立. @connection_requests = 0 # 在 check_out: max_connecting 中执行第二次检查的条件变量。 # 当待处理的数量达到 # 个连接减少。 @max_connecting_cv = mongo::ConditionVariable.new(锁) @max_connecting = .获取(:max_connecting, DEFAULT_MAX_CONNECTING) ObjectSpace.define_finalizer(self, self.class.finalize(@available_connections, @pending_connections, @populator)) publish_cmap_event( 监控::事件::Cmap::PoolCreated.new(@server.地址, , self) ) end |
实例属性详细信息
#Generation_manager ⇒整数(只读)
此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。
返回生成队列当前使用的连接的生成数。
233 234 235 |
# File 'lib/ Mongo/ 服务器/connection_pool.rb', line 233 def Generation_manager @generation_manager end |
# max_connecting ⇒对象(只读)
此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。
342 343 344 |
# File 'lib/ Mongo/ 服务器/connection_pool.rb', line 342 def max_connecting @max_connecting end |
# options ⇒哈希(只读)
返回 options 池选项。
180 181 182 |
# File 'lib/ Mongo/ 服务器/connection_pool.rb', line 180 def @options end |
#populate_semaphore ⇒对象(只读)
条件变量在池大小发生变化时进行广播,以唤醒填充器
59 60 61 |
# File 'lib/ Mongo/ 服务器/connection_pool.rb', line 59 def 填充信号量 @populate_semaphore end |
#填充器⇒对象(只读)
此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。
339 340 341 |
# File 'lib/ Mongo/ 服务器/connection_pool.rb', line 339 def 填充器 @populator end |
# server ⇒对象(只读)
此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。
183 184 185 |
# File 'lib/ Mongo/ 服务器/connection_pool.rb', line 183 def server @server end |
类方法详细信息
。 finalize (available_connections,pending_connections,populator) ⇒ Proc
完成用于垃圾收集的连接池。
813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 |
# File 'lib/ Mongo/ 服务器/connection_pool.rb', line 813 def self.finalize(available_connections, 挂起的连接, 填充器) proc do available_connections.每 do |连接| 连接.断开连接!(原因: :pool_close) end available_connections.清除 挂起的连接.每 do |连接| 连接.断开连接!(原因: :pool_close) end 挂起的连接.清除 # 终结器不会关闭已检出的连接。 # 这些必须自行进行垃圾收集 # 这应该会关闭它们。 end end |
实例方法详细信息
# available_count ⇒整数
池中的可用连接数。
291 292 293 294 295 296 297 |
# File 'lib/ Mongo/ 服务器/connection_pool.rb', line 291 def available_count ise_if_close! 锁.同步 do @available_connections.长度 end end |
# check_in (connection) ⇒对象
将连接重新签入池中。
该连接必须是该连接池之前创建的。
402 403 404 405 406 407 408 409 410 |
# File 'lib/ Mongo/ 服务器/connection_pool.rb', line 402 def check_in(连接) check_invariants 锁.同步 do do_check_in(连接) end 确保 check_invariants end |
# check_out (connection_global_id: nil, context: nil) ⇒ Mongo::Server::Connection
从池中检查连接。
如果池中有活动连接,则返回最近使用的连接。 否则,如果连接池大小小于最大大小,则创建一个新连接并将其返回。 否则,等待直至等待超时,如果仍然没有活动连接并且连接池达到最大大小,则引发 Timeout::Error。
返回的连接计入池的最大大小。 当调用者使用完连接后,应通过 check_in 方法重新签入连接。
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 |
# File 'lib/ Mongo/ 服务器/connection_pool.rb', line 366 def check_out(connection_global_id: nil, 上下文: nil) check_invariants publish_cmap_event( 监控::事件::Cmap::connectionCheckOutStarted.new(@server.地址) ) Raise_if_Pool_Closed! Raise_if_pool_paused_locked! 连接 = retrieve_and_connect_connection( connection_global_id, 上下文 ) publish_cmap_event( 监控::事件::Cmap::connectionCheckedOut.new(@server.地址, 连接.id, self), ) if Lint.已启用? 除非 连接.已连接? 提高 错误::LintError, " #{ 解决} 的 连接池已 签出断开连接 #{ connection.Generation}: #{ IDconnection.} " end end 连接 确保 check_invariants end |
# clear (options = nil) ⇒ true
关闭池中的所有空闲连接,并安排在当前检出的连接重新检入池中时将其关闭。 池已暂停,不会在背景创建新连接,并且在标记为就绪之前,检出请求将失败。
520 521 522 523 524 525 526 527 528 |
# File 'lib/ Mongo/ 服务器/connection_pool.rb', line 520 def 清除( = nil) ise_if_close! if Lint.已启用? && !@server.未知? 提高 错误::LintError, “ 正在尝试服务器 已知 的服务器 #{ @ 服务器.summary} 的池” end do_clear() end |
# close (options = nil) ⇒ true
将池标记为已关闭,关闭池中的所有空闲连接,并安排在当前签出的连接重新签入池中时将其关闭。 如果 force 选项为 true,则签出的连接也会关闭。 在池关闭后尝试使用池将引发 Error::PoolClosedError。
644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 |
# File 'lib/ Mongo/ 服务器/connection_pool.rb', line 644 def 关闭( = nil) return if 已关闭? ||= {} stop_populator 锁.同步 do 直到 @available_connections.空? 连接 = @available_connections.Pop 连接.断开连接!(原因: :pool_close) end if [:force] 直到 @checked_out_connections.空? 连接 = @checked_out_connections.采取(1).first 连接.断开连接!(原因: :pool_close) @checked_out_connections.删除(连接) end end 除非 && [:stay_ready] # 在释放锁之前将池标记为已关闭,因此 # 无法创建、签入或签出任何连接 @close = true @ready = false end @max_connecting_cv.广播 @size_cv.广播 end publish_cmap_event( 监控::事件::Cmap::PoolClosed.new(@server.地址, self) ) true end |
# close_idle_sockets ⇒对象
关闭打开时间超过最大空闲时间的套接字,
if the option is set.
734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 |
# File 'lib/ Mongo/ 服务器/connection_pool.rb', line 734 def close_idle_sockets return if 已关闭? return 除非 max_idle_time 锁.同步 do i = 0 while i < @available_connections.长度 连接 = @available_connections[i] if last_checkin = 连接.last_checkin if (时间.now - last_checkin) > max_idle_time 连接.断开连接!(原因: :idle) @available_connections.delete_at(i) @populate_semaphore.信号 来年 end end i += 1 end end end |
#已关闭? ⇒ true | false
池是否已关闭。
304 305 306 |
# File 'lib/ Mongo/ 服务器/connection_pool.rb', line 304 def 已关闭? !!@close end |
#断开连接! (options = nil) ⇒对象
此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。
断开与池的连接。
执行clear
执行的所有操作,但如果池已关闭,则此方法不执行任何操作,但clear
会引发 PoolClosedError。
537 538 539 540 541 542 543 |
# File 'lib/ Mongo/ 服务器/connection_pool.rb', line 537 def 断开连接!( = nil) do_clear() 救援 错误::PoolClosedError # “断开连接”状态介于关闭和暂停之间。 # 当我们尝试断开与池的连接时,允许池 # 已经关闭。 end |
# do_check_in (connection) ⇒对象
在获取锁后执行签入。
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 |
# File 'lib/ Mongo/ 服务器/connection_pool.rb', line 415 def do_check_in(连接) # 当连接中断时,会重新签入连接池 # 并关闭。 在该连接被删除之前正在使用该连接的操作 # 中断将尝试将其重新签入池中,并且我们 # 应忽略它,因为它已被关闭并从池中删除。 return if 连接.已关闭? && 连接.中断? 除非 连接.connection_pool == self 提高 ArgumentError, "正在尝试签入未由此池签出的连接: #{ connection }从池中签出#{ connection . connection_pool } (for #{ self } ) " end 除非 @checked_out_connections.包括?(连接) 提高 ArgumentError, "正在尝试签入此池当前未签出的连接: #{ connection } (for #{ self } ) " end # 注意:如果引发事件处理程序,则不会向资源发出信号。 # 这意味着线程等待连接释放 # 池大小达到最大可能会超时。 # 在此方法完成后开始等待的线程(使用 # 异常)应该没问题。 @checked_out_connections.删除(连接) @size_cv.信号 publish_cmap_event( 监控::事件::Cmap::connectionCheckedIn.new(@server.地址, 连接.id, self) ) if 连接.中断? 连接.断开连接!(原因: :stale) return end if 连接.错误? 连接.断开连接!(原因: :error) return end if 已关闭? 连接.断开连接!(原因: :pool_close) return end if 连接.已关闭? # 连接已关闭 -示例,因为它经历 # 网络错误。 这里不需要执行任何其他操作。 @populate_semaphore.信号 elsif 连接.生成 != 生成(service_id: 连接.service_id) && !连接.已固定? # 如果连接被标记为固定,则由ACID 事务使用 # 或负载均衡设置中的一系列游标操作。 # 在这种情况下,不应断开连接,直到 # 取消固定。 连接.断开连接!(原因: :stale) @populate_semaphore.信号 else 连接.record_checkin! @available_connections << 连接 @max_connecting_cv.信号 end end |
# do_clear (options = nil) ⇒对象
545 546 547 548 549 550 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 |
# File 'lib/ Mongo/ 服务器/connection_pool.rb', line 545 def do_clear( = nil) check_invariants service_id = && [:service_id] 锁.同步 do # 在发出池清除事件之前,必须先更新生成。 @generation_manager.碰撞(service_id: service_id) 除非 && [:lazy] close_available_connections(service_id) end if && [:interrupt_in_use_connections] schedule_for_interruption(@checked_out_connections, service_id) schedule_for_interruption(@pending_connections, service_id) end if @ready publish_cmap_event( 监控::事件::Cmap::PoolCleared.new( @server.地址, service_id: service_id, Interrupt_in_use_connections: &。[ ](:interrupt_in_use_connections) ) ) # 仅当服务器被标记为未知时才暂停连接池, # 否则,允许使用就绪池尝试重试。 do_pause if !@server.load_balancer? && @server.未知? end # 在此处进行广播以使所有线程在 max # 连接以跳出等待循环和错误。 @max_connecting_cv.广播 # 在此处进行广播以使所有线程等待大小池 # 跳出等待循环和错误。 @size_cv.广播 end # 清除后“安排后台线程”。 这是负责 # 用于清理过时的线程,并中断正在使用的连接。 @populate_semaphore.信号 true 确保 check_invariants end |
# do_pause ⇒对象
此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。
在不获取锁的情况下将连接池标记为已暂停。
493 494 495 496 497 498 499 500 501 |
# File 'lib/ Mongo/ 服务器/connection_pool.rb', line 493 def do_pause if Lint.已启用? && !@server.未知? 提高 错误::LintError, " 正在尝试暂停 已知 的服务器 #{ @server .summary } } 的 池 " end return if !@ready @ready = false end |
#检查⇒ string
为池获取打印美观的string检查。
691 692 693 694 695 696 697 698 699 700 701 702 |
# File 'lib/ Mongo/ 服务器/connection_pool.rb', line 691 def 检查 if 已关闭? " #<Mongo::Server::ConnectionPool: 0 x #{ object_id } min_size= #{ min_size } max_size= #{ max_size } " + " wait_timeout= #{ wait_timeout } completed > " elsif !准备好了吗? " #<Mongo::Server::ConnectionPool: 0 x #{ object_id } min_size= #{ min_size } max_size= #{ max_size } " + " wait_timeout= #{ wait_timeout } Paused> " else " #<Mongo::Server::ConnectionPool: 0 x #{ object_id } min_size= #{ min_size } max_size= #{ max_size } " + " wait_timeout= #{ wait_timeout } current_size= #{ size } available= #{ available_count } > " end end |
# max_idle_time ⇒ Float | nil
套接字自签入池以来可以保持空闲状态的最大秒数(如果已设置)。
228 229 230 |
# File 'lib/ Mongo/ 服务器/connection_pool.rb', line 228 def max_idle_time @max_idle_time ||= [:max_idle_time] end |
#max_size ⇒ Integer
获取连接池的最大大小。
193 194 195 |
# File 'lib/ Mongo/ 服务器/connection_pool.rb', line 193 def max_size @max_size ||= [:max_size] || [DEFAULT_MAX_SIZE, min_size].Max end |
#min_size ⇒ Integer
获取连接池的最小大小。
202 203 204 |
# File 'lib/ Mongo/ 服务器/connection_pool.rb', line 202 def min_size @min_size ||= [:min_size] || DEFAULT_MIN_SIZE end |
# pause ⇒对象
将连接池标记为已暂停。
478 479 480 481 482 483 484 485 486 487 488 |
# File 'lib/ Mongo/ 服务器/connection_pool.rb', line 478 def 暂停 ise_if_close! check_invariants 锁.同步 do do_pause end 确保 check_invariants end |
#已暂停? ⇒ true | false
如果连接池未关闭且未准备就绪,则连接池将暂停。
246 247 248 249 250 251 252 |
# File 'lib/ Mongo/ 服务器/connection_pool.rb', line 246 def 已暂停? ise_if_close! 锁.同步 do !@ready end end |
#填充=" true" | false
此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。
此方法执行三件事:
-
如果池的大小低于 min_size,则创建连接并将其添加到池中。 如果在此进程中遇到与套接字相关的错误,则重试一次;如果出现第二个错误或与套接字无关的错误,则引发此错误。
-
从连接池中删除过时的连接。
-
中断标记为中断的连接。
由池填充器后台线程使用。
发生,或非套接字相关错误
792 793 794 795 796 797 798 799 800 801 802 803 804 |
# File 'lib/ Mongo/ 服务器/connection_pool.rb', line 792 def 填充 return false if 已关闭? 开始 return create_and_add_connection 救援 错误::SocketError, 错误::SocketTimeoutError => e # 连接时遇到错误, # 忽略第一个错误,然后重试。 log_warn(" Populator 无法为#{ 解决 } : #{ e . class } : #{ e }连接。它将重试。 ") end return create_and_add_connection end |
#准备就绪⇒对象
指示池创建并返回连接。
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 |
# File 'lib/ Mongo/ 服务器/connection_pool.rb', line 593 def 准备就绪 ise_if_close! # TODO:将其重新添加到 RUBY- 3174中。 # if Lint.enabled? #除非@ 服务器.connected? #引发错误::LintError,“尝试为服务器#{@ 服务器.summary}准备一个池 已断开连接” # end # end 锁.同步 do return if @ready @ready = true end # 请注意,CMAP 规范要求对 CMAP 事件进行序列化 # 池。 为此,必须将事件发布到 # 一个同步的队列,而不是调用订阅者 # 来自像这样的trigger方法 inline。 在 MRI 上,假设 # 当这些线程不再有工作要做时,就会让位于其他线程,即 # 实际上,事件很可能始终发布在 # 所需顺序。 JRuby 真正与操作系统线程并发, # 不会提供这样的保证。 publish_cmap_event( 监控::事件::Cmap::PoolReady.new(@server.地址, , self) ) if .获取(:populator_io, true) if @populator.运行? @populate_semaphore.信号 else @populator.运行! end end end |
#准备好了吗? ⇒ true | false
池是否已准备就绪。
311 312 313 314 315 |
# File 'lib/ Mongo/ 服务器/connection_pool.rb', line 311 def 准备好了吗? 锁.同步 do @ready end end |
# size =" Integer "(整数)
连接池的大小。
包括可用连接和已签出连接。
261 262 263 264 265 266 267 |
# File 'lib/ Mongo/ 服务器/connection_pool.rb', line 261 def size ise_if_close! 锁.同步 do unsynchronized_size end end |
# stop_populator ⇒对象
此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。
停止后台填充器线程并清理已创建但尚未连接的所有连接。
在关闭池或出于测试目的而终止背景线程时使用。 在后一种情况下,必须在使用池之前调用此方法,以确保未决连接中的连接不是由 check_out 方法创建的。
764 765 766 767 768 769 770 771 772 773 774 |
# File 'lib/ Mongo/ 服务器/connection_pool.rb', line 764 def stop_populator @populator.停止! 锁.同步 do # 如果在填充运行时调用 stop_populator,则可能会 # 等待连接的连接数、尚未连接的连接数 #被移动到available_connections,或连接移动到available_connections # 但不会从挂起的连接中删除。 这些应该被清理掉。 clear_pending_connections end end |
#摘要⇒对象
此方法是实验性的,可能会发生变化。
321 322 323 324 325 326 327 328 329 330 331 332 333 |
# File 'lib/ Mongo/ 服务器/connection_pool.rb', line 321 def 总结 锁.同步 do 州 = if 已关闭? '已关闭' elsif !@ready ' paused ' else ' ready ' end " #<ConnectionPool size= #{ { unsynchronized_size } } ( #{ min_size } - #{ max_size } ) " + " used= #{ @checked_out_connections . length } available = #{ @available_connections . length } ending = #{ @pending_connections . length } #{ state } > " end end |
# Unavailable_connections =" Integer "(整数)
此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。
返回 池中不可用连接的数量。 用于计算是否已达到 max_pool_size。
282 283 284 |
# File 'lib/ Mongo/ 服务器/connection_pool.rb', line 282 def Unavailable_connections @checked_out_connections.长度 + @pending_connections.长度 + @connection_requests end |
#wait_timeout(context = nil) ⇒ Float
等待连接变为可用的时间(以秒为单位)。
214 215 216 217 218 219 220 |
# File 'lib/ Mongo/ 服务器/connection_pool.rb', line 214 def wait_timeout(上下文 = nil) if 上下文&。剩余超时秒数.nil? [:wait_timeout] || DEFAULT_WAIT_TIMEOUT else 上下文&。剩余超时秒数 end end |
# with_connection (connection_global_id: nil, context: nil) ⇒ 对象
在处理签入/签出逻辑时,将区块让出给连接。
714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 |
# File 'lib/ Mongo/ 服务器/connection_pool.rb', line 714 def with_connection(connection_global_id: nil, 上下文: nil) ise_if_close! 连接 = check_out( connection_global_id: connection_global_id, 上下文: 上下文 ) 产量(连接) 救援 错误::SocketError, 错误::SocketTimeoutError, 错误::ConnectionPershed => e 也许_引发_池_清除!(连接, e) 确保 if 连接 check_in(连接) end end |