Class: Contrast::Utils::ThreadTracker

Inherits:
Object
  • Object
show all
Defined in:
lib/contrast/utils/thread_tracker.rb

Overview

ThreadTracker allows tracking of singleton objects across threads

Instance Method Summary collapse

Constructor Details

#initializeThreadTracker

Returns a new instance of ThreadTracker.



8
# File 'lib/contrast/utils/thread_tracker.rb', line 8

def initialize; end

Instance Method Details

#currentObject



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