Class: ConnectionManager

Inherits:
Object
  • Object
show all
Defined in:
lib/connection-manager.rb,
lib/connection-manager/version.rb

Defined Under Namespace

Classes: Connection, LockingError, Wrapper

Constant Summary collapse

TIMEOUT_ARITY =
Timeout.method(:timeout).arity
VERSION =
"0.1.0"

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ ConnectionManager

Returns a new instance of ConnectionManager.



10
11
12
13
14
15
# File 'lib/connection-manager.rb', line 10

def initialize(**options)
  @connection_timeout = options.fetch(:timeout, 0)
  @manager_timeout = options.fetch(:manager_timeout, 0)
  @connections = {}
  @mutex = Mutex.new
end

Instance Method Details

#clearObject



17
18
19
20
21
22
23
24
25
26
# File 'lib/connection-manager.rb', line 17

def clear
  execute do
    connections.delete_if do |_, wrapper|
      wrapper.synchronize do
        wrapper.closed?
      end
    end
  end
  true
end

#close(key) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/connection-manager.rb', line 28

def close(key)
  wrapper = execute do
    connections[key.to_sym]
  end
  wrapper.synchronize do
    wrapper.close
  end if wrapper
end

#closed?(key) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
40
41
42
43
44
# File 'lib/connection-manager.rb', line 37

def closed?(key)
  wrapper = execute do
    connections[key.to_sym]
  end
  wrapper.synchronize do
    wrapper.closed?
  end if wrapper
end

#delete(key) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/connection-manager.rb', line 46

def delete(key)
  execute do
    wrapper = connections[key.to_sym]
    wrapper.synchronize do
      connections.delete(key.to_sym)
      true
    end if wrapper
  end
end

#delete_if(&block) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'lib/connection-manager.rb', line 56

def delete_if(&block)
  execute do
    connections.delete_if do |_, wrapper|
      wrapper.synchronize do
        block.call(wrapper.connection, wrapper.)
      end
    end
  end
  true
end

#empty?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/connection-manager.rb', line 67

def empty?
  size == 0
end

#exists?(key) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
74
75
# File 'lib/connection-manager.rb', line 71

def exists?(key)
  execute do
    connections.key? key.to_sym
  end
end

#metadata(key) ⇒ Object



77
78
79
80
81
82
83
84
# File 'lib/connection-manager.rb', line 77

def (key)
  wrapper = execute do
    connections[key.to_sym]
  end
  wrapper.synchronize do
    wrapper.
  end if wrapper
end

#open?(key) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
89
90
91
92
93
# File 'lib/connection-manager.rb', line 86

def open?(key)
  wrapper = execute do
    connections[key.to_sym]
  end
  wrapper.synchronize do
    !wrapper.closed?
  end if wrapper
end

#pop(key) ⇒ Object



95
96
97
98
99
100
101
102
# File 'lib/connection-manager.rb', line 95

def pop(key)
  execute do
    wrapper = connections[key.to_sym]
    wrapper.synchronize do
      connections.delete(key.to_sym).connection
    end if wrapper
  end
end

#push(key, **options, &block) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/connection-manager.rb', line 104

def push(key, **options, &block)
  options[:timeout] ||= connection_timeout
  execute do
    previous_connection = connections[key.to_sym]
    executor = if previous_connection
      -> { previous_connection.synchronize { connections[key.to_sym] = Wrapper.new(options, &block) } }
    else
      -> { connections[key.to_sym] = Wrapper.new(options, &block) }
    end
    executor.call
  end
  true
end

#reset(key) ⇒ Object



118
119
120
121
122
123
124
125
# File 'lib/connection-manager.rb', line 118

def reset(key)
  execute do
    wrapper = connections[key.to_sym]
    wrapper.synchronize do
      wrapper.reset
    end if wrapper
  end
end

#shutdownObject



127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/connection-manager.rb', line 127

def shutdown
  execute do
    connections.values.map do |wrapper|
      Thread.new do
        # Keep compatibility with ruby < 2.4
        Thread.current.report_on_exception = false if Thread.current.respond_to?(:report_on_exception=)
        wrapper.synchronize { wrapper.close }
      end
    end.each(&:join)
  end
  true
end

#sizeObject



140
141
142
143
144
# File 'lib/connection-manager.rb', line 140

def size
  execute do
    connections.keys.size
  end
end

#with(key, **options, &block) ⇒ Object



146
147
148
149
150
151
152
153
154
155
# File 'lib/connection-manager.rb', line 146

def with(key, **options, &block)
  wrapper = execute do
    connections[key.to_sym]
  end
  raise Connection::ClosedError if wrapper && wrapper.closed?

  wrapper.synchronize(options) do
    block.call(wrapper.connection, wrapper.)
  end if wrapper
end