rdkit 0.0.1 → 0.1.4

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.
Files changed (51) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +3 -0
  3. data/Gemfile +10 -1
  4. data/Gemfile.ci +13 -0
  5. data/Guardfile +40 -0
  6. data/README.md +282 -6
  7. data/Vagrantfile +13 -0
  8. data/example/blocking.rb +10 -0
  9. data/example/blocking/command_runner.rb +28 -0
  10. data/example/blocking/core.rb +24 -0
  11. data/example/blocking/server.rb +10 -0
  12. data/example/blocking/version.rb +3 -0
  13. data/example/callbacks.rb +9 -0
  14. data/example/callbacks/command_runner.rb +21 -0
  15. data/example/callbacks/core.rb +18 -0
  16. data/example/callbacks/server.rb +30 -0
  17. data/example/counter.rb +0 -2
  18. data/example/counter/command_runner.rb +4 -0
  19. data/example/counter/core.rb +4 -0
  20. data/example/http.rb +9 -0
  21. data/example/http/core.rb +18 -0
  22. data/example/http/responder.rb +7 -0
  23. data/example/http/server.rb +19 -0
  24. data/lib/rdkit.rb +20 -3
  25. data/lib/rdkit/callbacks.rb +10 -0
  26. data/lib/rdkit/client.rb +157 -0
  27. data/lib/rdkit/configuration.rb +31 -0
  28. data/lib/rdkit/core.rb +2 -4
  29. data/lib/rdkit/core_ext.rb +7 -0
  30. data/lib/rdkit/db.rb +257 -0
  31. data/lib/rdkit/db_commands.rb +182 -0
  32. data/lib/rdkit/errors.rb +32 -1
  33. data/lib/rdkit/http_parser.rb +56 -0
  34. data/lib/rdkit/http_responder.rb +74 -0
  35. data/lib/rdkit/introspection.rb +133 -21
  36. data/lib/rdkit/logger.rb +9 -4
  37. data/lib/rdkit/memory_monitoring.rb +29 -0
  38. data/lib/rdkit/notification_center.rb +21 -0
  39. data/lib/rdkit/rd_object.rb +69 -0
  40. data/lib/rdkit/resp.rb +9 -1
  41. data/lib/rdkit/{command_parser.rb → resp_parser.rb} +6 -18
  42. data/lib/rdkit/resp_responder.rb +105 -0
  43. data/lib/rdkit/server.rb +242 -86
  44. data/lib/rdkit/simple_commands.rb +17 -0
  45. data/lib/rdkit/slow_log.rb +52 -0
  46. data/lib/rdkit/subcommands.rb +157 -0
  47. data/lib/rdkit/version.rb +1 -1
  48. data/rdkit.gemspec +6 -0
  49. metadata +119 -5
  50. data/lib/rdkit/inheritable.rb +0 -15
  51. data/lib/rdkit/resp_runner.rb +0 -46
