[go: up one dir, main page]

Async issue

As it turned out, there are some issues with PG database while running Safrano under Falcon async server.

It didn't appear locally under SQLite, but on deployment with sequel Postgres I have this error:

NoMethodError: undefined method 'nfields' for nil:NilClass res.nfields.times do |fieldnum|

To get around the problem, I added rack middleware with synchronous lock as follows:

$sp ||= Async::Semaphore.new 1

class FiberLock
  def initialize(app) = @app = app
  def call(env)
    Async::Task.current.async do
      $sp.acquire do
        @app.call env
      end
    end.wait
  end
end

class ODataSrv < Safrano::ServerApp; end

rack_apps << Rack::Builder.new do
  use FiberLock
  run ODataSrv.new
end

Async do
  Rack::Handler::Falcon.run Rack::Cascade.new(rack_apps), Port: HTTP_PORT, Host: '0.0.0.0'
end