Class: MemoryLeak::Resources

Inherits:
Object
  • Object
show all
Defined in:
common/memoryleak.rb

Constant Summary collapse

@@refresh_mutex =
Mutex.new
@@resources =
{}
@@refresh_fns =
{}
@@expiration_seconds =
{}

Class Method Summary collapse

Class Method Details

.define(resource, refresh_fn, ttl, opts = {}) ⇒ Object



15
16
17
18
19
20
21
# File 'common/memoryleak.rb', line 15

def self.define(resource, refresh_fn, ttl, opts = {})
  @@resources[resource] = Atomic.new(nil)
  @@refresh_fns[resource] = refresh_fn
  @@expiration_seconds[resource] = ttl if ttl

  self.set(resource, opts[:init], 0)
end

.get(resource) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'common/memoryleak.rb', line 24

def self.get(resource)
  stale = (@@resources[resource].value.nil? ||
           (@@expiration_seconds[resource] &&
            (Time.now.to_i - @@resources[resource].value[:system_mtime]) > @@expiration_seconds[resource]))

  self.refresh(resource) if stale

  @@resources[resource].value[:value]
end

.invalidate_all!Object



51
52
53
54
55
# File 'common/memoryleak.rb', line 51

def self.invalidate_all!
  @@resources.values.each do |atom|
    atom.update {|val| val.merge(:system_mtime => 0) if val}
  end
end

.refresh(resource) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'common/memoryleak.rb', line 35

def self.refresh(resource)
  # Two concurrent users might trigger some resource to be refreshed at
  # around the same time (e.g. when two people create a new repository at
  # once).  We want both refreshes to run in sequence, so use a mutex to
  # serialize them.
  @@refresh_mutex.synchronize do
    self.set(resource, @@refresh_fns[resource].call)
  end
end

.set(resource, value, time = nil) ⇒ Object



46
47
48
# File 'common/memoryleak.rb', line 46

def self.set(resource, value, time = nil)
  @@resources[resource].swap({:value => value, :system_mtime => (time || Time.now.to_i)})
end