@@ -0,0 +1,17 @@
1
+ module RDKit
2
+ module SimpleCommands
3
+ def ping
4
+ 'PONG'
5
+ end
6
+
7
+ def echo(message)
8
+ message
9
+ end
10
+
11
+ def time
12
+ t = Time.now
13
+
14
+ [t.to_i, t.usec].map(&:to_s)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,52 @@
1
+ # http://redis.io/commands/slowlog
2
+
3
+ module RDKit
4
+ class SlowLog
5
+ module ClassMethods
6
+ @@logs = []
7
+ @@sequence_id = 0
8
+
9
+ def count
10
+ @@logs.size
11
+ end
12
+
13
+ def reset
14
+ @@logs.clear
15
+ end
16
+
17
+ def recent(count)
18
+ if count == 0
19
+ []
20
+ elsif count > 0
21
+ (@@logs[-count..-1] || @@logs).try(:reverse)
22
+ else
23
+ @@logs.try(:reverse)
24
+ end
25
+ end
26
+
27
+ def monitor(cmd, &block)
28
+ t0 = Time.now
29
+ result = block.call
30
+ elapsed_in_usec = ((Time.now - t0) * 1_000_000).to_i
31
+ elapsed_in_milliseconds = elapsed_in_usec / 1_000
32
+
33
+ if (threshold = Configuration.get_i('slowlog-log-slower-than')) >= 0
34
+ if elapsed_in_milliseconds >= threshold
35
+ @@logs << [@@sequence_id, Time.now.to_i, elapsed_in_milliseconds, cmd]
36
+ @@sequence_id += 1
37
+
38
+ if (max_len = Configuration.get_i('slowlog-max-len')) > 0
39
+ @@logs = @@logs[-max_len..-1]
40
+ end
41
+
42
+ NotificationCenter.publish('slowlog', [cmd, elapsed_in_usec])
43
+ end
44
+ end
45
+
46
+ [result, elapsed_in_usec]
47
+ end
48
+ end
49
+
50
+ class << self; include ClassMethods; end
51
+ end
52
+ end
@@ -0,0 +1,157 @@
1
+ module RDKit
2
+ module Subcommands
3
+ private
4
+
5
+ def execute_subcommand(base, valid_subcommands, subcommand, *args)
6
+ valid_subcommands, subcommand = valid_subcommands.map(&:upcase), subcommand.upcase
7
+
8
+ if valid_subcommands.include?(subcommand)
9
+ __send__("#{base}_#{subcommand.downcase}", *args)
10
+ else
11
+ raise UnknownSubcommandError, "#{base.upcase} subcommand must be one of #{valid_subcommands.join(', ')}"
12
+ end
13
+ rescue ArgumentError => e
14
+ raise WrongNumberOfArgumentForSubcommandError, "Wrong number of arguments for #{base.upcase} #{subcommand.downcase}"
15
+ end
16
+
17
+ module SlowLogSubcommands
18
+ private
19
+
20
+ def slowlog_get(count=nil)
21
+ if count
22
+ if count.to_i.to_s != count
23
+ raise ValueNotAnIntegerOrOutOfRangeError
24
+ end
25
+
26
+ SlowLog.recent(count.to_i)
27
+ else
28
+ SlowLog.recent(-1)
29
+ end
30
+ end
31
+
32
+ def slowlog_len
33
+ SlowLog.count
34
+ end
35
+
36
+ def slowlog_reset
37
+ SlowLog.reset
38
+
39
+ 'OK'
40
+ end
41
+ end
42
+ include SlowLogSubcommands
43
+
44
+ module ConfigSubcommands
45
+ private
46
+
47
+ def config_resetstat
48
+ Introspection::Stats.clear(:total_commands_processed)
49
+ Introspection::Stats.clear(:total_connections_received)
50
+ Introspection::Stats.clear(:total_net_input_bytes)
51
+ Introspection::Stats.clear(:total_net_output_bytes)
52
+ Introspection::Commandstats.reset
53
+
54
+ 'OK'
55
+ end
56
+
57
+ def config_get(key)
58
+ if value = Configuration.get(key)
59
+ [key, value]
60
+ else
61
+ []
62
+ end
63
+ end
64
+
65
+ def config_set(key, value)
66
+ Configuration.set(key, value)
67
+
68
+ 'OK'
69
+ end
70
+ end
71
+ include ConfigSubcommands
72
+
73
+ module ClientSubcommands
74
+ private
75
+
76
+ def client_getname
77
+ server.current_client.name
78
+ end
79
+
80
+ def client_setname(name)
81
+ server.current_client.name = name
82
+
83
+ 'OK'
84
+ end
85
+
86
+ def client_list
87
+ server.clients.map do |client|
88
+ client.info.map { |k, v| "#{k}=#{v}" }.join(' ')
89
+ end.join("\n") + "\n"
90
+ end
91
+
92
+ def client_kill(*args)
93
+ raise SyntaxError if args.size == 0
94
+
95
+ if args.size == 1
96
+ # client kill HOST:PORT
97
+ if kill_by_addr(args.first)
98
+ 'OK'
99
+ else
100
+ raise NoSuchClientError
101
+ end
102
+ else
103
+ raise SyntaxError if args.size % 2 != 0
104
+
105
+ killed = 0
106
+ while args.size > 0
107
+ type, arg = args.shift.downcase, args.shift
108
+
109
+ case type
110
+ when 'id'
111
+ id = arg.to_i
112
+
113
+ if id.to_s != arg
114
+ raise ValueNotAnIntegerOrOutOfRangeError
115
+ end
116
+
117
+ if client = server.clients.find { |client| client.id == id }
118
+ killed += 1
119
+ client.kill!
120
+ end
121
+ when 'addr'
122
+ killed += 1 if kill_by_addr(arg)
123
+ else
124
+ raise SyntaxError
125
+ end
126
+ end
127
+
128
+ killed
129
+ end
130
+ end
131
+
132
+ def kill_by_addr(addr)
133
+ if client = server.clients.find { |c| c.socket_addr == addr }
134
+ client.kill!
135
+
136
+ true
137
+ end
138
+ end
139
+ end
140
+ include ClientSubcommands
141
+
142
+ module DebugSubcommands
143
+ private
144
+
145
+ def debug_sleep(sec)
146
+ sleep sec.to_i
147
+
148
+ 'OK'
149
+ end
150
+
151
+ def debug_segfault
152
+ raise Exception, "simulating a crash..." # Exception is not StandardError
153
+ end
154
+ end
155
+ include DebugSubcommands
156
+ end
157
+ end
data/lib/rdkit/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module RDKit
2
- VERSION = '0.0.1'
2
+ VERSION = '0.1.4'
3
3
  end
