2

I know every request to a Rails app instantiates a new controller for the request, so the controller itself is thread safe.

So, if I make a request to /users, the UsersController for example will be a new instance.

However, UsersController inherits from ApplicationController and my ApplicationController is something like this:

class ApplicationController < ActionController::Base
  before_action :authenticate_user!

  def authenticate_user!
    # example
  end

  include CurrentUserHelper
end

This is my CurrentUserHelper:

module CurrentUserHelper
  def current_user
    return @current_user if @current_user

    @current_user = #logic to set current user
  end

  class UserNotFound < StandardError
  end
end

Since I use an instance variable in the CurrentUserHelper I'm not sure if it is thread safe since I don't know if it is a new instance of the module for every request. I ask because my web server is Puma and I want multiple threads.

David
  • 7,028
  • 10
  • 48
  • 95
  • 1
    Actually, I think I'm overthinking this... it should be okay as module is included in the new instance of the class and the instance variable will be applied to the class. – David Feb 19 '18 at 06:57

0 Answers0