Class: Crea::RPC::ThreadSafeHttpClient

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

Overview

ThreadSafeHttpClient is the default RPC Client used by ‘crea-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_CREA_ADDRESS_PREFIX, ChainConfig::NETWORKS_CREA_CHAIN_ID, ChainConfig::NETWORKS_CREA_CORE_ASSET, ChainConfig::NETWORKS_CREA_DEBT_ASSET, ChainConfig::NETWORKS_CREA_DEFAULT_NODE, ChainConfig::NETWORKS_CREA_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 Crea::RPC::BaseClient

Instance Method Details

#http_postObject

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



15
16
17
18
19
20
# File 'lib/crea/rpc/thread_safe_http_client.rb', line 15

def http_post
  thread = Thread.current
  http_post = thread.thread_variable_get(:http_post)
  http_post ||= Net::HTTP::Post.new(uri.request_uri, POST_HEADERS)
  thread.thread_variable_set(:http_post, http_post)
end

#http_request(request) ⇒ Object



22
# File 'lib/crea/rpc/thread_safe_http_client.rb', line 22

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.



26
27
28
29
30
31
32
# File 'lib/crea/rpc/thread_safe_http_client.rb', line 26

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