Ruby Thread # [] = method notes

About Ruby Thread # [] = method

The Ruby Thread # [] = method allows you to keep a unique value for each thread. (It's like ThreadLocal in Java) If you use this, the application server (almost all AP servers such as Passenger) associated with 1 thread and 1 request will It can be used as a thread-local variable that can be closed and used only during one request.

Example of using Ruby's Thread.current [] method

You can define a thread-local variable foo by assigningThread.current [: foo]to the current thread as follows:

def self.foo
  Thread.current[:foo] ||= 0
end

There is also a gem called RecordWithOperator that provides the Ruby on Rails framework with the ability to record record operators (creators, updaters, deleters). It is implemented using this Thread # [] =. https://github.com/nay/record_with_operator/blob/4389d077b0303b956cc211ef439a46a216ae2cc0/lib/record_with_operator/operator.rb#L4

Thread # [] = Method notes

If you are using an application server such as Passenger or Puma that reuses threads once created Since the thread local variable is associated with the thread that is supposed to be reused, it must be explicitly destroyed on the application side. Please note that the variable defined in the previous request (thread) can be referenced in another request.

Redmine also previously had code to handle the current user using Thread # [] = There was a case of a security bug in which the name of another user was displayed in the error message. http://www.redmine.org/issues/16685

solution

This can be avoided by explicitly discarding the Thread.current value on the application side. If you use a gem called RequestStore, you can use the Rack :: Middleware layer for each request. It will now clear the value of Thread.current.

An example of use is as follows:

def self.foo
  RequestStore.store[:foo] ||= 0
end

Recommended Posts

Ruby Thread # [] = method notes
[Ruby] Notes on gets method
Ruby to_s method
Ruby Hash notes
[Ruby] slice method
[Ruby] end_with? method
[Ruby] Method memorandum
[Ruby] initialize method
Ruby build method
Ruby accessor method
ruby map method
Ruby Learning # 30 Initialize Method
abbreviation for ruby method
Ruby Learning # 24 Exponent Method
Ruby study notes (puts)
definition of ruby method
[Ruby] Method definition summary
[Java] Basic method notes
Integer check method with ruby
Ruby algorithm (inject, method definition)
Ruby study notes (variables & functions)
[Ruby] Method that returns truth
[ruby] Method call with argument
[Java] New Thread generation method (1)
Ruby design pattern template method pattern memo
Method
[Ruby] Method to count specific characters
[Ruby] present/blank method and postfix if.
[Ruby] Extracting elements with slice method
[Ruby basics] split method and to_s method
[Ruby] How to use any? Method
String output method memo in Ruby
Notes on Android (java) thread processing
[Ruby] Search problem using index method
[Ruby on Rails] Convenient helper method
[Ruby] undefined method `dark?'occurs in rqr_code
How to use Ruby inject method
[Ruby] Obtaining even values ​​using the even? Method
[Ruby] Writing notes for cherry books [Notes for yourself]
Ruby on Rails installation method [Mac edition]
Implemented "Floyd Cycle Detection Method" in Ruby
Ruby print puts p printf output method
Notes on using FCM with Ruby on Rails
[Ruby / Rails] Mechanism for retrying Thread Error
ruby Exercise Memo II (variable and method)
Extract characters from Ruby strings slice method