bones-rpc 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (56) hide show
  1. checksums.yaml +4 -4
  2. data/.ruby-gemset +1 -1
  3. data/.ruby-version +1 -1
  4. data/bones-rpc.gemspec +1 -4
  5. data/lib/bones/rpc.rb +1 -1
  6. data/lib/bones/rpc/adapter.rb +44 -4
  7. data/lib/bones/rpc/adapter/json.rb +66 -0
  8. data/lib/bones/rpc/address.rb +6 -4
  9. data/lib/bones/rpc/backend.rb +31 -0
  10. data/lib/bones/rpc/backend/base.rb +30 -0
  11. data/lib/bones/rpc/backend/synchronous.rb +29 -0
  12. data/lib/bones/rpc/cluster.rb +18 -18
  13. data/lib/bones/rpc/connection.rb +19 -15
  14. data/lib/bones/rpc/connection/socket.rb +0 -2
  15. data/lib/bones/rpc/connection/socket/connectable.rb +15 -11
  16. data/lib/bones/rpc/context.rb +2 -2
  17. data/lib/bones/rpc/dns_resolver.rb +85 -0
  18. data/lib/bones/rpc/errors.rb +3 -0
  19. data/lib/bones/rpc/failover.rb +3 -3
  20. data/lib/bones/rpc/failover/disconnect.rb +2 -2
  21. data/lib/bones/rpc/failover/ignore.rb +2 -2
  22. data/lib/bones/rpc/failover/retry.rb +2 -2
  23. data/lib/bones/rpc/instrumentable.rb +3 -3
  24. data/lib/bones/rpc/instrumentable/log.rb +2 -2
  25. data/lib/bones/rpc/instrumentable/noop.rb +2 -2
  26. data/lib/bones/rpc/loggable.rb +8 -8
  27. data/lib/bones/rpc/node.rb +47 -37
  28. data/lib/bones/rpc/node/registry.rb +4 -0
  29. data/lib/bones/rpc/parser.rb +16 -5
  30. data/lib/bones/rpc/parser/buffer.rb +6 -2
  31. data/lib/bones/rpc/protocol/adapter_helper.rb +2 -2
  32. data/lib/bones/rpc/protocol/binary_helper.rb +2 -2
  33. data/lib/bones/rpc/read_preference.rb +3 -3
  34. data/lib/bones/rpc/read_preference/nearest.rb +3 -3
  35. data/lib/bones/rpc/read_preference/selectable.rb +4 -4
  36. data/lib/bones/rpc/readable.rb +4 -4
  37. data/lib/bones/rpc/session.rb +25 -16
  38. data/lib/bones/rpc/synchronous.rb +2 -0
  39. data/lib/bones/rpc/synchronous/connection.rb +36 -0
  40. data/lib/bones/rpc/synchronous/connection/reader.rb +59 -0
  41. data/lib/bones/rpc/synchronous/connection/socket.rb +4 -0
  42. data/lib/bones/rpc/synchronous/connection/socket/ssl.rb +57 -0
  43. data/lib/bones/rpc/synchronous/connection/socket/tcp.rb +30 -0
  44. data/lib/bones/rpc/synchronous/connection/writer.rb +86 -0
  45. data/lib/bones/rpc/synchronous/future.rb +91 -0
  46. data/lib/bones/rpc/synchronous/node.rb +45 -0
  47. data/lib/bones/rpc/uri.rb +20 -20
  48. data/lib/bones/rpc/version.rb +1 -1
  49. metadata +16 -52
  50. data/lib/bones/rpc/adapter/erlang.rb +0 -28
  51. data/lib/bones/rpc/adapter/msgpack.rb +0 -52
  52. data/lib/bones/rpc/connection/reader.rb +0 -49
  53. data/lib/bones/rpc/connection/socket/ssl.rb +0 -35
  54. data/lib/bones/rpc/connection/socket/tcp.rb +0 -28
  55. data/lib/bones/rpc/connection/writer.rb +0 -51
  56. data/lib/bones/rpc/future.rb +0 -26
