Class: Contrast::Utils::ThreadTracker
- Defined in:
- lib/contrast/utils/thread_tracker.rb
Overview
ThreadTracker allows tracking of singleton objects across threads
Instance Method Summary collapse
- #current ⇒ Object
- #delete(key) ⇒ Object
-
#get(key, default = nil) ⇒ Object
Note about Ruby – thread#[] is fiber-local, #thread_variables is not.
-
#initialize ⇒ ThreadTracker
constructor
A new instance of ThreadTracker.
- #lifespan(obj) ⇒ Object
- #set(key, value) ⇒ Object
- #update_current_context(context) ⇒ Object
Constructor Details
#initialize ⇒ ThreadTracker
Returns a new instance of ThreadTracker.
8 |
# File 'lib/contrast/utils/thread_tracker.rb', line 8 def initialize; end |
Instance Method Details
#current ⇒ Object
32 33 34 |
# File 'lib/contrast/utils/thread_tracker.rb', line 32 def current get(:current_context) end |
#delete(key) ⇒ Object
21 22 23 |
# File 'lib/contrast/utils/thread_tracker.rb', line 21 def delete key Thread.current[key] = nil end |
#get(key, default = nil) ⇒ Object
Note about Ruby – thread#[] is fiber-local, #thread_variables is not.
13 14 15 |
# File 'lib/contrast/utils/thread_tracker.rb', line 13 def get key, default = nil Thread.current[key] || default end |
#lifespan(obj) ⇒ Object
25 26 27 28 29 30 |
# File 'lib/contrast/utils/thread_tracker.rb', line 25 def lifespan obj set(:current_context, obj) response = yield(obj) delete(:current_context) response end |
#set(key, value) ⇒ Object
17 18 19 |
# File 'lib/contrast/utils/thread_tracker.rb', line 17 def set key, value Thread.current[key] = value end |
#update_current_context(context) ⇒ Object
36 37 38 |
# File 'lib/contrast/utils/thread_tracker.rb', line 36 def update_current_context context set(:current_context, context) end |