类:Mongo::Server::Connection

继承:
ConnectionBase 显示全部
扩展方式:
可转发
包括:
IDMonitoring::PublishableRetryable
定义于:
lib/ Mongo/ 服务器/connection.rb

Overview

此类对服务器的套接字连接及其行为进行建模。

由于:

  • 2.0.0

常量摘要折叠

PING =
已弃用。

MongoDB Server选择规范不再需要此功能。

ping 命令。

由于:

  • 2.1.0

{ 网络探测(ping): 1 }.冻结
PING_OP_MSG =
已弃用。

MongoDB Server选择规范不再需要此功能。

OP_MSG 的网络探测(ping)命令

由于:

  • 2.5.0

{ :ping => 1, ' $db ' => Database::ADMIN }.冻结
PING_OP_MSG_MESSAGE =
已弃用。

MongoDB Server选择规范不再需要此功能。

作为 OP_MSG 的网络探测(ping)消息

由于:

  • 2.5.0

protocol::消息.new([], {}, PING_OP_MSG)
PING_OP_MSG_BYTES =
已弃用。

MongoDB Server选择规范不再需要此功能。

原始字节形式的网络探测(ping) OP_MSG 消息

由于:

  • 2.5.0

PING_OP_MSG_MESSAGE.序列化.to_s.冻结

Loggable中包含的常量

Loggable::PREFIX

ConnectionBase继承的常量

Mongo::Server::ConnectionBase::DEFAULT_MAX_BSON_OBJECT_SIZEMongo::Server::ConnectionBase::MAX_BSON_COMMAND_OVERHEADMongo::Server::ConnectionBase::REDUCED_MAX_BSON_SIZE

实例属性摘要折叠

包含在Monitoring::Publishable 中的属性

#monitoring

ConnectionBase继承的属性

#description#options#server

ConnectionCommon继承的属性

#compressor , #pid

实例方法摘要折叠

ID中包含的方法

包含

Retryable 中包含的方法

#read_worker#select_server#with_overload_retry#write_worker

Monitoring::Publishable 中包含的方法

#publish_cmap_event#publish_event#publish_sdam_event

Loggable中包含的方法

#log_debug#log_error#log_ Fatal#log_info#log_warn#logger

ConnectionBase继承的方法

#app_metadata#dispatch#Generation#service_id

ConnectionCommon继承的方法

#handshake_command , #handshake_document

构造函数详情

#initialize (server, options = {}) ⇒连接

此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。

注意:

绝不能在MongoDB Server之外直接实例化连接。

使用指定选项创建指向指定目标地址的新连接对象。

构造函数不执行任何 I/O(因此不创建套接字、不握手也不进行身份验证);调用 connect! 连接对象上的方法来创建网络连接。

例子:

创建连接。

Connection.new(server)

