Class: Zold::Remotes

Inherits:
Dry::Struct
  • Object
show all
Defined in:
lib/zold/remotes.rb

Overview

All remotes

Defined Under Namespace

Classes: Empty, Remote

Constant Summary collapse

PORT =

The default TCP port all nodes are supposed to use.

4096
TOLERANCE =

At what amount of errors we delete the remote automatically

8
MAX_NODES =

Default number of nodes to fetch.

16

Instance Method Summary collapse

Instance Method Details

#add(host, port = Remotes::PORT) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/zold/remotes.rb', line 151

def add(host, port = Remotes::PORT)
  raise 'Host can\'t be nil' if host.nil?
  raise 'Host can\'t be empty' if host.empty?
  raise 'Port can\'t be nil' if port.nil?
  raise 'Port has to be of type Integer' unless port.is_a?(Integer)
  raise 'Port can\'t be zero' if port.zero?
  raise 'Port can\'t be negative' if port.negative?
  raise 'Port can\'t be over 65536' if port > 0xffff
  modify do |list|
    list + [{ host: host.downcase, port: port, score: 0, errors: 0 }]
  end
end

#allObject



122
123
124
125
126
127
128
129
130
131
# File 'lib/zold/remotes.rb', line 122

def all
  list = load
  max_score = list.map { |r| r[:score] }.max || 0
  max_score = 1 if max_score.zero?
  max_errors = list.map { |r| r[:errors] }.max || 0
  max_errors = 1 if max_errors.zero?
  list.sort_by do |r|
    (1 - r[:errors] / max_errors) * 5 + (r[:score] / max_score)
  end.reverse
end

#cleanObject



133
134
135
# File 'lib/zold/remotes.rb', line 133

def clean
  modify { [] }
end

#defaultsObject



137
138
139
140
141
142
# File 'lib/zold/remotes.rb', line 137

def defaults
  other = Remotes.new(file: File.expand_path(File.join(File.dirname(__FILE__), '../../resources/remotes')))
  other.all.each do |r|
    add(r[:host], r[:port])
  end
end

#error(host, port = Remotes::PORT) ⇒ Object



211
212
213
214
215
216
# File 'lib/zold/remotes.rb', line 211

def error(host, port = Remotes::PORT)
  raise 'Host can\'t be nil' if host.nil?
  raise 'Port can\'t be nil' if port.nil?
  raise 'Port has to be of type Integer' unless port.is_a?(Integer)
  if_present(host, port) { |r| r[:errors] += 1 }
end

#exists?(host, port = Remotes::PORT) ⇒ Boolean

Returns:

  • (Boolean)


144
145
146
147
148
149
# File 'lib/zold/remotes.rb', line 144

def exists?(host, port = Remotes::PORT)
  raise 'Port has to be of type Integer' unless port.is_a?(Integer)
  raise 'Host can\'t be nil' if host.nil?
  raise 'Port can\'t be nil' if port.nil?
  !load.find { |r| r[:host] == host.downcase && r[:port] == port }.nil?
end

#iterate(log, farm: Farm::Empty.new) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/zold/remotes.rb', line 173

def iterate(log, farm: Farm::Empty.new)
  raise 'Log can\'t be nil' if log.nil?
  raise 'Farm can\'t be nil' if farm.nil?
  list = all
  return if list.empty?
  best = farm.best[0]
  require_relative 'score'
  score = best.nil? ? Score::ZERO : best
  idx = 0
  pool = Concurrent::FixedThreadPool.new([list.count, Concurrent.processor_count * 4].min, max_queue: 0)
  list.each do |r|
    pool.post do
      Thread.current.abort_on_exception = true
      Thread.current.name = "remotes-#{idx}@#{r[:host]}:#{r[:port]}"
      start = Time.now
      begin
        yield Remotes::Remote.new(
          host: r[:host],
          port: r[:port],
          score: score,
          idx: idx,
          log: log,
          network: network
        )
        raise 'Took too long to execute' if (Time.now - start).round > timeout
      rescue StandardError => e
        error(r[:host], r[:port])
        log.info("#{Rainbow("#{r[:host]}:#{r[:port]}").red}: #{e.message} in #{Age.new(start)}")
        log.debug(Backtrace.new(e).to_s)
        remove(r[:host], r[:port]) if errors > Remotes::TOLERANCE
      end
    end
    idx += 1
  end
  pool.shutdown
  pool.kill unless pool.wait_for_termination(5 * 60)
end

#mtimeObject



226
227
228
# File 'lib/zold/remotes.rb', line 226

def mtime
  File.exist?(file) ? File.mtime(file) : Time.now
end

#remove(host, port = Remotes::PORT) ⇒ Object



164
165
166
167
168
169
170
171
# File 'lib/zold/remotes.rb', line 164

def remove(host, port = Remotes::PORT)
  raise 'Port has to be of type Integer' unless port.is_a?(Integer)
  raise 'Host can\'t be nil' if host.nil?
  raise 'Port can\'t be nil' if port.nil?
  modify do |list|
    list.reject { |r| r[:host] == host.downcase && r[:port] == port }
  end
end

#rescore(host, port, score) ⇒ Object



218
219
220
221
222
223
224
# File 'lib/zold/remotes.rb', line 218

def rescore(host, port, score)
  raise 'Host can\'t be nil' if host.nil?
  raise 'Port can\'t be nil' if port.nil?
  raise 'Score can\'t be nil' if score.nil?
  raise 'Port has to be of type Integer' unless port.is_a?(Integer)
  if_present(host, port) { |r| r[:score] = score }
end