Class: Contrast::Utils::ThreadTracker
- Defined in:
- lib/contrast/utils/thread_tracker.rb
Overview
ThreadTracker allows tracking of singleton objects across threads. It acts on Thread#[] as that call is fiber-local where as Thread#thread_variables is not.
Instance Method Summary collapse
-
#current ⇒ Contrast::Agent::RequestContext
Retrieve the Thread#[] RequestContext.
-
#delete(key) ⇒ Object
Remove the given key from the current Thread#[] by setting it to nil.
-
#get(key, default = nil) ⇒ Object
Get the given key to given value in Thread#[] or return default.
-
#lifespan(context) ⇒ Object
Wrap the block given with a RequestContext by setting it beforehand and deleting it after.
-
#set(key, value) ⇒ Object
Set the given key to given value in Thread#[].
-
#update_current_context(context) ⇒ Object
Set the Thread#[] context to the one provided.
Instance Method Details
#current ⇒ Contrast::Agent::RequestContext
Retrieve the Thread#[] RequestContext
46 47 48 |
# File 'lib/contrast/utils/thread_tracker.rb', line 46 def current get(:current_context) end |
#delete(key) ⇒ Object
Remove the given key from the current Thread#[] by setting it to nil.
29 30 31 |
# File 'lib/contrast/utils/thread_tracker.rb', line 29 def delete key Thread.current[key] = nil end |
#get(key, default = nil) ⇒ Object
Get the given key to given value in Thread#[] or return default.
14 15 16 |
# File 'lib/contrast/utils/thread_tracker.rb', line 14 def get key, default = nil Thread.current[key] || default end |
#lifespan(context) ⇒ Object
Wrap the block given with a RequestContext by setting it beforehand and deleting it after.
36 37 38 39 40 41 |
# File 'lib/contrast/utils/thread_tracker.rb', line 36 def lifespan context set(:current_context, context) response = yield(context) delete(:current_context) response end |
#set(key, value) ⇒ Object
Set the given key to given value in Thread#[].
22 23 24 |
# File 'lib/contrast/utils/thread_tracker.rb', line 22 def set key, value Thread.current[key] = value end |
#update_current_context(context) ⇒ Object
Set the Thread#[] context to the one provided.
53 54 55 |
# File 'lib/contrast/utils/thread_tracker.rb', line 53 def update_current_context context set(:current_context, context) end |