fleck 1.0.1 → 2.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +11 -10
- data/CHANGELOG.md +89 -74
- data/Gemfile +6 -4
- data/examples/actions.rb +60 -53
- data/examples/blocking_consumer.rb +42 -42
- data/examples/consumer_initialization.rb +44 -42
- data/examples/deprecation.rb +50 -57
- data/examples/example.rb +76 -74
- data/examples/expired.rb +72 -76
- data/examples/fanout.rb +62 -64
- data/fleck.gemspec +37 -36
- data/lib/fleck/client.rb +124 -124
- data/lib/fleck/configuration.rb +147 -144
- data/lib/fleck/consumer.rb +7 -287
- data/lib/fleck/core/consumer/action_param.rb +106 -0
- data/lib/fleck/core/consumer/actions.rb +79 -0
- data/lib/fleck/core/consumer/base.rb +111 -0
- data/lib/fleck/core/consumer/configuration.rb +69 -0
- data/lib/fleck/core/consumer/decorators.rb +77 -0
- data/lib/fleck/core/consumer/helpers_definers.rb +56 -0
- data/lib/fleck/core/consumer/logger.rb +88 -0
- data/lib/fleck/core/consumer/request.rb +100 -0
- data/lib/fleck/core/consumer/response.rb +77 -0
- data/lib/fleck/core/consumer/response_helpers.rb +81 -0
- data/lib/fleck/core/consumer/validation.rb +163 -0
- data/lib/fleck/core/consumer.rb +166 -0
- data/lib/fleck/core.rb +9 -0
- data/lib/fleck/loggable.rb +15 -10
- data/lib/fleck/{hash_with_indifferent_access.rb → utilities/hash_with_indifferent_access.rb} +80 -85
- data/lib/fleck/utilities/host_rating.rb +104 -0
- data/lib/fleck/version.rb +6 -3
- data/lib/fleck.rb +82 -72
- metadata +35 -24
- data/lib/fleck/consumer/request.rb +0 -52
- data/lib/fleck/consumer/response.rb +0 -80
- data/lib/fleck/host_rating.rb +0 -74
data/examples/example.rb
CHANGED
@@ -1,74 +1,76 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
#
|
3
|
-
|
4
|
-
require 'fleck'
|
5
|
-
|
6
|
-
user = ENV['USER'] || 'guest'
|
7
|
-
pass = ENV['PASS'] || 'guest'
|
8
|
-
|
9
|
-
CONCURRENCY = (ENV['CONCURRENCY'] || 10).to_i
|
10
|
-
SAMPLES = (ENV['SAMPLES'] || 10_000).to_i
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
config.
|
15
|
-
config.
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
end
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
end
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'fleck'
|
5
|
+
|
6
|
+
user = ENV['USER'] || 'guest'
|
7
|
+
pass = ENV['PASS'] || 'guest'
|
8
|
+
|
9
|
+
CONCURRENCY = (ENV['CONCURRENCY'] || 10).to_i
|
10
|
+
SAMPLES = (ENV['SAMPLES'] || 10_000).to_i
|
11
|
+
QUEUE = 'example.queue'
|
12
|
+
|
13
|
+
Fleck.configure do |config|
|
14
|
+
config.default_user = user
|
15
|
+
config.default_pass = pass
|
16
|
+
config.loglevel = Logger::DEBUG
|
17
|
+
end
|
18
|
+
|
19
|
+
client = Fleck::Client.new(Fleck.connection, QUEUE, concurrency: CONCURRENCY.to_i)
|
20
|
+
|
21
|
+
count = 0
|
22
|
+
success = 0
|
23
|
+
failure = 0
|
24
|
+
|
25
|
+
mutex = Mutex.new
|
26
|
+
lock = Mutex.new
|
27
|
+
condition = ConditionVariable.new
|
28
|
+
|
29
|
+
class First < Fleck::Consumer
|
30
|
+
configure queue: QUEUE, concurrency: CONCURRENCY.to_i
|
31
|
+
|
32
|
+
action :incr, 'Returns a message with incremented number'
|
33
|
+
param :num, type: 'number', required: true
|
34
|
+
def incr
|
35
|
+
if rand > 0.1
|
36
|
+
not_found! if request.action != 'incr'
|
37
|
+
|
38
|
+
ok! "#{params[:num].to_i + 1}. Hello, World!"
|
39
|
+
else
|
40
|
+
logger.warn "REJECTING REQUEST {headers: #{headers}, params: #{params}"
|
41
|
+
request.reject!(requeue: true)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
Thread.new do
|
47
|
+
SAMPLES.times do |i|
|
48
|
+
client.request(
|
49
|
+
action: 'incr',
|
50
|
+
params: { num: i, secret: 'supersecret' },
|
51
|
+
async: true,
|
52
|
+
timeout: 1,
|
53
|
+
rmq_options: { priority: (rand * 9).round(0), mandatory: false }
|
54
|
+
) do |request, response|
|
55
|
+
if response.status == 200
|
56
|
+
request.logger.debug response.body
|
57
|
+
else
|
58
|
+
request.logger.error "#{response.status} #{response.errors.join(', ')}"
|
59
|
+
end
|
60
|
+
mutex.synchronize do
|
61
|
+
count += 1
|
62
|
+
if response.status == 200
|
63
|
+
success += 1
|
64
|
+
else
|
65
|
+
failure += 1
|
66
|
+
end
|
67
|
+
|
68
|
+
lock.synchronize { condition.signal } if count >= SAMPLES
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
lock.synchronize { condition.wait(lock) }
|
75
|
+
|
76
|
+
puts "Total: #{count}, Success: #{success}, Failure: #{failure}"
|
data/examples/expired.rb
CHANGED
@@ -1,76 +1,72 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
#
|
3
|
-
|
4
|
-
require 'fleck'
|
5
|
-
|
6
|
-
user = ENV['USER'] || 'guest'
|
7
|
-
pass = ENV['PASS'] || 'guest'
|
8
|
-
|
9
|
-
CONCURRENCY = (ENV['CONCURRENCY'] || 2).to_i
|
10
|
-
SAMPLES = (ENV['SAMPLES'] || 10).to_i
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
config.
|
15
|
-
config.
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
end
|
74
|
-
|
75
|
-
lock.synchronize { condition.wait(lock) }
|
76
|
-
exit
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'fleck'
|
5
|
+
|
6
|
+
user = ENV['USER'] || 'guest'
|
7
|
+
pass = ENV['PASS'] || 'guest'
|
8
|
+
|
9
|
+
CONCURRENCY = (ENV['CONCURRENCY'] || 2).to_i
|
10
|
+
SAMPLES = (ENV['SAMPLES'] || 10).to_i
|
11
|
+
QUEUE = 'expiration.example.queue'
|
12
|
+
|
13
|
+
Fleck.configure do |config|
|
14
|
+
config.default_user = user
|
15
|
+
config.default_pass = pass
|
16
|
+
config.loglevel = Logger::DEBUG
|
17
|
+
end
|
18
|
+
|
19
|
+
connection = Fleck.connection(host: '127.0.0.1', port: 5672, user: user, pass: pass, vhost: '/')
|
20
|
+
client = Fleck::Client.new(connection, QUEUE, concurrency: CONCURRENCY.to_i)
|
21
|
+
success_probability = 0.8
|
22
|
+
|
23
|
+
count = 0
|
24
|
+
success = 0
|
25
|
+
failure = 0
|
26
|
+
|
27
|
+
mutex = Mutex.new
|
28
|
+
lock = Mutex.new
|
29
|
+
condition = ConditionVariable.new
|
30
|
+
|
31
|
+
class MyConsumer < Fleck::Consumer
|
32
|
+
configure queue: QUEUE, concurrency: CONCURRENCY.to_i
|
33
|
+
|
34
|
+
action :hello
|
35
|
+
def hello
|
36
|
+
sleep rand
|
37
|
+
ok! "#{params[:num] + 1}. Hello!"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
SAMPLES.to_i.times do |num|
|
42
|
+
client.request(
|
43
|
+
action: 'hello',
|
44
|
+
params: { num: num },
|
45
|
+
async: true,
|
46
|
+
timeout: (success_probability * SAMPLES.to_f) / CONCURRENCY
|
47
|
+
) do |request, response|
|
48
|
+
if request.expired?
|
49
|
+
puts "EXPIRED: #{response.inspect}"
|
50
|
+
elsif response.status == 200
|
51
|
+
puts "SUCCESS: #{response.body}"
|
52
|
+
else
|
53
|
+
puts "ERROR: #{response.inspect}"
|
54
|
+
end
|
55
|
+
|
56
|
+
mutex.synchronize do
|
57
|
+
count += 1
|
58
|
+
if response.status == 200
|
59
|
+
success += 1
|
60
|
+
else
|
61
|
+
failure += 1
|
62
|
+
end
|
63
|
+
|
64
|
+
lock.synchronize { condition.signal } if count >= SAMPLES
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
lock.synchronize { condition.wait(lock) }
|
70
|
+
|
71
|
+
puts "Ztimer: (count: #{Ztimer.count}, jobs: #{Ztimer.jobs_count})"
|
72
|
+
puts "Total: #{count}, Success: #{success}, Failure: #{failure}"
|
data/examples/fanout.rb
CHANGED
@@ -1,64 +1,62 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
# encoding: utf-8
|
3
|
-
|
4
|
-
require 'fleck'
|
5
|
-
|
6
|
-
user = ENV['USER'] || 'guest'
|
7
|
-
pass = ENV['PASS'] || 'guest'
|
8
|
-
|
9
|
-
CONCURRENCY = (ENV['CONCURRENCY'] || 10).to_i
|
10
|
-
SAMPLES = (ENV['SAMPLES'] || 1_000).to_i
|
11
|
-
|
12
|
-
Fleck.configure do |config|
|
13
|
-
config.default_user = user
|
14
|
-
config.default_pass = pass
|
15
|
-
config.loglevel = Logger::DEBUG
|
16
|
-
end
|
17
|
-
|
18
|
-
connection = Fleck.connection(host: "127.0.0.1", port: 5672, user: user, pass: pass, vhost: "/")
|
19
|
-
client = Fleck::Client.new(connection, "example.queue", concurrency: CONCURRENCY.to_i, exchange_type: :fanout, exchange_name: 'fanout.example.queue', multiple_responses: true)
|
20
|
-
|
21
|
-
count = 0
|
22
|
-
success = 0
|
23
|
-
failure = 0
|
24
|
-
|
25
|
-
mutex = Mutex.new
|
26
|
-
lock = Mutex.new
|
27
|
-
condition = ConditionVariable.new
|
28
|
-
|
29
|
-
class First < Fleck::Consumer
|
30
|
-
configure queue: "example.queue", concurrency: CONCURRENCY.to_i, exchange_type: :fanout, exchange_name: 'fanout.example.queue'
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
end
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
end
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
lock.synchronize { condition.wait(lock) }
|
64
|
-
exit
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# encoding: utf-8
|
3
|
+
|
4
|
+
require 'fleck'
|
5
|
+
|
6
|
+
user = ENV['USER'] || 'guest'
|
7
|
+
pass = ENV['PASS'] || 'guest'
|
8
|
+
|
9
|
+
CONCURRENCY = (ENV['CONCURRENCY'] || 10).to_i
|
10
|
+
SAMPLES = (ENV['SAMPLES'] || 1_000).to_i
|
11
|
+
|
12
|
+
Fleck.configure do |config|
|
13
|
+
config.default_user = user
|
14
|
+
config.default_pass = pass
|
15
|
+
config.loglevel = Logger::DEBUG
|
16
|
+
end
|
17
|
+
|
18
|
+
connection = Fleck.connection(host: "127.0.0.1", port: 5672, user: user, pass: pass, vhost: "/")
|
19
|
+
client = Fleck::Client.new(connection, "example.queue", concurrency: CONCURRENCY.to_i, exchange_type: :fanout, exchange_name: 'fanout.example.queue', multiple_responses: true)
|
20
|
+
|
21
|
+
count = 0
|
22
|
+
success = 0
|
23
|
+
failure = 0
|
24
|
+
|
25
|
+
mutex = Mutex.new
|
26
|
+
lock = Mutex.new
|
27
|
+
condition = ConditionVariable.new
|
28
|
+
|
29
|
+
class First < Fleck::Consumer
|
30
|
+
configure queue: "example.queue", concurrency: CONCURRENCY.to_i, exchange_type: :fanout, exchange_name: 'fanout.example.queue'
|
31
|
+
|
32
|
+
action :incr
|
33
|
+
def incr
|
34
|
+
if request.action == 'incr'
|
35
|
+
ok! "#{request.params[:num].to_i + 1}. Hello, World!"
|
36
|
+
else
|
37
|
+
not_found!
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
Thread.new do
|
43
|
+
SAMPLES.times do |i|
|
44
|
+
client.request(action: 'incr', params: { num: i }, timeout: 60) do |request, response|
|
45
|
+
request.logger.debug response.body
|
46
|
+
mutex.synchronize do
|
47
|
+
count += 1
|
48
|
+
if response.status == 200
|
49
|
+
success += 1
|
50
|
+
else
|
51
|
+
failure += 1
|
52
|
+
end
|
53
|
+
|
54
|
+
lock.synchronize { condition.signal } if count >= SAMPLES
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
lock.synchronize { condition.wait(lock) }
|
61
|
+
|
62
|
+
puts "Total: #{count}, Success: #{success}, Failure: #{failure}"
|
data/fleck.gemspec
CHANGED
@@ -1,36 +1,37 @@
|
|
1
|
-
#
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
spec.
|
9
|
-
spec.
|
10
|
-
spec.
|
11
|
-
spec.
|
12
|
-
|
13
|
-
|
14
|
-
spec.
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
spec.
|
20
|
-
|
21
|
-
|
22
|
-
spec.
|
23
|
-
spec.
|
24
|
-
spec.
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
spec.add_development_dependency
|
30
|
-
spec.add_development_dependency
|
31
|
-
spec.
|
32
|
-
spec.add_dependency
|
33
|
-
spec.add_dependency
|
34
|
-
spec.add_dependency
|
35
|
-
spec.add_dependency
|
36
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path(__dir__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require 'lib/fleck/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = 'fleck'
|
9
|
+
spec.platform = 'ruby'
|
10
|
+
spec.version = Fleck::VERSION
|
11
|
+
spec.authors = ['Groza Sergiu']
|
12
|
+
spec.email = ['serioja90@gmail.com']
|
13
|
+
|
14
|
+
spec.summary = %(A Ruby gem for syncronous and asyncronous communication via Message Queue services.)
|
15
|
+
spec.description = %(
|
16
|
+
Fleck is a library for syncronous and asyncronous communication over Message Queues services. Unlike a common
|
17
|
+
HTTP communication, Fleck requests and responses are pure JSON messages.
|
18
|
+
)
|
19
|
+
spec.homepage = 'https://github.com/serioja90/fleck'
|
20
|
+
spec.license = 'MIT'
|
21
|
+
|
22
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
23
|
+
spec.bindir = 'exe'
|
24
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
25
|
+
spec.require_paths = ['lib']
|
26
|
+
|
27
|
+
spec.required_ruby_version = '>= 2.5'
|
28
|
+
|
29
|
+
spec.add_development_dependency 'bundler', '>= 2.2.33'
|
30
|
+
spec.add_development_dependency 'rake', '~> 13.0'
|
31
|
+
spec.add_development_dependency 'rspec', '~> 3.9'
|
32
|
+
spec.add_dependency 'bunny', '~> 2.14'
|
33
|
+
spec.add_dependency 'oj', '~> 3.10'
|
34
|
+
spec.add_dependency 'rainbow', '~> 2.2'
|
35
|
+
spec.add_dependency 'thread_safe', '~> 0.3'
|
36
|
+
spec.add_dependency 'ztimer', '~> 0.6'
|
37
|
+
end
|