Module: LittleGhost::ExecutionState

Defined in:
lib/little_ghost/execution_state.rb

Overview

ExecutionState carries request-scoped values across fibers and the worker threads LittleGhost creates. It keeps event and instrumentation context from leaking between concurrent runs.

Framework extensions may use #capture and #with to preserve event and instrumentation context. Captured hashes are immutable; values within them are not deep-copied.

Constant Summary collapse

STORAGE_KEY =

:nodoc:

:little_ghost_execution_state
EMPTY_STATE =

:nodoc:

{}.freeze

Class Method Summary collapse

Class Method Details

.[](key) ⇒ Object

Reads key from the current execution state.



17
18
19
# File 'lib/little_ghost/execution_state.rb', line 17

def [](key)
  state[key]
end

.[]=(key, value) ⇒ Object

Replaces key in the current fiber-local state.



22
23
24
# File 'lib/little_ghost/execution_state.rb', line 22

def []=(key, value)
  replace(state.merge(key => value))
end

.captureObject

Captures the current immutable state hash for propagation.



34
35
36
# File 'lib/little_ghost/execution_state.rb', line 34

def capture
  state
end

.delete(key) ⇒ Object

Deletes key and returns its previous value.



27
28
29
30
31
# File 'lib/little_ghost/execution_state.rb', line 27

def delete(key)
  value = state[key]
  replace(state.except(key))
  value
end

.with(values) ⇒ Object

Merges values while the block runs, then restores prior state.



39
40
41
42
43
44
45
# File 'lib/little_ghost/execution_state.rb', line 39

def with(values)
  previous = state
  replace(previous.merge(values))
  yield
ensure
  replace(previous)
end