Class: Hive::RPC::ThreadSafeHttpClient

Inherits:
HttpClient show all
Defined in:
lib/hive/rpc/thread_safe_http_client.rb

Overview

ThreadSafeHttpClient is the default RPC Client used by ‘hive-ruby.` It’s perfect for simple requests. But for higher performance, it’s better to override HttpClient and implement something other than Net::HTTP.

It performs http requests in a Mutex critical section because Net::HTTP is not thread safe. This is the very minimum level thread safety available.

Constant Summary collapse

SEMAPHORE =
Mutex.new.freeze

Constants inherited from HttpClient

HttpClient::JSON_RPC_BATCH_SIZE_MAXIMUM, HttpClient::POST_HEADERS, HttpClient::TIMEOUT_ERRORS

Constants inherited from BaseClient

BaseClient::MAX_TIMEOUT_BACKOFF, BaseClient::MAX_TIMEOUT_RETRY_COUNT, BaseClient::TIMEOUT_ERRORS

Constants included from ChainConfig

ChainConfig::EXPIRE_IN_SECS, ChainConfig::EXPIRE_IN_SECS_PROPOSAL, ChainConfig::NETWORKS_HIVE_ADDRESS_PREFIX, ChainConfig::NETWORKS_HIVE_CHAIN_ID, ChainConfig::NETWORKS_HIVE_CORE_ASSET, ChainConfig::NETWORKS_HIVE_DEBT_ASSET, ChainConfig::NETWORKS_HIVE_DEFAULT_NODE, ChainConfig::NETWORKS_HIVE_LEGACY_CHAIN_ID, ChainConfig::NETWORKS_HIVE_VEST_ASSET, ChainConfig::NETWORKS_TEST_ADDRESS_PREFIX, ChainConfig::NETWORKS_TEST_CHAIN_ID, ChainConfig::NETWORKS_TEST_CORE_ASSET, ChainConfig::NETWORKS_TEST_DEBT_ASSET, ChainConfig::NETWORKS_TEST_DEFAULT_NODE, ChainConfig::NETWORKS_TEST_VEST_ASSET, ChainConfig::NETWORK_CHAIN_IDS

Instance Attribute Summary

Attributes inherited from BaseClient

#chain, #error_pipe, #url

Instance Method Summary collapse

Methods inherited from HttpClient

#http, #rpc_batch_execute, #rpc_execute

Methods inherited from BaseClient

#evaluate_id, #initialize, #put, #rpc_batch_execute, #rpc_execute, #uri, #yield_response

Constructor Details

This class inherits a constructor from Hive::RPC::BaseClient

Instance Method Details

#http_post(api_name) ⇒ Object

Same as #HttpClient#http_post, but scoped to each thread, uri, and api_name so it is thread safe.



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/hive/rpc/thread_safe_http_client.rb', line 15

def http_post(api_name)
  raise "Namespace required." if api_name.nil?
  
  thread = Thread.current
  http_posts = thread.thread_variable_get(:http_posts) || {}
  
  SEMAPHORE.synchronize do
    http_posts[[uri, api_name]] ||= Net::HTTP::Post.new(uri.request_uri, POST_HEADERS)
    thread.thread_variable_set(:http_posts, http_posts)
  end
  
  http_posts[[uri, api_name]]
end

#http_request(request) ⇒ Object



29
# File 'lib/hive/rpc/thread_safe_http_client.rb', line 29

def http_request(request); SEMAPHORE.synchronize{super}; end

#rpc_idObject

Same as #BaseClient#rpc_id, auto-increment, but scoped to each thread so it is thread safe.



33
34
35
36
37
38
39
# File 'lib/hive/rpc/thread_safe_http_client.rb', line 33

def rpc_id
  thread = Thread.current
  rpc_id = thread.thread_variable_get(:rpc_id)
  rpc_id ||= 0
  rpc_id += 1
  thread.thread_variable_set(:rpc_id, rpc_id)
end