@@ -0,0 +1,91 @@
1
+ # encoding: utf-8
2
+ require 'thread'
3
+
4
+ module Bones
5
+ module RPC
6
+ module Synchronous
7
+ class Future
8
+
9
+ def initialize
10
+ @start = Time.now
11
+ @mutex = Mutex.new
12
+ @ready = false
13
+ @result = nil
14
+ @forwards = nil
15
+ end
16
+
17
+ # Check if this future has a value yet
18
+ def ready?
19
+ @ready
20
+ end
21
+
22
+ # Obtain the value for this Future
23
+ def value(timeout = nil)
24
+ ready = result = nil
25
+
26
+ begin
27
+ @mutex.lock
28
+
29
+ if @ready
30
+ ready = true
31
+ result = @result
32
+ end
33
+ ensure
34
+ @mutex.unlock
35
+ end
36
+
37
+ unless ready
38
+ if timeout
39
+ raise TimeoutError, "Timeout not supported by Bones::RPC::Synchronous backend"
40
+ end
41
+ end
42
+
43
+ if result
44
+ result.value
45
+ else
46
+ raise TimeoutError, "Timeout not supported by Bones::RPC::Synchronous backend"
47
+ end
48
+ end
49
+ alias_method :call, :value
50
+
51
+ # Signal this future with the given result value
52
+ def signal(value)
53
+ @stop = Time.now
54
+ result = Result.new(value, self)
55
+
56
+ @mutex.synchronize do
57
+ raise "the future has already happened!" if @ready
58
+
59
+ @result = result
60
+ @ready = true
61
+ end
62
+ end
63
+ alias_method :<<, :signal
64
+
65
+ # Inspect this Bones::RPC::Synchronous::Future
66
+ alias_method :inspect, :to_s
67
+
68
+ def runtime
69
+ if @stop
70
+ @stop - @start
71
+ else
72
+ Time.now - @start
73
+ end
74
+ end
75
+
76
+ # Wrapper for result values to distinguish them in mailboxes
77
+ class Result
78
+ attr_reader :future
79
+
80
+ def initialize(result, future)
81
+ @result, @future = result, future
82
+ end
83
+
84
+ def value
85
+ @result.value
86
+ end
87
+ end
88
+ end
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,45 @@
1
+ # encoding: utf-8
2
+ require 'bones/rpc/node'
3
+ require 'bones/rpc/synchronous/connection'
4
+ require 'bones/rpc/synchronous/future'
5
+ require 'thread'
6
+
7
+ module Bones
8
+ module RPC
9
+ module Synchronous
10
+
11
+ # Represents a client to a node in a server cluster.
12
+ #
13
+ # @since 0.0.1
14
+ class Node < ::Bones::RPC::Node
15
+ # Compatability with Celluloid
16
+ def abort(cause)
17
+ raise cause
18
+ end
19
+
20
+ def async
21
+ self
22
+ end
23
+
24
+ def current_actor
25
+ self
26
+ end
27
+
28
+ def connection
29
+ @mutex.synchronize do
30
+ if block_given?
31
+ yield @connection
32
+ else
33
+ @connection
34
+ end
35
+ end
36
+ end
37
+
38
+ def initialize(*args)
39
+ @mutex = ::Mutex.new
40
+ super(*args)
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -4,52 +4,52 @@ module Bones
4
4
 
5
5
  # Parses Bones::RPC uri
6
6
  #
7
- # @since 1.3.0
7
+ # @since 0.0.1
8
8
  class Uri
9
9
 
10
10
  # Get the scheme pattern.
11
11
  #
12
- # @since 1.3.0
12
+ # @since 0.0.1
13
13
  SCHEME = /(bones-rpc:\/\/)/
14
14
 
15
15
  # The user name pattern.
16
16
  #