参数:

  • server ( Mongo::Server )

    连接所属的服务器。

  • 选项 哈希 (默认为: {}

    连接选项。

选项哈希 ( options ):

  • :pipe ( IO )

    从套接字读取时,在 select 系统调用期间要侦听的管道读取端的文件描述符。

  • :Generation 整数

    此连接的生成时间。 仅当未处于负载均衡模式时,才应在此选项中指定生成,并且应是创建连接时连接池的生成。 在负载均衡模式下,握手完成后会在连接上设置生成。

  • :server_api 哈希

    请求的服务器 API 版本。 此哈希可以包含以下项目:

    • :version -- string
    • :strict -- 布尔值
    • :deprecation_errors -- 布尔值

由于:

  • 2.0.0



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/ Mongo/ 服务器/connection.rb', line 90

def 初始化(server, 选项 = {})
  if server.load_balancer? && 选项[:Generation]
    提高 ArgumentError, '当服务器是负载负载均衡器时,无法设立生成 '
  end

  @id = server.next_connection_id
  @global_id = self.class.next_id
  @monitoring = server.监控
  @options = 选项.冻结
  @server = server
  @socket = nil
  @last_checkin = nil
  @auth_ Mechanism = nil
  @pid = 处理.进程 ID (PID)
  @pin_reasons = .new

  publish_cmap_event(
    监控::事件::Cmap::connectionCreated.new(地址, id)
  )
end

实例属性详细信息

# global_id整数(只读)

跨所有连接。

返回:

  • ( Integer )

    连接的全局 ID。 这将是唯一的

由于:

  • 2.0.0



124
125
126
# File 'lib/ Mongo/ 服务器/connection.rb', line 124

def global_id
  @global_id
end

# ID =" Integer " (只读)

跨连接到同一服务器对象。

返回:

  • ( Integer )

    连接的 ID。 这将是唯一的

由于:

  • 2.9.0



120
121
122
# File 'lib/ Mongo/ 服务器/connection.rb', line 120

def id
  @id
end

# last_checkin时间(只读)

返回上次将连接重新检入池中的时间。

返回:

  • 时间

    上次将连接检回池中的时间。

由于:

  • 2.5.0



114
115
116
# File 'lib/ Mongo/ 服务器/connection.rb', line 114

def last_checkin
  @last_checkin
end

实例方法详细信息

#已关闭?true | false

连接是否已关闭。

不应再使用关闭的连接。 而是从连接池中获取新连接。

返回:

  • ( true | false )

    连接是否已关闭。

由于:

  • 2.9.0



152
153
154
# File 'lib/ Mongo/ 服务器/connection.rb', line 152

def 已关闭?
  !!@close
end

# connect! (context = nil) ⇒ true

注意:

如果以前不存在套接字,则此方法会通过设置套接字来更改连接对象。

建立与目标地址的网络连接。

如果已建立连接,则此方法不执行任何操作。

例子:

连接到主机。

connection.connect!

返回:

  • ( true )

    如果连接成功。

由于:

  • 2.0.0



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/ Mongo/ 服务器/connection.rb', line 228

def 连接!(上下文 = nil)
  ise_if_close!

  除非 @socket
    @socket = create_socket(上下文)
    @description, @compressor = do_connect

    if server.load_balancer?
      if Lint.已启用? && !service_id
        提高 错误::内部驱动程序错误,
              ' 连接到负载负载均衡器,必须在此处设立service_id,但没有 '
      end
      @Generation = connection_pool.Generation_manager.生成(service_id: service_id)
    end

    publish_cmap_event(
      监控::事件::Cmap::connectionReady.new(地址, id)
    )

    @close_event_published = false
  end
  true
end

#已连接?true | false

连接是否已连接且未中断、关闭或引发错误。

返回:

  • ( true | false )

    如果连接已连接。

由于:

  • 2.0.0



140
141
142
# File 'lib/ Mongo/ 服务器/connection.rb', line 140

def 已连接?
  !已关闭? && !错误? && !中断? && !!@socket
end

# connection_pool对象

此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。

创建此连接的连接池。 可能为零。

由于:

  • 2.0.0



132
133
134
# File 'lib/ Mongo/ 服务器/connection.rb', line 132

def connection_pool
  选项[:connection_pool]
end

#断开连接! (options = nil) ⇒ true

注意:

连接断开后,不应再使用。 应从连接池获取新连接,连接池将返回就绪连接或创建新连接。 如果启用了 linting,则重复使用已断开的连接将引发 Error::LintError。 如果未启用 linting,则会记录警告。

注意:

如果关闭成功,此方法会通过将套接字设置为 nil 来更改连接对象。

断开连接。

参数:

  • 选项 哈希 (默认为: nil

    一组可自定义的选项

选项哈希 ( options ):

  • :reason 符号

    连接关闭的原因。

  • :interrupted ( true | false )

    连接是否中断。

返回:

  • ( true )

    如果断开连接成功。

由于:

  • 2.0.0



310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'lib/ Mongo/ 服务器/connection.rb', line 310

def 断开连接!(选项 = nil)
  # 注意:@close 在这里可能为 true,但我们也可能有一个套接字。
  # 检查套接字,而不是@关闭标志。
  @auth_ Mechanism = nil
  @last_checkin = nil
  if 套接字
    开始
      套接字.关闭
    救援 StandardError
      nil
    end
    @socket = nil
  end
  @close = true
  中断! if 选项 && 选项[:interrupted]

  # 为了满足 CMAP 规范测试,发布关闭事件,即使
  # 套接字从未连接(因此就绪事件从未
  # 已发布)。 但跟踪我们是否发布了关闭事件,并且不
  # 多次发布,除非重新连接套接字 -
  # 在这种情况下,每次套接字关闭都会发布一次关闭事件。
  除非 @close_event_published
    原因 = 选项 && 选项[:reason]
    publish_cmap_event(
      监控::事件::Cmap::connectionClosed.new(
        地址,
        id,
        原因
      )
    )
    @close_event_published = true
  end

  true
end

#错误?布尔

此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。

返回:

  • 布尔值

由于:

  • 2.0.0



172
173
174
# File 'lib/ Mongo/ 服务器/connection.rb', line 172

def 错误?
  !!@error
end

#中断!对象

将连接标记为已中断。

由于:

  • 2.0.0



167
168
169
# File 'lib/ Mongo/ 服务器/connection.rb', line 167

def 中断!
  @interrupted = true
end

#中断?true | false

连接是否中断。

中断的连接已从池中删除,不应重新检入池中。

返回:

  • ( true | false )

    连接是否中断。

由于:

  • 2.0.0



162
163
164
# File 'lib/ Mongo/ 服务器/connection.rb', line 162

def 中断?
  !!@interrupted
end

#pin (reason =: 游标) ⇒ 对象

此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。

由于给定原因,将连接标记为固定。

参数:

  • 原因 符号 (默认为: :游标)

    固定的原因(:游标或:ACID 事务)。默认为 :游标以实现向后兼容。

由于:

  • 2.0.0



194
195
196
# File 'lib/ Mongo/ 服务器/connection.rb', line 194

def (原因 = :cursor)
  @pin_reasons << 原因
end

# pingtrue , false

已弃用。

MongoDB Server选择规范不再需要此功能。

注意:

它使用预序列化的 ping 消息进行优化。

对连接执行 Ping 操作,查看服务器是否响应命令。 这在服务器端是非阻塞的。

例子:

对连接执行 Ping 操作。

connection.ping

返回:

  • ( true , false )

    如果服务器正在接受连接。

由于:

  • 2.1.0



359
360
361
362
363
364
365
366
367
# File 'lib/ Mongo/ 服务器/connection.rb', line 359

def ping
  确保连接 do |套接字|
    回复 = add_server_diagnostics do
      套接字.(PING_OP_MSG_BYTES)
      protocol::message.反序列化(套接字, max_message_size)
    end
    回复.文档[0][操作::结果::正常] == 1
  end
end

#已固定?布尔

此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。

该连接是由事务还是游标操作使用。

如果固定连接空闲或过时,则不应将其断开连接或从连接池中删除。

@return [ true | false ] 连接是否已固定。

返回:

  • 布尔值

由于:

  • 2.0.0



184
185
186
# File 'lib/ Mongo/ 服务器/connection.rb', line 184

def 已固定?
  !@pin_reasons.空?
end

# record_checkin!self

记录上次签到时间。

例子:

记录此连接的签入时间。

connection.record_checkin!

返回:

  • ( self )

由于:

  • 2.5.0



388
389
390
391
# File 'lib/ Mongo/ 服务器/connection.rb', line 388

def record_checkin!
  @last_checkin = 时间.now
  self
end

# socket_timeoutFloat也称为:超时

获取在套接字上执行操作的超时时间。

返回:

  • (浮点)

    操作超时(以秒为单位)。

由于:

  • 2.0.0



374
375
376
# File 'lib/ Mongo/ 服务器/connection.rb', line 374

def socket_timeout
  @timeout ||= 选项[:socket_timeout]
end

#transportSymbol | nil

此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。

获取此连接的传输类型。

返回:

  • (Symbol | nil)

    传输类型:tcp 或 :unix,如果没有套接字,则为 nil。

由于:

  • 2.0.0



398
399
400
401
402
403
404
405
406
407
# File 'lib/ Mongo/ 服务器/connection.rb', line 398

def 传输
  return nil if @socket.nil?

  案例 @socket
  when mongo::套接字::Unix
    :unix
  else
    :tcp
  end
end

# unpin (reason =: 游标) ⇒ 对象

此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。

由于给定原因,从连接中删除管脚。

参数:

  • 原因 符号 (默认为: :游标)

    取消固定的原因(:游标或:ACID 事务)。默认为 :游标以实现向后兼容。

由于:

  • 2.0.0



204
205
206
# File 'lib/ Mongo/ 服务器/connection.rb', line 204

def 取消固定(原因 = :cursor)
  @pin_reasons.删除(原因)
end

#unpin_all对象

此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。

从连接中删除所有管脚。

由于:

  • 2.0.0



211
212
213
# File 'lib/ Mongo/ 服务器/connection.rb', line 211

def unpin_all
  @pin_reasons.清除
end