data/rdkit.gemspec CHANGED
@@ -19,6 +19,12 @@ Gem::Specification.new do |spec|
19
19
 
20
20
  spec.add_runtime_dependency 'hiredis'
21
21
  spec.add_runtime_dependency 'newrelic_rpm'
22
+ spec.add_runtime_dependency 'sigdump'
23
+ spec.add_runtime_dependency 'thread', '~> 0.2.1'
24
+ spec.add_runtime_dependency 'http_parser.rb'
25
+ spec.add_runtime_dependency 'rack'
26
+ spec.add_runtime_dependency 'httpi'
27
+ spec.add_runtime_dependency 'multi_json'
22
28
 
23
29
  spec.add_development_dependency "bundler", "~> 1.8"
24
30
  spec.add_development_dependency "rake", "~> 10.0"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rdkit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Forrest Ye
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-05-27 00:00:00.000000000 Z
11
+ date: 2015-12-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: hiredis
@@ -38,6 +38,90 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: sigdump
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
+ - !ruby/object:Gem::Dependency
56
+ name: thread
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.2.1
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.2.1
69
+ - !ruby/object:Gem::Dependency
70
+ name: http_parser.rb
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rack
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: httpi
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: multi_json
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
41
125
  - !ruby/object:Gem::Dependency
42
126
  name: bundler
43
127
  requirement: !ruby/object:Gem::Requirement
@@ -91,27 +175,57 @@ files:
91
175
  - ".rspec"
92
176
  - ".travis.yml"
93
177
  - Gemfile
178
+ - Gemfile.ci
179
+ - Guardfile
94
180
  - README.md
95
181
  - Rakefile
182
+ - Vagrantfile
96
183
  - bin/console
97
184
  - bin/setup
185
+ - example/blocking.rb
186
+ - example/blocking/command_runner.rb
187
+ - example/blocking/core.rb
188
+ - example/blocking/server.rb
189
+ - example/blocking/version.rb
190
+ - example/callbacks.rb
191
+ - example/callbacks/command_runner.rb
192
+ - example/callbacks/core.rb
193
+ - example/callbacks/server.rb
98
194
  - example/counter.rb
