Sinatra / ActiveRecord cannot handle concurrent requests?

This is my first Sinatra project, and I'm pretty late, and I understand that when you make multiple queries that use ActiveRecord, I run into problems. If I make only one request, each of them will work on its own. But when I call both at once, I get a refusal.

So far, I have narrowed it down to two ActiveRecord concurrent queries. Maybe I did not configure ActiveRecord correctly? I use PostgreSQL because Heroku uses it and I am not prone to change. (The problem also occurs on Heroku.)

Here is the log:

192.168.1.113 - - [30/Sep/2012:10:33:00 MDT] "GET /version/current?platform=android HTTP/1.1" 200 33 - -> /version/current?platform=android ActiveRecord::StatementInvalid - NoMethodError: undefined method `fields' for nil:NilClass: SELECT "rankings".* FROM "rankings" WHERE "rankings"."user_id" = 1 LIMIT 1: /Users/zablanc/.rvm/gems/ ruby-1.9.3-head@emm /gems/activerecord-3.2.7/lib/active_record/connection_adapters/postgresql_adapter.rb:667:in `block in exec_query' ... Warning! Rack::Session::Cookie data size exceeds 4K. Warning! Rack::Session::Cookie failed to save session. Content dropped. 192.168.1.113 - - [30/Sep/2012:10:33:01 MDT] "GET /badges/all HTTP/1.1" 200 311 - -> /badges/all 192.168.1.113 - - [30/Sep/2012:10:33:01 MDT] "GET /moves/ranking/all HTTP/1.1" 500 166185 - -> /moves/ranking/all 

I have no idea how to plug these warnings in a cookie, as they do not seem to affect the application. Here's how I configure my application (in the configuration file, which is required from the main script):

 enable :logging use ActiveRecord::ConnectionAdapters::ConnectionManagement use Rack::Session::Cookie, :key => 'rack.session', :path => '/', :expire_after => 31_536_000, # In seconds :secret => 'jeowkfj...secret...kjn5' ActiveRecord::Base.include_root_in_json = false def establish_connection(url) db = URI.parse(url) ActiveRecord::Base.establish_connection( :adapter => db.scheme == 'postgres' ? 'postgresql' : db.scheme, :host => db.host, :port => db.port, :username => db.user, :password => db.password, :database => db.path[1..-1], :encoding => 'utf8' ) end configure :development do establish_connection('postgres://postgres: postgres@localhost :5432/emm') end configure :test do establish_connection('postgres://postgres: postgres@localhost :5432/emm-test') end configure :production do establish_connection(ENV['DATABASE_URL']) end 

I assume that I am not setting up ActiveRecord correctly, but I think that this is exactly the same as the tutorials that I saw. What gives?

+4
source share
1 answer

It sounds like you are using threads, but there is unsafe code in your application.

Which web server do you use, what middleware do you use, which postgresql stone do you use, have you verified that all your gems are thread safe?

+1
source

Source: https://habr.com/ru/post/1437059/


All Articles