Class: Zold::Remotes
- Inherits:
-
Object
- Object
- Zold::Remotes
- Defined in:
- lib/zold/remotes.rb
Overview
All remotes
Defined Under Namespace
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
- #add(host, port = PORT) ⇒ Object
- #all ⇒ Object
- #clean ⇒ Object
- #error(host, port = PORT) ⇒ Object
- #exists?(host, port = PORT) ⇒ Boolean
-
#initialize(file:, network: 'test', timeout: 60) ⇒ Remotes
constructor
A new instance of Remotes.
-
#iterate(log, farm: Farm::Empty.new) ⇒ Object
Go through the list of remotes and call a provided block for each of them.
- #master?(host, port) ⇒ Boolean
- #masters ⇒ Object
- #mtime ⇒ Object
- #remove(host, port = PORT) ⇒ Object
- #rescore(host, port, score) ⇒ Object
Constructor Details
#initialize(file:, network: 'test', timeout: 60) ⇒ Remotes
Returns a new instance of Remotes.
130 131 132 133 134 |
# File 'lib/zold/remotes.rb', line 130 def initialize(file:, network: 'test', timeout: 60) @file = file @network = network @timeout = timeout end |
Instance Method Details
#add(host, port = PORT) ⇒ Object
168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/zold/remotes.rb', line 168 def add(host, port = 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 |
#all ⇒ Object
136 137 138 139 140 141 142 143 144 145 |
# File 'lib/zold/remotes.rb', line 136 def all list = Futex.new(@file).open(false) { 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 |
#clean ⇒ Object
147 148 149 |
# File 'lib/zold/remotes.rb', line 147 def clean modify { [] } end |
#error(host, port = PORT) ⇒ Object
218 219 220 221 222 223 |
# File 'lib/zold/remotes.rb', line 218 def error(host, port = 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 = PORT) ⇒ Boolean
160 161 162 163 164 165 166 |
# File 'lib/zold/remotes.rb', line 160 def exists?(host, port = 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? list = Futex.new(@file).open(false) { load } !list.find { |r| r[:host] == host.downcase && r[:port] == port }.nil? end |
#iterate(log, farm: Farm::Empty.new) ⇒ Object
Go through the list of remotes and call a provided block for each of them. See how it’s used, for example, in fetch.rb.
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/zold/remotes.rb', line 192 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? ThreadPool.new('remotes', log: log).run(Concurrent.processor_count * 4, all) do |r, idx| Thread.current.name = "remotes-#{idx}@#{r[:host]}:#{r[:port]}" start = Time.now best = farm.best[0] begin yield Remotes::Remote.new( host: r[:host], port: r[:port], score: best.nil? ? Score::ZERO : best, 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.} in #{Age.new(start)}") log.debug(Backtrace.new(e).to_s) remove(r[:host], r[:port]) if r[:errors] > TOLERANCE end end end |
#master?(host, port) ⇒ Boolean
238 239 240 |
# File 'lib/zold/remotes.rb', line 238 def master?(host, port) !MASTERS.find { |r| r[0] == host && r[1].to_i == port }.nil? end |
#masters ⇒ Object
151 152 153 154 155 156 157 158 |
# File 'lib/zold/remotes.rb', line 151 def masters MASTERS.each do |r| if block_given? next unless yield(r[0], r[1].to_i) end add(r[0], r[1].to_i) end end |
#mtime ⇒ Object
234 235 236 |
# File 'lib/zold/remotes.rb', line 234 def mtime File.exist?(@file) ? File.mtime(@file) : Time.now end |
#remove(host, port = PORT) ⇒ Object
181 182 183 184 185 186 187 188 |
# File 'lib/zold/remotes.rb', line 181 def remove(host, port = 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
225 226 227 228 229 230 231 232 |
# File 'lib/zold/remotes.rb', line 225 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) raise 'Score has to be of type Integer' unless score.is_a?(Integer) if_present(host, port) { |r| r[:score] = score } end |