Module: Raptor::ThreadLocals

Defined in:
lib/raptor/thread_locals.rb,
sig/generated/raptor/thread_locals.rbs

Overview

Owns Raptor's reusable per-thread values and clears application thread locals after each request.

Constant Summary collapse

PRESERVED_KEYS =

Returns:

  • (Object)
[
  :raptor_http_parser,
  :raptor_read_buffer,
  :raptor_response_buffer
].freeze

Class Method Summary collapse

Class Method Details

.clear ⇒ void

This method returns an undefined value.

Clears application-owned true thread locals while retaining Raptor's reusable per-thread values.



33
34
35
36
37
38
# File 'lib/raptor/thread_locals.rb', line 33

def self.clear
  thread = Thread.current
  thread.thread_variables.each do |key|
    thread.thread_variable_set(key, nil) unless PRESERVED_KEYS.include?(key)
  end
end

.fetch(key) { ... } ⇒ Object

Returns a reusable value stored on the current thread.

Parameters:

  • key (Symbol) —

    thread-variable key used to cache the value

Yields:

Yield Returns:

  • (Object) —

    value to store when the key is unset

Returns:

  • (Object) —

    the existing or newly stored value



22
23
24
25
# File 'lib/raptor/thread_locals.rb', line 22

def self.fetch(key)
  thread = Thread.current
  thread.thread_variable_get(key) || thread.thread_variable_set(key, yield)
end