bundle_requests 0.0.2
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.
- checksums.yaml +7 -0
- data/lib/bundle_requests.rb +5 -0
- data/lib/bundle_requests/consumer.rb +106 -0
- data/lib/bundle_requests/rack_middleware.rb +39 -0
- metadata +46 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 16807dcb8673b52b5078317c75c9c5da5d9b2f13
|
4
|
+
data.tar.gz: d2b83331927cd1751592de16248104f0b795a1d8
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: dc8937bdda634d1c7d92d1b144f07ab402d0c91e68f5f76a7af3ebe112a41b05407acf45bb6076e3bce18d3edec215f1b97a2ba0cb1b19fe44ec336983e26586
|
7
|
+
data.tar.gz: b03eade7bea825d63108e3fa5ad5d61d6b38ba62a2a7c6f0b26507e7ceff47702d636b335d3f394c421baba214b2d6587467586352c14cd05084bfacb478d3b5
|
@@ -0,0 +1,106 @@
|
|
1
|
+
module BundleRequests
|
2
|
+
class Consumer
|
3
|
+
def initialize(app , config)
|
4
|
+
@app = app
|
5
|
+
$waiting_threads = Queue.new
|
6
|
+
generate_config_hash(config)
|
7
|
+
Thread.new{consumer_code}
|
8
|
+
end
|
9
|
+
|
10
|
+
def get_waiting_threads
|
11
|
+
$waiting_threads
|
12
|
+
end
|
13
|
+
|
14
|
+
def get_configuration
|
15
|
+
$configuration
|
16
|
+
end
|
17
|
+
# List All Instance Variables here for reference
|
18
|
+
# - @app
|
19
|
+
# - $configuration
|
20
|
+
# - @consumer
|
21
|
+
# - $waiting_threads
|
22
|
+
|
23
|
+
def consumer_code
|
24
|
+
while true
|
25
|
+
c = $waiting_threads.length
|
26
|
+
if c < $configuration["max_waiting_thread"]
|
27
|
+
puts "#{c} threads are waiting so sleeping"
|
28
|
+
sleep($configuration["wait_time"])
|
29
|
+
next if $waiting_threads.length == 0
|
30
|
+
end
|
31
|
+
puts "Started request processing----------------------------------"
|
32
|
+
threads = pop_some_waiting_threads
|
33
|
+
rack_input = gather_all_requests(threads)
|
34
|
+
result = call_bundle_api(rack_input,threads[0]['request']) # may through exception if 0 threads are present remove it afterwards
|
35
|
+
distribute_result_and_wakeup_those_threads(threads, result)
|
36
|
+
puts "Completed proccessing requests------------------------------"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def pop_some_waiting_threads
|
41
|
+
threads = []
|
42
|
+
$waiting_threads.length.times do
|
43
|
+
t = $waiting_threads.pop
|
44
|
+
threads << t
|
45
|
+
end
|
46
|
+
Rails.logger.info("Currently proccessing #{threads.length} threads")
|
47
|
+
threads
|
48
|
+
end
|
49
|
+
|
50
|
+
def gather_all_requests(threads)
|
51
|
+
rack_input = []
|
52
|
+
threads.each do |t|
|
53
|
+
e = t['request']
|
54
|
+
req = Rack::Request.new(e)
|
55
|
+
Rails.logger.debug req.inspect
|
56
|
+
rack_input << JSON.parse(req.body.read)
|
57
|
+
end
|
58
|
+
Rails.logger.info rack_input
|
59
|
+
rack_input
|
60
|
+
end
|
61
|
+
|
62
|
+
def call_bundle_api(rack_input,default_env={})
|
63
|
+
env = default_env # if this doesnt work assign myenv to some env of any threads
|
64
|
+
env['PATH_INFO'] = $configuration['bundle_api']
|
65
|
+
env['QUERY_STRING'] = ''
|
66
|
+
env['REQUEST_METHOD'] = 'POST'
|
67
|
+
env['CONTENT_LENGTH'] = {'requests' => rack_input}.to_json.length
|
68
|
+
env['rack.input'] = StringIO.new({'requests' => rack_input}.to_json)
|
69
|
+
request = Rack::Request.new(env)
|
70
|
+
Rails.logger.info("new Environment is #{env}")
|
71
|
+
Rails.logger.info("New request content are #{request}")
|
72
|
+
result = @app.call(env)
|
73
|
+
Rails.logger.info("Result are #{result}")
|
74
|
+
result
|
75
|
+
end
|
76
|
+
|
77
|
+
def distribute_result_and_wakeup_those_threads(threads, result)
|
78
|
+
for index in 0...threads.length
|
79
|
+
t = threads[index]
|
80
|
+
# hardcoded response for development. Replace it with result[index]
|
81
|
+
t["response"] = [200,{"Content-Type" => "application/json"},["Status Ok"]]
|
82
|
+
t.wakeup
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def generate_config_hash(options)
|
87
|
+
if $configuration.nil?
|
88
|
+
config = {
|
89
|
+
"incoming_request" => "/api",
|
90
|
+
"bundle_api" => "/bundle_api",
|
91
|
+
"wait_time" => 10,
|
92
|
+
"thread_wait_after_closing_entrance" => 2,
|
93
|
+
"max_waiting_thread" => 16
|
94
|
+
}
|
95
|
+
|
96
|
+
options.each do |key,value|
|
97
|
+
if !value.nil?
|
98
|
+
config[key] = value
|
99
|
+
end
|
100
|
+
end
|
101
|
+
$configuration = config
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
end
|
106
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'bundle_requests/consumer'
|
2
|
+
module BundleRequests
|
3
|
+
class RackMiddleware
|
4
|
+
def initialize(app, config={})
|
5
|
+
@app = app
|
6
|
+
start_consumer(app, config)
|
7
|
+
end
|
8
|
+
# @consumer
|
9
|
+
# @app
|
10
|
+
# $waiting_threads
|
11
|
+
# $configuration
|
12
|
+
|
13
|
+
def start_consumer(app, config)
|
14
|
+
Mutex.new.synchronize do
|
15
|
+
if @consumer.nil?
|
16
|
+
@consumer = BundleRequests::Consumer.new(app, config) # cretes new infinite thread
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def call env
|
22
|
+
Rails.logger.info("request #{env['REQUEST_PATH']} #{Thread.current.name}")
|
23
|
+
s = Time.now
|
24
|
+
if env['REQUEST_PATH'] == $configuration['incoming_request']
|
25
|
+
Thread.current['request'] = env
|
26
|
+
$waiting_threads << Thread.current
|
27
|
+
puts "I am waiting #{Thread.current.object_id}"
|
28
|
+
Thread.stop
|
29
|
+
response = Thread.current['response']
|
30
|
+
else
|
31
|
+
puts "[Not bundle api]"
|
32
|
+
response = @app.call env
|
33
|
+
end
|
34
|
+
puts "TIME required for request to process is -#{Time.now - s} "
|
35
|
+
response
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
end
|
metadata
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: bundle_requests
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Prashant Lokhande
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-04-11 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description:
|
14
|
+
email: lprashant94@gmail.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/bundle_requests.rb
|
20
|
+
- lib/bundle_requests/consumer.rb
|
21
|
+
- lib/bundle_requests/rack_middleware.rb
|
22
|
+
homepage: https://github.com/lprashant-94/bundle_requests/blob/master/README.md
|
23
|
+
licenses:
|
24
|
+
- Nonstandard
|
25
|
+
metadata: {}
|
26
|
+
post_install_message:
|
27
|
+
rdoc_options: []
|
28
|
+
require_paths:
|
29
|
+
- lib
|
30
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
requirements: []
|
41
|
+
rubyforge_project:
|
42
|
+
rubygems_version: 2.5.1
|
43
|
+
signing_key:
|
44
|
+
specification_version: 4
|
45
|
+
summary: It Bundles same kind of requests and process in batch
|
46
|
+
test_files: []
|