99
195
  - example/counter/command_runner.rb
100
196
  - example/counter/core.rb
101
197
  - example/counter/server.rb
102
198
  - example/counter/version.rb
199
+ - example/http.rb
200
+ - example/http/core.rb
201
+ - example/http/responder.rb
202
+ - example/http/server.rb
103
203
  - lib/rdkit.rb
104
- - lib/rdkit/command_parser.rb
204
+ - lib/rdkit/callbacks.rb
205
+ - lib/rdkit/client.rb
206
+ - lib/rdkit/configuration.rb
105
207
  - lib/rdkit/core.rb
208
+ - lib/rdkit/core_ext.rb
209
+ - lib/rdkit/db.rb
210
+ - lib/rdkit/db_commands.rb
106
211
  - lib/rdkit/errors.rb
107
- - lib/rdkit/inheritable.rb
212
+ - lib/rdkit/http_parser.rb
213
+ - lib/rdkit/http_responder.rb
108
214
  - lib/rdkit/introspection.rb
109
215
  - lib/rdkit/logger.rb
216
+ - lib/rdkit/memory_monitoring.rb
217
+ - lib/rdkit/notification_center.rb
218
+ - lib/rdkit/rd_object.rb
110
219
  - lib/rdkit/resp.rb
111
- - lib/rdkit/resp_runner.rb
220
+ - lib/rdkit/resp_parser.rb
221
+ - lib/rdkit/resp_responder.rb
112
222
  - lib/rdkit/server.rb
223
+ - lib/rdkit/simple_commands.rb
224
+ - lib/rdkit/slow_log.rb
225
+ - lib/rdkit/subcommands.rb
113
226
  - lib/rdkit/version.rb
114
227
  - rdkit.gemspec
228
+ - tmp/.gitkeep
115
229
  homepage: http://github.com/forresty/rdkit
116
230
  licenses: []
117
231
  metadata: {}
@@ -1,15 +0,0 @@
1
- module RDKit
2
- module Inheritable
3
- def self.included(base)
4
- base.instance_eval do
5
- def instance(*args)
6
- @subclass.new(*args)
7
- end
8
-
9
- def inherited(klass)
10
- @subclass = klass
11
- end
12
- end
13
- end
14
- end
15
- end
@@ -1,46 +0,0 @@
1
- module RDKit
2
- class RESPRunner
3
- include Inheritable
4
-
5
- def resp(cmd)
6
- RESP.compose(call(cmd))
7
- rescue => e
8
- RESP.compose(e)
9
- end
10
-
11
- # 获取服务器状态
12
- def info
13
- Introspection.info.map do |type, value|
14
- "# #{type.capitalize}\r\n" + value.map { |k, v| "#{k}:#{v}" }.join("\r\n") + "\r\n"
15
- end.join("\r\n") + "\r\n"
16
- end
17
-
18
- private
19
-
20
- def call(cmd)
21
- @logger ||= Logger.new
22
-
23
- Introspection::Stats.incr(:total_commands_processed)
24
-
25
- @logger.debug "running command: #{cmd}"
26
- cmd, *args = cmd
27
-
28
- cmd.downcase!
29
-
30
- if self.respond_to?(cmd)
31
- self.__send__(cmd, *args)
32
- else
33
- raise UnknownCommandError, "ERR unknown command '#{cmd}'"
34
- end
35
- rescue ArgumentError => e
36
- raise WrongNumberOfArgumentError, "ERR wrong number of arguments for '#{cmd}' command"
37
- end
38
-
39
- module RedisCompatibility
40
- def select(id); 'OK'; end
41
- def ping; 'PONG'; end
42
- end
43
-
44
- include RedisCompatibility
45
- end
46
- end