Class: Datadog::Core::Utils::OnlyOnce
- Inherits:
-
Object
- Object
- Datadog::Core::Utils::OnlyOnce
- Defined in:
- lib/datadog/core/utils/only_once.rb
Overview
Helper class to execute something only once such as not repeating warning logs, and instrumenting classes only once.
Thread-safe when used correctly (e.g. be careful of races when lazily initializing instances of this class).
Note: In its current state, this class is not Ractor-safe. In github.com/DataDog/dd-trace-rb/pull/1398#issuecomment-797378810 we have a discussion of alternatives, including an alternative implementation that is Ractor-safe once spent.
Direct Known Subclasses
Instance Method Summary collapse
-
#initialize ⇒ OnlyOnce
constructor
A new instance of OnlyOnce.
- #ran? ⇒ Boolean
- #run ⇒ Object
Constructor Details
#initialize ⇒ OnlyOnce
Returns a new instance of OnlyOnce.
15 16 17 18 |
# File 'lib/datadog/core/utils/only_once.rb', line 15 def initialize @mutex = Mutex.new @ran_once = false end |
Instance Method Details
#ran? ⇒ Boolean
30 31 32 |
# File 'lib/datadog/core/utils/only_once.rb', line 30 def ran? @mutex.synchronize { @ran_once } end |
#run ⇒ Object
20 21 22 23 24 25 26 27 28 |
# File 'lib/datadog/core/utils/only_once.rb', line 20 def run @mutex.synchronize do return if @ran_once @ran_once = true yield end end |