Skip to content

Commit ab0b224

Browse files
committed
ruby 0.7.5 with Thread.new each call (not ideal)
1 parent 943cc47 commit ab0b224

File tree

2 files changed

+35
-29
lines changed

2 files changed

+35
-29
lines changed

ruby/lib/pdsdk/base.rb

+34-28
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
require "pdsdk/version"
22
require "pdsdk/logger"
33
require "json"
4-
require 'concurrent'
54

65
module Pdsdk
76
class Error < StandardError
@@ -27,41 +26,48 @@ def bootstrap!
2726
if !@secret_key
2827
logger.warn "no $#{ENV_SECRET_KEY_KEY} detected, will not sign payloads"
2928
end
29+
30+
@hostname = ENV["PD_SDK_HOST"] || "sdk.m.pipedream.net"
31+
@proto = ENV["PD_SDK_PROTO"] || "https"
3032
end
3133

32-
# XXX self.send_message for string and becomes { message } ?
33-
def send_event(api_key, raw_event, opts={}, include_response = false)
34-
hostname = ENV["PD_SDK_HOST"] || "sdk.m.pipedream.net"
35-
proto = ENV["PD_SDK_PROTO"] || "https"
34+
def sync_send_event(api_key, raw_event, opts={}, include_response=false)
3635
event = opts[:exports] || {}
3736
event[:raw_event] = raw_event
37+
# logger.info "going to send event: #{event} to #{api_key}"
3838
if opts[:deployment]
39-
_uri = "#{proto}://#{hostname}/pipelines/#{api_key}/deployments/#{opts[:deployment]}/events"
39+
_uri = "#{@proto}://#{@hostname}/pipelines/#{api_key}/deployments/#{opts[:deployment]}/events"
4040
else
41-
_uri = "#{proto}://#{hostname}/pipelines/#{api_key}/events"
41+
_uri = "#{@proto}://#{@hostname}/pipelines/#{api_key}/events"
4242
end
4343
uri = URI(_uri)
44-
use_ssl = uri.scheme == "https"
45-
# TODO clean up old connections
46-
# TODO ensure reconnects if client disconnects
47-
@http_connection ||= Concurrent::ThreadLocalVar.new { Net::HTTP.start(uri.host, uri.port, use_ssl: use_ssl, open_timeout: 1) }
48-
logger.info "going to send event: #{event} to #{api_key}" # TODO remove
49-
payload = event.to_json
50-
headers = {
51-
"user-agent" => "pipedream-sdk:ruby/1",
52-
"content-type" => "application/json",
53-
"accept" => "application/json",
54-
"x-pd-sdk-version" => Pdsdk::VERSION,
55-
}
56-
headers["x-pd-sig"] = "sha256=#{OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha256'), @secret_key, payload)}" if @secret_key
57-
req = Net::HTTP::Post.new(uri.request_uri, headers)
58-
req.body = payload
59-
resp = @http_connection.value.request(req)
60-
logger.info "received response: #{resp}" # TODO remove
61-
if include_response
62-
{ 'code' => resp.code.to_i, 'body' => resp.body }
63-
else
64-
{ 'code' => resp.code.to_i }
44+
Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https", open_timeout: 1) do |http|
45+
payload = event.to_json
46+
headers = {
47+
"user-agent" => "pipedream-sdk:ruby/1",
48+
"content-type" => "application/json",
49+
"accept" => "application/json",
50+
"x-pd-sdk-version" => Pdsdk::VERSION,
51+
}
52+
headers["x-pd-sig"] = "sha256=#{OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha256'), @secret_key, payload)}" if @secret_key
53+
req = Net::HTTP::Post.new(uri.request_uri, headers)
54+
req.body = payload
55+
resp = http.request(req)
56+
# logger.info "received response: #{resp}"
57+
if include_response
58+
{ 'code' => resp.code.to_i, 'body' => resp.body }
59+
else
60+
{ 'code' => resp.code.to_i }
61+
end
62+
end
63+
end
64+
65+
# XXX self.send_message for string and becomes { message } ?
66+
def send_event(api_key, raw_event, opts={})
67+
# TODO make a proper worker with queue in separate thread rather than making new every time
68+
# ... mostly so we can connect http client once
69+
Thread.new do
70+
sync_send_event(api_key, raw_event, opts)
6571
end
6672
end
6773

ruby/lib/pdsdk/version.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module Pdsdk
2-
VERSION = "0.7.4"
2+
VERSION = "0.7.5"
33
end

0 commit comments

Comments
 (0)