Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is the correct place to initialize BigDecimal limit when using Puma

Tags:

I have a rails initializer that sets the limit on Big Decimal

BigDecimal.limit(20)

This is executed and seems to set the limit properly.

But when a web request comes in (I am using Puma), the limit does not appear to be set on the thread that handles the request. The limit is set to 0 which is the default.

Is there somewhere else I should be setting the limit?

If you want to reproduce:

create a brand new rails app and add an initializer that sets limit

BigDecimal.limit(20)
puts "*** Set #{BigDecimal.limit}"

in your application controller add

before_action :check_big_decimal

def check_big_decimal
  puts "***request #{BigDecimal.limit}"
end

start your server, refresh page and make a request

You should see it set to 20 but then it is back to 0 when the request comes in.

like image 353
RMK Avatar asked May 27 '20 15:05

RMK


1 Answers

I got an answer from the Rails community

Basic gist is:

This happens because Ruby stores the BigDecimal limit in thread local storage. When e.g. Puma starts a new thread to handle a request, the new thread can't access the BigDecimal limit set in the initial thread.

like image 109
RMK Avatar answered Nov 15 '22 05:11

RMK