17
- # @since 1.3.0
17
+ # @since 0.0.1
18
18
  USER = /([-.\w:]+)/
19
19
 
20
20
  # The password pattern.
21
21
  #
22
- # @since 1.3.0
22
+ # @since 0.0.1
23
23
  PASS = /([^@,]+)/
24
24
 
25
25
  # The nodes pattern.
26
26
  #
27
- # @since 1.3.0
27
+ # @since 0.0.1
28
28
  NODES = /((([-.\w]+)(?::(\w+))?,?)+)/
29
29
 
30
30
  # The database pattern.
31
31
  #
32
- # @since 1.3.0
32
+ # @since 0.0.1
33
33
  DATABASE = /(?:\/([-\w]+))?/
34
34
 
35
35
  # The options pattern.
36
36
  #
37
- # @since 1.3.0
37
+ # @since 0.0.1
38
38
  OPTIONS = /(?:\?(.+))/
39
39
 
40
40
  # The full URI pattern.
41
41
  #
42
- # @since 1.3.0
42
+ # @since 0.0.1
43
43
  URI = /#{SCHEME}(#{USER}:#{PASS}@)?#{NODES}#{DATABASE}#{OPTIONS}?/
44
44
 
45
45
  # The options that have to do with write concerns.
46
46
  #
47
- # @since 2.0.0
47
+ # @since 0.0.1
48
48
  WRITE_OPTIONS = [ "w", "j", "fsync", "wtimeout" ].freeze
49
49
 
50
50
  # The mappings from read preferences in the URI to Bones::RPC's.
51
51
  #
