class Zold::SpreadEntrance

The entrance

Public Class Methods

new(entrance, wallets, remotes, address, log: Log::NULL, ignore_score_weakeness: false, tolerate_edges: false) click to toggle source
# File lib/zold/node/spread_entrance.rb, line 44
def initialize(entrance, wallets, remotes, address, log: Log::NULL,
  ignore_score_weakeness: false, tolerate_edges: false)
  @entrance = entrance
  @wallets = wallets
  @remotes = remotes
  @address = address
  @log = log
  @ignore_score_weakeness = ignore_score_weakeness
  @tolerate_edges = tolerate_edges
  @mutex = Mutex.new
  @push = ThreadPool.new('spread-entrance')
end

Public Instance Methods

push(id, body) click to toggle source

This method is thread-safe

# File lib/zold/node/spread_entrance.rb, line 97
def push(id, body)
  mods = @entrance.push(id, body)
  return mods if @remotes.all.empty?
  mods.each do |m|
    next if @seen.include?(m)
    @mutex.synchronize { @seen << m }
    @modified.push(m)
    @log.debug("Spread-push scheduled for #{m}, queue size is #{@modified.size}")
  end
  mods
end
start() { |self| ... } click to toggle source
# File lib/zold/node/spread_entrance.rb, line 64
def start
  raise 'Block must be given to start()' unless block_given?
  @entrance.start do
    @seen = Set.new
    @modified = Queue.new
    @push.add do
      Endless.new('push', log: @log).run do
        id = @modified.pop
        if @remotes.all.empty?
          @log.info("There are no remotes, won't spread #{id}")
        elsif @wallets.acq(id) { |w| Tax.new(w).in_debt? }
          @log.info("The wallet #{id} is in debt, won't spread")
        else
          Thread.current.thread_variable_set(:wallet, id.to_s)
          Push.new(wallets: @wallets, remotes: @remotes, log: @log).run(
            ['push', "--ignore-node=#{Shellwords.escape(@address)}", id.to_s, '--tolerate-quorum=1'] +
            (@ignore_score_weakeness ? ['--ignore-score-weakness'] : []) +
            (@tolerate_edges ? ['--tolerate-edges'] : [])
          )
        end
        @mutex.synchronize { @seen.delete(id) }
      end
    end
    begin
      yield(self)
    ensure
      @modified.clear
      @push.kill
    end
  end
end
to_json() click to toggle source
# File lib/zold/node/spread_entrance.rb, line 57
def to_json
  @entrance.to_json.merge(
    'modified': @modified.size,
    'push': @push.to_json
  )
end