em-synchrony-moped 0.9.2 → 0.9.3

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.
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = 'em-synchrony-moped'
5
- s.version = "0.9.2"
5
+ s.version = "0.9.3"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Adam Lebsack"]
@@ -2,33 +2,56 @@ require "moped/connection"
2
2
  require "moped/node"
3
3
  require 'em-resolv-replace'
4
4
 
5
- silence_warnings {
6
-
5
+ silence_warnings do
7
6
  module Moped
8
7
  class Cluster
9
8
  def sleep(seconds)
10
9
  EM::Synchrony.sleep(seconds)
11
10
  end
12
- end
13
-
14
- class Node
15
- def resolve_address
16
- begin
17
- parse_address and true
18
- rescue SocketError
11
+
12
+ # MONKEY PATCH: if all available connections are down, use the seeds
13
+ # again to determine, where the application should connect. This is a
14
+ # implementation detail and unlikely to be main line. Therefore no pull
15
+ # request here. The assumtion is, that if we can't connect to any mongodb
16
+ # that they are relocated and available under a different DNS address.
17
+ def nodes(opts = {})
18
+ current_time = Time.new
19
+ down_boundary = current_time - down_interval
20
+ refresh_boundary = current_time - refresh_interval
21
+
22
+ # Find the nodes that were down but are ready to be refreshed, or those
23
+ # with stale connection information.
24
+ needs_refresh, available = @nodes.partition do |node|
25
+ node.down? ? (node.down_at < down_boundary) : node.needs_refresh?(refresh_boundary)
26
+ end
27
+
28
+ # Refresh those nodes.
29
+ available.concat refresh(needs_refresh)
30
+
31
+ # Now return all the nodes that are available and participating in the
32
+ # replica set.
33
+ avail_not_down = available.reject do |node|
34
+ node.down? || !member?(node) || (!opts[:include_arbiters] && node.arbiter?)
35
+ end
36
+
37
+ if avail_not_down.empty?
19
38
  if logger = Moped.logger
20
- logger.warn " MOPED: Could not resolve IP address for #{address}"
39
+ logger.warn " MOPED: reinitialize cluster because all nodes " \
40
+ "are down with #{@seeds.inspect}"
21
41
  end
22
- @down_at = Time.new
23
- false
42
+ @nodes = @seeds.map { |host| Node.new(host, @options) }
24
43
  end
44
+
45
+ avail_not_down
25
46
  end
26
-
47
+ end
48
+
49
+ class Node
27
50
  # Override to support non-blocking DNS requests
28
51
  def parse_address
29
52
  host, port = address.split(":")
30
53
  @port = (port || 27017).to_i
31
-
54
+
32
55
  @resolver ||= Resolv.new([Resolv::Hosts.new, Resolv::DNS.new])
33
56
 
34
57
  # For now, limit the IPs only to IPv4 hosts. In order to support IPv6,
@@ -46,7 +69,7 @@ silence_warnings {
46
69
  end
47
70
  end
48
71
 
49
-
72
+
50
73
  class Connection
51
74
  def connect
52
75
  @sock = if !!options[:ssl]
@@ -56,19 +79,19 @@ silence_warnings {
56
79
  end
57
80
  end
58
81
  end
59
-
82
+
60
83
  Sockets.send(:remove_const, :TCP)
61
84
  Sockets.send(:remove_const, :SSL)
62
85
  module Sockets
63
86
  module Connectable
64
87
  attr_accessor :options
65
-
88
+
66
89
  def alive?
67
90
  !closed?
68
91
  end
69
-
92
+
70
93
  module ClassMethods
71
-
94
+
72
95
  def connect(host, port, timeout, options={})
73
96
  socket = EM.connect(host, port, self) do |c|
74
97
  c.pending_connect_timeout = timeout
@@ -91,8 +114,8 @@ silence_warnings {
91
114
  rescue OpenSSL::SSL::SSLError => error
92
115
  raise Errors::ConnectionFailure, "#{host}:#{port}: #{error.class.name} (#{error.errno}): #{error.message}"
93
116
  end
94
-
95
-
117
+
118
+
96
119
  end
97
120
  end
98
121
 
@@ -111,7 +134,7 @@ silence_warnings {
111
134
  start_tls
112
135
  end
113
136
  end
114
-
137
+
115
138
  def ssl_verify_peer(pem)
116
139
  unless cert_store = @options[:ssl][:cert_store]
117
140
  cert_store = OpenSSL::X509::Store.new
@@ -126,7 +149,7 @@ silence_warnings {
126
149
  return true
127
150
  end
128
151
  end
129
-
152
+
130
153
  host = @options[:ssl][:verify_host]
131
154
  if OpenSSL::SSL.verify_certificate_identity(cert, host)
132
155
  @verified = true
@@ -134,13 +157,13 @@ silence_warnings {
134
157
  end
135
158
  end
136
159
  end
137
-
160
+
138
161
  true
139
162
  rescue => e
140
163
  unbind "Failed to verify SSL certificate of peer"
141
164
  false
142
165
  end
143
-
166
+
144
167
  def ssl_handshake_completed
145
168
  if @options[:ssl][:verify_peer] && !@verified
146
169
  unbind "Failed to verify SSL certificate of peer"
@@ -149,8 +172,8 @@ silence_warnings {
149
172
  @in_req.succeed self
150
173
  end
151
174
  end
152
-
175
+
153
176
  end
154
177
  end
155
178
  end
156
- }
179
+ end
data/spec/moped_spec.rb CHANGED
@@ -11,24 +11,6 @@ describe "em-synchrony/moped" do
11
11
  Moped::Node.new("#{host}:#{FakeMongodHelper::BASE_PORT}", options)
12
12
  end
13
13
 
14
- context "ip address caching" do
15
- subject { new_node }
16
- around do |block|
17
- EventMachine.synchrony do
18
- start_mongod
19
-
20
- block.call
21
- EM.stop
22
- end
23
- end
24
-
25
- it "doesn't cache the ip_address" do
26
- subject.ip_address == "10.0.0.1"
27
- subject.should_receive(:parse_address)
28
- subject.refresh
29
- end
30
- end
31
-
32
14
  context "without ssl" do
33
15
 
34
16
  it "should connect" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: em-synchrony-moped
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.2
4
+ version: 0.9.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-05-14 00:00:00.000000000 Z
12
+ date: 2013-05-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: eventmachine