52
- # @since 2.0.0
52
+ # @since 0.0.1
53
53
  READ_MAPPINGS = {
54
54
  "nearest" => :nearest,
55
55
  "primary" => :primary,
@@ -69,7 +69,7 @@ module Bones
69
69
  #
70
70
  # @return [ true, false ] If authorization is provided.
71
71
  #
72
- # @since 1.3.0
72
+ # @since 0.0.1
73
73
  def auth_provided?
74
74
  !username.nil? && !password.nil?
75
75
  end
@@ -81,7 +81,7 @@ module Bones
81
81
  #
82
82
  # @return [ String ] The database.
83
83
  #
84
- # @since 1.3.0
84
+ # @since 0.0.1
85
85
  def database
86
86
  @database ||= match[9]
87
87
  end
@@ -93,7 +93,7 @@ module Bones
93
93
  #
94
94
  # @return [ Array<String> ] The hosts.
95
95
  #
96
- # @since 1.3.0
96
+ # @since 0.0.1
97
97
  def hosts
98
98
  @hosts ||= match[5].split(",")
99
99
  end
@@ -105,7 +105,7 @@ module Bones
105
105
  #
106
106
  # @param [ String ] string The uri string.
107
107
  #
108
- # @since 1.3.0
108
+ # @since 0.0.1
109
109
  def initialize(string)
110
110
  @match = string.match(URI)
111
111
  invalid_uri!(string) unless @match
@@ -118,7 +118,7 @@ module Bones
118
118
  #
119
119
  # @param [ String ] Invalid string
120
120
  #
121
- # @since 1.3.1
121
+ # @since 0.0.1
122
122
  def invalid_uri!(string)
123
123
  scrubbed = string.gsub(/[^:]+@/, '<password>@')
124
124
  raise Errors::InvalidBonesRPCURI, "The provided connection string is not a value URI: #{scrubbed}"
@@ -134,7 +134,7 @@ module Bones
134
134
  #
135
135
  # @return [ Hash ] Options hash usable by Moped
136
136
  #
137
- # @since 1.3.0
137
+ # @since 0.0.1
138
138
  def options
139
139
  options_string, options = match[10], {}
140
140
  unless options_string.nil?
@@ -159,7 +159,7 @@ module Bones
159
159
  #
160
160
  # @return [ String ] The password.
161
161
  #
162
- # @since 1.3.0
162
+ # @since 0.0.1
163
163
  def password
164
164
  @password ||= match[4]
165
165
  end
@@ -171,7 +171,7 @@ module Bones
171
171
  #
172
172
  # @return [ Hash ] The uri as options.
173
173
  #
174
- # @since 1.3.0
174
+ # @since 0.0.1
175
175
  def to_hash
176
176
  config = { database: database, hosts: hosts }
177
177
  if username && password
@@ -187,7 +187,7 @@ module Bones
187
187
  #
188
188
  # @return [ Array ] Array of arguments usable by bones_rpc
189
189
  #
190
- # @since 1.3.0
190
+ # @since 0.0.1
191
191
  def bones_rpc_arguments
192
192
  [ hosts, options ]
193
193
  end
@@ -199,7 +199,7 @@ module Bones
199
199
  #
200
200
  # @return [ String ] The username.
201
201
  #
202
- # @since 1.3.0
202
+ # @since 0.0.1
203
203
  def username
204
204
  @username ||= match[3]
205
205
  end
@@ -1,6 +1,6 @@
1
1
  # encoding: utf-8
2
2
  module Bones
3
3
  module RPC
4
- VERSION = "0.0.2"
4
+ VERSION = "0.0.3"
5
5
  end
6
6
  end
metadata CHANGED
@@ -1,57 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bones-rpc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Bennett
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-09-27 00:00:00.000000000 Z
11
+ date: 2013-10-10 00:00:00.000000000 Z
12
12
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: celluloid-io
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - '>='
18
- - !ruby/object:Gem::Version
19
- version: 0.14.0
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - '>='
25
- - !ruby/object:Gem::Version
26
- version: 0.14.0
27
- - !ruby/object:Gem::Dependency
28
- name: erlang-etf
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - '>='
32
- - !ruby/object:Gem::Version
33
- version: '0'
34
- type: :runtime
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - '>='
39
- - !ruby/object:Gem::Version
40
- version: '0'
41
- - !ruby/object:Gem::Dependency
42
- name: msgpack
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - '>='
46
- - !ruby/object:Gem::Version
47
- version: '0'
48
- type: :runtime
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - '>='
53
- - !ruby/object:Gem::Version
54
- version: '0'
55
13
  - !ruby/object:Gem::Dependency
56
14
  name: optionable
57
15
  requirement: !ruby/object:Gem::Requirement
@@ -96,7 +54,7 @@ dependencies:
96
54
  version: '0'
97
55
  description: Bones::RPC client for ruby
98
56
  email:
99
- - andrew@delorum.com
57
+ - andrew@pagodabox.com
100
58
  executables: []
101
59
  extensions: []
102
60
  extra_rdoc_files: []
@@ -113,26 +71,23 @@ files:
113
71
  - lib/bones/rpc.rb
114
72
  - lib/bones/rpc/adapter.rb
115
73
  - lib/bones/rpc/adapter/base.rb
116
- - lib/bones/rpc/adapter/erlang.rb
117
74
  - lib/bones/rpc/adapter/json.rb
118
- - lib/bones/rpc/adapter/msgpack.rb
119
75
  - lib/bones/rpc/adapter/parser.rb
120
76
  - lib/bones/rpc/address.rb
77
+ - lib/bones/rpc/backend.rb
78
+ - lib/bones/rpc/backend/base.rb
79
+ - lib/bones/rpc/backend/synchronous.rb
121
80
  - lib/bones/rpc/cluster.rb
122
81
  - lib/bones/rpc/connection.rb
123
- - lib/bones/rpc/connection/reader.rb
124
82
  - lib/bones/rpc/connection/socket.rb
125
83
  - lib/bones/rpc/connection/socket/connectable.rb
126
- - lib/bones/rpc/connection/socket/ssl.rb
127
- - lib/bones/rpc/connection/socket/tcp.rb
128
- - lib/bones/rpc/connection/writer.rb
129
84
  - lib/bones/rpc/context.rb
85
+ - lib/bones/rpc/dns_resolver.rb
130
86
  - lib/bones/rpc/errors.rb
131
87
  - lib/bones/rpc/failover.rb
132
88
  - lib/bones/rpc/failover/disconnect.rb
133
89
  - lib/bones/rpc/failover/ignore.rb
134
90
  - lib/bones/rpc/failover/retry.rb
135
- - lib/bones/rpc/future.rb
136
91
  - lib/bones/rpc/instrumentable.rb
137
92
  - lib/bones/rpc/instrumentable/log.rb
138
93
  - lib/bones/rpc/instrumentable/noop.rb
@@ -155,6 +110,15 @@ files:
155
110
  - lib/bones/rpc/read_preference/selectable.rb
156
111
  - lib/bones/rpc/readable.rb
157
112
  - lib/bones/rpc/session.rb
113
+ - lib/bones/rpc/synchronous.rb
114
+ - lib/bones/rpc/synchronous/connection.rb
115
+ - lib/bones/rpc/synchronous/connection/reader.rb
116
+ - lib/bones/rpc/synchronous/connection/socket.rb
117
+ - lib/bones/rpc/synchronous/connection/socket/ssl.rb
118
+ - lib/bones/rpc/synchronous/connection/socket/tcp.rb
119
+ - lib/bones/rpc/synchronous/connection/writer.rb
120
+ - lib/bones/rpc/synchronous/future.rb
121
+ - lib/bones/rpc/synchronous/node.rb
158
122
  - lib/bones/rpc/uri.rb
159
123
  - lib/bones/rpc/version.rb
160
124
  homepage: ''
@@ -1,28 +0,0 @@
1
- # encoding: utf-8
2
- require 'erlang/etf'
3
-
4
- module Bones
5
- module RPC
6
- module Adapter
7
- module Erlang
8
-
9
- @adapter_name = :erlang
10
-
11
- def pack(message, buffer="")
12
- data = ::Erlang.term_to_binary(message)
13
- len = data.bytesize
14
- buffer << [len].pack('N')
15
- buffer << data
16
- end
17
-
18
- def unpack(buffer)
19
- len, = buffer.read(4).unpack('N')
20
- data = buffer.read(len)
21
- ::Erlang.binary_to_term(data)
22
- end
23
-
24
- Adapter.register self
25
- end
26
- end
27
- end
28
- end
@@ -1,52 +0,0 @@
1
- # encoding: utf-8
2
- require 'msgpack'
3
-
4
- module Bones
5
- module RPC
6
- module Adapter
7
- module MessagePack
8
-
9
- @adapter_name = :msgpack
10
-
11
- def pack(message, buffer="")
12
- buffer << ::MessagePack.pack(message)
13
- end
14
-
15
- def unpack(buffer)
16
- ::MessagePack.unpack(buffer)
17
- end
18
-
19
- def unpack_stream(stream)
20
- buffer = StringIO.new(stream)
21
- ::MessagePack::Unpacker.new(buffer).read
22
- end
23
-
24
- def read(unpacker)
25
- (unpacker.adapter_unpacker ||= ::MessagePack::Unpacker.new(unpacker.buffer)).read
26
- end
27
-
28
- def unpacker(data)
29
- ::MessagePack::Unpacker.new(StringIO.new(data))
30
- end
31
-
32
- def parser(data)
33
- Adapter::Parser.new(self, data)
34
- end
35
-
36
- def unpacker_pos(parser)
37
- size = parser.unpacker.buffer.size
38
- pos = parser.unpacker.buffer.io.pos
39
- (pos > size) ? (pos - size) : 0
40
- end
41
-
42
- def unpacker_seek(parser, n)
43
- pos = unpacker_pos(parser)
44
- parser.unpacker.buffer.skip(n - pos) if pos < n
45
- return pos
46
- end
47
-
48
- Adapter.register self
49
- end
50
- end
51
- end
52
- end