class Radiator::SSC::BaseSteemSmartContractRPC

Constants

MAX_BACKOFF
POST_HEADERS

@private

Public Class Methods

new(options = {}) click to toggle source
# File lib/radiator/ssc/base_steem_smart_contract_rpc.rb, line 12
def initialize(options = {})
  @root_url = options[:root_url] || 'https://api.steem-engine.com/rpc'
  
  @self_hashie_logger = false
  @hashie_logger = if options[:hashie_logger].nil?
    @self_hashie_logger = true
    Logger.new(nil)
  else
    options[:hashie_logger]
  end
  
  unless @hashie_logger.respond_to? :warn
    @hashie_logger = Logger.new(@hashie_logger)
  end
  
  @reuse_ssl_sessions = if options.keys.include? :reuse_ssl_sessions
    options[:reuse_ssl_sessions]
  else
    true
  end
  
  @persist = if options[:persist].nil?
    true
  else
    options[:persist]
  end
  
  if defined? Net::HTTP::Persistent::DEFAULT_POOL_SIZE
    @pool_size = options[:pool_size] || Net::HTTP::Persistent::DEFAULT_POOL_SIZE
  end
  
  Hashie.logger = @hashie_logger
  @uri = nil
  @http_id = nil
  @http = nil
  @max_requests = options[:max_requests] || 30
end

Public Instance Methods

shutdown() click to toggle source

Stops the persistant http connections.

# File lib/radiator/ssc/base_steem_smart_contract_rpc.rb, line 52
def shutdown
  @uri = nil
  @http_id = nil
  @http = nil
end

Protected Instance Methods

healthy?() click to toggle source
# File lib/radiator/ssc/base_steem_smart_contract_rpc.rb, line 137
def healthy?
  warn("Health check not defined for: #{uri}")
  true
end
http() click to toggle source
# File lib/radiator/ssc/base_steem_smart_contract_rpc.rb, line 71
def http
  @http ||= if persist?
    if defined? Net::HTTP::Persistent::DEFAULT_POOL_SIZE
      Net::HTTP::Persistent.new(name: http_id, pool_size: @pool_size).tap do |http|
        http.keep_alive = 30
        http.idle_timeout = 10
        http.max_requests = @max_requests
        http.retry_change_requests = true if defined? http.retry_change_requests
        http.reuse_ssl_sessions = @reuse_ssl_sessions
      end
    else
      # net-http-persistent < 3.0
      Net::HTTP::Persistent.new(http_id) do |http|
        http = Net::HTTP.new(uri.host, uri.port)
        http.use_ssl = uri.scheme == 'https'
      end
    end
  else
    Net::HTTP.new(uri.host, uri.port).tap do |http|
      http.use_ssl = uri.scheme == 'https'
    end
  end
end
http_id() click to toggle source
# File lib/radiator/ssc/base_steem_smart_contract_rpc.rb, line 67
def http_id
  @http_id ||= "radiator-#{Radiator::VERSION}-ssc-blockchain-#{SecureRandom.uuid}"
end
persist?() click to toggle source
# File lib/radiator/ssc/base_steem_smart_contract_rpc.rb, line 142
def persist?
  !!@persist
end
post_request() click to toggle source
# File lib/radiator/ssc/base_steem_smart_contract_rpc.rb, line 95
def post_request
  Net::HTTP::Post.new uri.request_uri, POST_HEADERS
end
request(options) click to toggle source
# File lib/radiator/ssc/base_steem_smart_contract_rpc.rb, line 99
def request(options)
  request = post_request
  skip_health_check = options.delete(:skip_health_check)
  request.body = JSON[options.merge(jsonrpc: '2.0', id: rpc_id)]
  
  unless skip_health_check
    unless healthy?
      @backoff ||= 0.1
      
      backoff = @backoff
      
      if !!backoff
        raise "Too many failures on #{url}" if backoff >= MAX_BACKOFF
        
        backoff *= backoff
        @backoff = backoff
        sleep backoff
      end
    end
    
    @backoff = nil
  end
  
  response = case http
  when Net::HTTP::Persistent then http.request(uri, request)
  when Net::HTTP then http.request(request)
  else; raise ApiError, "Unsuppored scheme: #{http.inspect}"
  end
  
  response = Hashie::Mash.new(JSON[response.body])
  
  if !!(error = response.error)
    raise ApiError, "Error #{error.code}: #{error.message}"
  end
  
  response.result
end
rpc_id() click to toggle source
# File lib/radiator/ssc/base_steem_smart_contract_rpc.rb, line 58
def rpc_id
  @rpc_id ||= 0
  @rpc_id = @rpc_id + 1
end
uri() click to toggle source
# File lib/radiator/ssc/base_steem_smart_contract_rpc.rb, line 63
def uri
  @uri ||= URI.parse(@url)
end