Class: Agent::Lock::Facade::Memo

Inherits:
Object
  • Object
show all
Defined in:
lib/agent/lock/facade/memo.rb

Overview

One value, built on first use by exactly one thread and reused after.

A build that raises is not kept, so the next call tries again: a Redis that was down for Agent::Lock.distributed is retried, not remembered as down. Each memo has its own mutex, so a slow build of one never blocks another.

Instance Method Summary collapse

Constructor Details

#initialize(&build) ⇒ Memo

Returns a new instance of Memo.

Yield Returns:

  • (Object) —

    the value, built under the memo's mutex



14
15
16
17
18
# File 'lib/agent/lock/facade/memo.rb', line 14

def initialize(&build)
  @build = build
  @mutex = Mutex.new
  @value = nil
end

Instance Method Details

#reset ⇒ void

This method returns an undefined value.

Forget the value, so the next #value builds a new one.



26
# File 'lib/agent/lock/facade/memo.rb', line 26

def reset = @mutex.synchronize { @value = nil }

#value ⇒ Object

Returns the value, building it if this is the first call.

Returns:

  • (Object) —

    the value, building it if this is the first call



21
# File 'lib/agent/lock/facade/memo.rb', line 21

def value = @mutex.synchronize { @value ||= @build.call }