da_funk 0.28.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,165 +0,0 @@
1
- module Serfx
2
- # This class wraps the low level msgpack data transformation and tcp
3
- # communication for the RPC session. methods in this module are used to
4
- # implement the actual RPC commands available via [Commands]
5
- class Connection
6
- MAX_MESSAGE_SIZE = 1024
7
- DEFAULT_TIMEOUT = 5
8
- COMMANDS = {
9
- handshake: [:header],
10
- auth: [:header],
11
- event: [:header],
12
- force_leave: [:header],
13
- join: [:header, :body],
14
- members: [:header, :body],
15
- members_filtered: [:header, :body],
16
- tags: [:header],
17
- stream: [:header],
18
- monitor: [:header],
19
- stop: [:header],
20
- leave: [:header],
21
- query: [:header],
22
- respond: [:header],
23
- install_key: [:header, :body],
24
- use_key: [:header, :body],
25
- remove_key: [:header, :body],
26
- list_keys: [:header, :body],
27
- stats: [:header, :body]
28
- }
29
-
30
- include Serfx::Commands
31
-
32
- attr_reader :host, :port, :seq, :socket, :socket_block, :timeout, :stream_timeout
33
-
34
- def close
35
- @socket.close
36
- end
37
-
38
- def closed?
39
- @socket.closed?
40
- end
41
-
42
- # @param opts [Hash] Specify the RPC connection details
43
- # @option opts [SSL] :socket Socket SSL or normal tcp socket
44
- # @option opts [Fixnum] :timeout Timeout in seconds to wait for a event and return Fiber
45
- # @option opts [Symbol] :authkey encryption key for RPC communication
46
- # @option opts [Block] :socket_block Callback to create socket if socket was closed, should return [socket]
47
- def initialize(opts = {})
48
- if @socket_block = opts[:socket_block]
49
- @socket = @socket_block.call(true)
50
- else
51
- @socket = opts[:socket]
52
- end
53
- @timeout = opts[:timeout] || DEFAULT_TIMEOUT
54
- @stream_timeout = opts[:stream_timeout] || DEFAULT_TIMEOUT
55
- @seq = 0
56
- @authkey = opts[:authkey]
57
- @requests = {}
58
- @responses = {}
59
- end
60
-
61
- # read data from tcp socket and pipe it through msgpack unpacker for
62
- # deserialization
63
- #
64
- # @return [Hash]
65
- def read_data(read_timeout = self.timeout)
66
- buf = read_buffer(read_timeout)
67
- return {'Error' => "Socket closed"} if buf.nil?
68
- return if buf.empty?
69
- parse_package(buf)
70
- end
71
-
72
- def parse_package(buf)
73
- object = MessagePack.unpack(buf)
74
- check = MessagePack.pack(object)
75
- if check.size == buf.size
76
- [object, nil]
77
- else
78
- [object, MessagePack.unpack(buf[check.size..-1])]
79
- end
80
- end
81
-
82
- def read_buffer(read_timeout)
83
- time_timeout = Time.now + read_timeout
84
- loop do
85
- bytes = socket.bytes_available
86
- return socket.recv_nonblock(bytes) if bytes > 0
87
- break unless time_timeout > Time.now
88
- usleep 200_000
89
- end
90
- ""
91
- end
92
-
93
- # takes raw RPC command name and an optional request body
94
- # and convert them to msgpack encoded data and then send
95
- # over tcp
96
- #
97
- # @param command [String] RPC command name
98
- # @param body [Hash] request body of the RPC command
99
- #
100
- # @return [Integer]
101
- def tcp_send(command, body = nil)
102
- @seq += 1
103
- header = {
104
- 'Command' => command.to_s.gsub('_', '-'),
105
- 'Seq' => seq
106
- }
107
-
108
- buff = ""
109
- buff << header.to_msgpack
110
- buff << body.to_msgpack unless body.nil?
111
-
112
- res = socket.write(buff) unless buff.empty?
113
- @requests[seq] = { header: header, ack?: false }
114
- seq
115
- end
116
-
117
- # checks if the RPC response header has `error` field popular or not
118
- # raises [RPCError] exception if error string is not empty
119
- #
120
- # @param header [Hash] RPC response header as hash
121
- def check_rpc_error!(header, must_return = false)
122
- if header
123
- raise RPCError, header['Error'] unless header['Error'].empty?
124
- else
125
- raise RPCError, "Socket Closed" if must_return
126
- end
127
- end
128
-
129
- # read data from the tcp socket. and convert it to a [Response] object
130
- #
131
- # @param command [String] RPC command name for which response will be read
132
- # @return [Response]
133
- def read_response(command, read_timeout = self.timeout)
134
- header, body = read_data(read_timeout)
135
- check_rpc_error!(header, true)
136
- if COMMANDS[command].include?(:body)
137
- Response.new(header, body)
138
- else
139
- Response.new(header)
140
- end
141
- end
142
-
143
- # make an RPC request against the serf agent
144
- #
145
- # @param command [String] name of the RPC command
146
- # @param body [Hash] an optional request body for the RPC command
147
- # @return [Response]
148
- def request(command, body = nil, read_timeout = self.timeout)
149
- tcp_send(command, body)
150
- read_response(command, read_timeout)
151
- end
152
-
153
- def fiber_yield!(ev)
154
- if Fiber.yield(ev) == "close"
155
- self.close
156
- false
157
- elsif self.closed?
158
- false
159
- else
160
- true
161
- end
162
- end
163
- end
164
- end
165
-
@@ -1,5 +0,0 @@
1
- # encoding: UTF-8
2
-
3
- module Serfx
4
- class RPCError < RuntimeError; end
5
- end
@@ -1,28 +0,0 @@
1
- module Serfx
2
- # Store agent rpc response data
3
- # All RPC responses in Serf composed of an header and an optional
4
- # body.
5
- #
6
- class Response
7
- # Header is composed of two sub-parts
8
- # - Seq : an integer representing the original request
9
- # - Error: a string that represent whether the request made, was
10
- # successfull or no. For all successful RPC requests, Error should
11
- # be an empty string
12
- #
13
- # `{"Seq": 0, "Error": ""}`
14
- #
15
- Header = DaFunk::Struct.klass(:seq, :error)
16
- attr_reader :header, :body
17
-
18
- # Constructs a response object from a given header and body.
19
- #
20
- # @param header [Hash] header of the response as hash
21
- # @param body [Hash] body of the response as hash
22
- def initialize(header, body = nil)
23
- @header = Header.call(header['Seq'], header['Error']) if header
24
- @body = body
25
- end
26
- end
27
- end
28
-
data/lib/serfx.rb DELETED
@@ -1,27 +0,0 @@
1
-
2
- module Serfx
3
- # Creates a serf rpc connection, performs handshake and auth
4
- # (if authkey is supplied). If a block is provided, the connection
5
- # object will be yield-ed and the connection will be closed after
6
- # the block's execution, otherwise the connection will be returned
7
- # Params:
8
- # +opts+:: An optional hash which can have following keys:
9
- # * host => Serf host's rpc bind address (127.0.0.1 by default)
10
- # * port => Serf host's rpc port (7373 by default)
11
- # * authkey => Encryption key for RPC communiction
12
- #
13
- # @return value of the block evaluation or the connection object
14
- def self.connect(opts = {})
15
- conn = Serfx::Connection.new(opts)
16
- conn.handshake
17
- conn.auth if opts.key?(:authkey)
18
- res = nil
19
- if block_given?
20
- res = yield conn
21
- conn.close unless conn.closed?
22
- res
23
- else
24
- conn
25
- end
26
- end
27
- end