regurgitator.git  about / heads / tags
Read-only Rack endpoints for MogileFS and BARFS
blob 3fdefd0e64003431f10abad85a3115dc1359aef5 1465 bytes (raw)
$ git show HEAD:lib/regurgitator/server_settings.rb	# shows this blob on the CLI

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
 
# -*- encoding: binary -*-
# helpers for server_settings lookups

require_relative 'local'
module Regurgitator::ServerSettings # :nodoc:

  def server_settings_init
    @zone_cache_mtime = 0
    @zone_cache = nil
    @zone_cache_lock = Mutex.new
  end

  def self.extended(obj)
    obj.server_settings_init
  end

  def refresh_zone(force = false) # :nodoc:
    @zone_cache_lock.synchronize { refresh_zone_unlocked(force) }
  end

  def refresh_zone_unlocked(force)
    return @zone_cache if !force && ((Regurgitator.now-@zone_cache_mtime) < 60)
    tmp = Patricia.new
    sql = 'SELECT value FROM server_settings WHERE field = ? LIMIT 1'.freeze

    begin
      row = @db[sql, 'network_zones'.freeze].first or return tmp
      row[:value].split(/\s*,\s*/).each do |zone|
        row = @db[sql, "zone_#{zone}"].first or next
        begin
          tmp.add(row[:value], zone)
        rescue ArgumentError
        end
      end
    ensure
      @zone_cache_mtime = Regurgitator.now
      return @zone_cache = tmp
    end
  end

  # If +skip_local+ is true, it may return +:local+ if +addr+ is the address
  # of the host this method is running on
  # Returns the zone (a String) for a given +addr+, if there is one
  # Returns +nil+ if zone information is unknown.
  def zone_for(addr, skip_local = false)
    return :local if skip_local && Regurgitator::Local.include?(addr)
    zone = refresh_zone.search_best(addr) and return zone.data # String
    nil
  end
end

git clone https://yhbt.net/regurgitator.git