Skip to content

Commit

Permalink
feat(deliver): improve concurrency in send_req by utilizing WaitGroup…
Browse files Browse the repository at this point in the history
… for endpoint processing

Signed-off-by: HAHWUL <hahwul@gmail.com>
  • Loading branch information
hahwul committed Jan 18, 2025
1 parent db80b49 commit f100dbb
Showing 1 changed file with 39 additions and 29 deletions.
68 changes: 39 additions & 29 deletions src/deliver/send_req.cr
Original file line number Diff line number Diff line change
@@ -1,44 +1,54 @@
require "crest"
require "wait_group"
require "../utils/http_symbols"
require "../models/deliver"

class SendReq < Deliver
def run(endpoints : Array(Endpoint))
applied_endpoints = apply_all(endpoints)
wg = WaitGroup.new

applied_endpoints.each do |endpoint|
begin
if endpoint.params.size > 0
endpoint_hash = endpoint.params_to_hash
body = {} of String => String
is_json = false
if endpoint_hash["json"].size > 0
is_json = true
body = endpoint_hash["json"]
wg.add(1)
spawn do
begin
if endpoint.params.size > 0
endpoint_hash = endpoint.params_to_hash
body = {} of String => String
is_json = false
if endpoint_hash["json"].size > 0
is_json = true
body = endpoint_hash["json"]
else
body = endpoint_hash["form"]
end

Crest::Request.execute(
method: get_symbol(endpoint.method),
url: endpoint.url,
tls: OpenSSL::SSL::Context::Client.insecure,
user_agent: "Noir/#{Noir::VERSION}",
params: endpoint_hash["query"],
form: body,
headers: @headers,
json: is_json
)
else
body = endpoint_hash["form"]
Crest::Request.execute(
method: get_symbol(endpoint.method),
url: endpoint.url,
headers: @headers,
tls: OpenSSL::SSL::Context::Client.insecure,
user_agent: "Noir/#{Noir::VERSION}"
)
end

Crest::Request.execute(
method: get_symbol(endpoint.method),
url: endpoint.url,
tls: OpenSSL::SSL::Context::Client.insecure,
user_agent: "Noir/#{Noir::VERSION}",
params: endpoint_hash["query"],
form: body,
headers: @headers,
json: is_json
)
else
Crest::Request.execute(
method: get_symbol(endpoint.method),
url: endpoint.url,
headers: @headers,
tls: OpenSSL::SSL::Context::Client.insecure,
user_agent: "Noir/#{Noir::VERSION}"
)
rescue
ensure
wg.done
end
rescue
end
end

wg.wait
end
end

0 comments on commit f100dbb

Please sign in to comment.