Class: Mongo::Server::ConnectionPool::GenerationManager Private

Inherits:
Object
  • Object
show all
Defined in:
lib/mongo/server/connection_pool/generation_manager.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Since:

  • 2.0.0, largely rewritten in 2.9.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server:) ⇒ GenerationManager

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 new instance of GenerationManager.

Since:

  • 2.0.0, largely rewritten in 2.9.0



25
26
27
28
29
30
31
# File 'lib/mongo/server/connection_pool/generation_manager.rb', line 25

def initialize(server:)
  @map = Hash.new { |hash, key| hash[key] = 1 }
  @pipe_fds = Hash.new { |hash, key| hash[key] = { 1 => IO.pipe } }
  @server = server
  @lock = Mutex.new
  @scheduled_for_close = []
end

Instance Attribute Details

#serverObject (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.

Since:

  • 2.0.0, largely rewritten in 2.9.0



33
34
35
# File 'lib/mongo/server/connection_pool/generation_manager.rb', line 33

def server
  @server
end

Instance Method Details

#bump(service_id: 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.

Since:

  • 2.0.0, largely rewritten in 2.9.0



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/mongo/server/connection_pool/generation_manager.rb', line 68

def bump(service_id: nil)
  @lock.synchronize do
    close_all_scheduled
    if service_id
      gen = @map[service_id] += 1
      @pipe_fds[service_id] ||= {}
      @pipe_fds[service_id][gen] = IO.pipe
    else
      # When service id is not supplied, one of two things may be
      # happening;
      #
      # 1. The pool is not to a load balancer, in which case we only
      #    need to increment the generation for the nil service_id.
      # 2. The pool is to a load balancer, in which case we need to
      #    increment the generation for each service.
      #
      # Incrementing everything in the map accomplishes both tasks.
      @map.each do |k, v|
        gen = @map[k] += 1
        @pipe_fds[service_id] ||= {}
        @pipe_fds[service_id][gen] = IO.pipe
      end
    end
  end
end

#close_all_pipesObject

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.

Close all pipes in the generation manager.

This method should be called only when the ConnectionPool that owns this GenerationManager is closed, to ensure that all pipes are closed properly.

Since:

  • 2.0.0, largely rewritten in 2.9.0



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/mongo/server/connection_pool/generation_manager.rb', line 99

def close_all_pipes
  @lock.synchronize do
    close_all_scheduled
    @pipe_fds.keys.each do |service_id|
      generations = @pipe_fds.delete(service_id)
      generations.values.each do |(r, w)|
        r.close
        w.close
      rescue IOError
        # Ignore any IOError that occurs when closing the
        # pipe, as there is nothing we can do about it.
      end
    end
  end
end

#generation(service_id: 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.

Since:

  • 2.0.0, largely rewritten in 2.9.0



35
36
37
38
39
40
41
# File 'lib/mongo/server/connection_pool/generation_manager.rb', line 35

def generation(service_id: nil)
  validate_service_id!(service_id)

  @lock.synchronize do
    @map[service_id]
  end
end

#generation_unlocked(service_id: 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.

Since:

  • 2.0.0, largely rewritten in 2.9.0



43
44
45
46
47
# File 'lib/mongo/server/connection_pool/generation_manager.rb', line 43

def generation_unlocked(service_id: nil)
  validate_service_id!(service_id)

  @map[service_id]
end

#pipe_fds(service_id: 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.

Since:

  • 2.0.0, largely rewritten in 2.9.0



49
50
51
# File 'lib/mongo/server/connection_pool/generation_manager.rb', line 49

def pipe_fds(service_id: nil)
  @pipe_fds.dig(service_id, @map[service_id])
end

#remove_pipe_fds(generation, service_id: 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.

Since:

  • 2.0.0, largely rewritten in 2.9.0



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/mongo/server/connection_pool/generation_manager.rb', line 53

def remove_pipe_fds(generation, service_id: nil)
  validate_service_id!(service_id)

  r, w = @pipe_fds[service_id].delete(generation)
  return unless r && w

  w.close
  # Schedule the read end of the pipe to be closed. We cannot close it
  # immediately since we need to wait for any Kernel#select calls to
  # notice that part of the pipe is closed, and check the socket. This
  # all happens when attempting to read from the socket and waiting for
  # it to become ready again.
  @scheduled_for_close << r
end