Coder Social home page Coder Social logo

async-postgres's Introduction

Socketry

Gem Version Build Status Code Climate Coverage Status MIT licensed

High-level Ruby socket library with support for TCP, UDP, and SSL sockets.

Implements thread-safe timeouts using asynchronous I/O and high-precision monotonic timers.

Motivation

By default, Ruby sockets do not provide a built-in timeout mechanism. The only timeout mechanism provided by the language leverages timeout.rb, which uses unsafe multithreaded behaviors to implement timeouts.

While Socketry provides a synchronous, blocking API similar to Ruby's own TCPSocket and UDPSocket classes, behind the scenes it uses non-blocking I/O to implement thread-safe timeouts.

Installation

Add this line to your application's Gemfile:

gem "socketry"

And then execute:

$ bundle

Or install it yourself as:

$ gem install socketry

Basic Usage

Below is a basic example of how to use Socketry to make an HTTPS request:

require "socketry"

socket = Socketry::SSL::Socket.connect("github.com", 443)
socket.writepartial("GET / HTTP/1.0\r\nHost: github.com\r\n\r\n")
p socket.readpartial(1024)

TCP, SSL, and UDP servers and sockets also available.

Documentation

Please see the Socketry wiki for more detailed documentation and usage notes.

YARD API documentation is also available.

Supported Ruby Versions

This library aims to support and is tested against the following Ruby versions:

  • Ruby 2.2.6+
  • Ruby 2.3
  • Ruby 2.4
  • Ruby 2.5
  • JRuby 9.1.6.0+

If something doesn't work on one of these versions, it's a bug.

This library may inadvertently work (or seem to work) on other Ruby versions, however support will only be provided for the versions listed above.

If you would like this library to support another Ruby version or implementation, you may volunteer to be a maintainer. Being a maintainer entails making sure all tests run and pass on that implementation. When something breaks on your implementation, you will be responsible for providing patches in a timely fashion. If critical issues for a particular implementation exist at the time of a major release, support for that Ruby version may be dropped.

Contributing

  • Fork this repository on github
  • Make your changes and send us a pull request
  • If we like them we'll merge them
  • If we've accepted a patch, feel free to ask for commit access

License

Copyright (c) 2016 Tony Arcieri. Distributed under the MIT License. See LICENSE.txt for further details.

async-postgres's People

Contributors

ioquatix avatar olleolleolle avatar tricknotes avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

async-postgres's Issues

Doesn't seem to work with Rails 4

Rails 4 seem to use the PG::Connection#connect method rather than PG#connect. If I add the following monkey-patch, the queries do seem to execute asynchronously:

module PG
  class Connection
    def self.connect(*args)
      Async::Postgres::Proxy.new(*args)
    end
  end
end

I can send a PR in a few weeks, if that's the way we want to go.

Review connection pool implementation

Regarding rails/rails#37070 I realised maybe it's possible for long running queries to overlap because the ConnectionPool is storing connections seemingly per-thread. So even if you start a transaction in different fibers, maybe it breaks.

      # Retrieve the connection associated with the current thread, or call
      # #checkout to obtain one if necessary.
      #
      # #connection can be called any number of times; the connection is
      # held in a cache keyed by a thread.
      def connection
        @thread_cached_conns[connection_cache_key(current_thread)] ||= checkout
      end

One option is to try and hijack the ConnectionPool - e.g. if spec[:connection_pool] is defined, use it.

        message_bus.instrument("!connection.active_record", payload) do
          owner_to_pool[spec.name] = ConnectionAdapters::ConnectionPool.new(spec)
        end

It seems to me, a per-fiber or per-thread connection pool might make more sense.

There was an exception - RuntimeError(No async task available!)

When I added the latest falcon and async-postgres in my Rails application so I got this error while a first request:

There was an exception - RuntimeError(No async task available!)
~/.gem/ruby/2.7.0/gems/async-1.24.1/lib/async/task.rb:183:in `current'
~/.gem/ruby/2.7.0/gems/async-postgres-0.1.0/lib/async/postgres/pool.rb:30:in `initialize'
~/.gem/ruby/2.7.0/gems/async-postgres-0.1.0/lib/async/postgres.rb:29:in `new'
~/.gem/ruby/2.7.0/gems/async-postgres-0.1.0/lib/async/postgres.rb:29:in `connect'
~/.gem/ruby/2.7.0/gems/activerecord-6.0.2.1/lib/active_record/connection_adapters/postgresql_adapter.rb:46:in `postgresql_connection'
~/.gem/ruby/2.7.0/gems/activerecord-6.0.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:889:in `new_connection'
~/.gem/ruby/2.7.0/gems/activerecord-6.0.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:933:in `checkout_new_connection'
~/.gem/ruby/2.7.0/gems/activerecord-6.0.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:912:in `try_to_checkout_new_connection'
~/.gem/ruby/2.7.0/gems/activerecord-6.0.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:873:in `acquire_connection'
~/.gem/ruby/2.7.0/gems/activerecord-6.0.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:595:in `checkout'
~/.gem/ruby/2.7.0/gems/activerecord-6.0.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:439:in `connection'
~/.gem/ruby/2.7.0/gems/activerecord-6.0.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:1121:in `retrieve_connection'
~/.gem/ruby/2.7.0/gems/activerecord-6.0.2.1/lib/active_record/connection_handling.rb:238:in `retrieve_connection'
~/.gem/ruby/2.7.0/gems/activerecord-6.0.2.1/lib/active_record/connection_handling.rb:206:in `connection'
~/.gem/ruby/2.7.0/gems/activerecord-6.0.2.1/lib/active_record/model_schema.rb:340:in `table_exists?'
~/.gem/ruby/2.7.0/gems/activerecord-6.0.2.1/lib/active_record/attribute_methods/primary_key.rb:92:in `get_primary_key'
~/.gem/ruby/2.7.0/gems/activerecord-6.0.2.1/lib/active_record/attribute_methods/primary_key.rb:80:in `reset_primary_key'
~/.gem/ruby/2.7.0/gems/activerecord-6.0.2.1/lib/active_record/attribute_methods/primary_key.rb:68:in `primary_key'
~/.gem/ruby/2.7.0/gems/orm_adapter-0.5.0/lib/orm_adapter/adapters/active_record.rb:17:in `get'
~/.gem/ruby/2.7.0/gems/devise-4.7.1/lib/devise/models/authenticatable.rb:237:in `serialize_from_session'
~/.gem/ruby/2.7.0/gems/devise-4.7.1/lib/devise.rb:485:in `block (2 levels) in configure_warden!'
~/.gem/ruby/2.7.0/gems/warden-1.2.8/lib/warden/session_serializer.rb:35:in `fetch'
~/.gem/ruby/2.7.0/gems/warden-1.2.8/lib/warden/proxy.rb:224:in `user'
~/app/app/channels/application_cable/connection.rb:15:in `find_verified_user'
~/app/app/channels/application_cable/connection.rb:8:in `connect'
~/.gem/ruby/2.7.0/gems/actioncable-6.0.2.1/lib/action_cable/connection/base.rb:171:in `handle_open'
~/.gem/ruby/2.7.0/gems/actioncable-6.0.2.1/lib/action_cable/server/worker.rb:59:in `block in invoke'
~/.gem/ruby/2.7.0/gems/actioncable-6.0.2.1/lib/action_cable/server/worker.rb:41:in `block in work'
~/.gem/ruby/2.7.0/gems/activesupport-6.0.2.1/lib/active_support/callbacks.rb:112:in `block in run_callbacks'
~/.gem/ruby/2.7.0/gems/actioncable-6.0.2.1/lib/action_cable/server/worker/active_record_connection_management.rb:16:in `block in with_database_connections'
~/.gem/ruby/2.7.0/gems/actioncable-6.0.2.1/lib/action_cable/connection/tagged_logger_proxy.rb:24:in `block in tag'
~/.gem/ruby/2.7.0/gems/activesupport-6.0.2.1/lib/active_support/tagged_logging.rb:80:in `block in tagged'
~/.gem/ruby/2.7.0/gems/activesupport-6.0.2.1/lib/active_support/tagged_logging.rb:28:in `tagged'
~/.gem/ruby/2.7.0/gems/activesupport-6.0.2.1/lib/active_support/tagged_logging.rb:80:in `tagged'
~/.gem/ruby/2.7.0/gems/actioncable-6.0.2.1/lib/action_cable/connection/tagged_logger_proxy.rb:24:in `tag'
~/.gem/ruby/2.7.0/gems/actioncable-6.0.2.1/lib/action_cable/server/worker/active_record_connection_management.rb:16:in `with_database_connections'
~/.gem/ruby/2.7.0/gems/activesupport-6.0.2.1/lib/active_support/callbacks.rb:121:in `block in run_callbacks'
~/.gem/ruby/2.7.0/gems/actioncable-6.0.2.1/lib/action_cable/engine.rb:62:in `block (4 levels) in <class:Engine>'
~/.gem/ruby/2.7.0/gems/activesupport-6.0.2.1/lib/active_support/execution_wrapper.rb:88:in `wrap'
~/.gem/ruby/2.7.0/gems/actioncable-6.0.2.1/lib/action_cable/engine.rb:57:in `block (3 levels) in <class:Engine>'
~/.gem/ruby/2.7.0/gems/activesupport-6.0.2.1/lib/active_support/callbacks.rb:121:in `instance_exec'
~/.gem/ruby/2.7.0/gems/activesupport-6.0.2.1/lib/active_support/callbacks.rb:121:in `block in run_callbacks'
~/.gem/ruby/2.7.0/gems/activesupport-6.0.2.1/lib/active_support/callbacks.rb:139:in `run_callbacks'
~/.gem/ruby/2.7.0/gems/actioncable-6.0.2.1/lib/action_cable/server/worker.rb:40:in `work'
~/.gem/ruby/2.7.0/gems/actioncable-6.0.2.1/lib/action_cable/server/worker.rb:58:in `invoke'
~/.gem/ruby/2.7.0/gems/actioncable-6.0.2.1/lib/action_cable/server/worker.rb:53:in `block in async_invoke'
~/.gem/ruby/2.7.0/gems/concurrent-ruby-1.1.5/lib/concurrent/executor/ruby_thread_pool_executor.rb:348:in `run_task'
~/.gem/ruby/2.7.0/gems/concurrent-ruby-1.1.5/lib/concurrent/executor/ruby_thread_pool_executor.rb:337:in `block (3 levels) in create_worker'
~/.gem/ruby/2.7.0/gems/concurrent-ruby-1.1.5/lib/concurrent/executor/ruby_thread_pool_executor.rb:320:in `loop'
~/.gem/ruby/2.7.0/gems/concurrent-ruby-1.1.5/lib/concurrent/executor/ruby_thread_pool_executor.rb:320:in `block (2 levels) in create_worker'
~/.gem/ruby/2.7.0/gems/concurrent-ruby-1.1.5/lib/concurrent/executor/ruby_thread_pool_executor.rb:319:in `catch'
~/.gem/ruby/2.7.0/gems/concurrent-ruby-1.1.5/lib/concurrent/executor/ruby_thread_pool_executor.rb:319:in `block in create_worker'

It doesn't work with Sequel

I'm aware that async-postgres is still experimental, but I just thought I would report that it currently doesn't successfully hook into the Sequel Postgres adapter. The following script executes in 10 seconds:

ENV["NO_SEQUEL_PG"] = "1" # don't activate the sequel_pg gem if it's installed

require "async"
require "async/postgres"
require "sequel"

DB = Sequel.postgres("postgres")

Async.run do |task|
  task.async do
    DB.get{pg_sleep(5)}
  end

  task.async do
    DB.get{pg_sleep(5)}
  end
end

I was looking at Sequel::Postgres, but it's fairly complex due to supporting different versions of the pg gem, along with postgres-pr and the additional sequel_pg gem, so I couldn't really find what has to be overridden.

Occasional deadlocks due to Rails convention for using mutexes

Rails loads table/column info from the database, and this data is turn used to define dynamic methods backed by database columns. The code that performs this initial column load is wrapped in a mutex, which prevents thread safety bugs, but if you're using async-postgres, there's a nondeterministic chance you'll get bitten by a ThreadError: deadlock; recursive locking error in (at least) the following case:

  1. Fiber 1 initializes/saves an ActiveRecord obj for the first time; the column metadata hasn't been loaded, so it locks the mutex, succeeds, and then goes on to load the column data from postgres, ultimately hitting async_exec, which async-postgres modifies to return control back to the reactor
  2. Fiber 2 initializes/saves an ActiveRecord obj, the column metadata still isn't marked as loaded, so it attempts to lock the mutex, but it hits the deadlock because it's trying to lock the mutex from the same thread.

I'm not sure what the solution is here, wouldn't be surprised if this was biting falcon users too; note that I'm seeing this in a Sidekiq-ish style background worker that I wrote with the following libs:

gem "async", require: false
gem "async-http", require: false
gem 'async-redis', github: "socketry/async-redis", require: false
gem 'async-postgres', require: false

locked at the following versions

    async (1.19.0)
      console (~> 1.0)
      nio4r (~> 2.3)
      timers (~> 4.1)
    async-http (0.46.3)
      async (~> 1.19)
      async-io (~> 1.18)
      protocol-http (~> 0.8.0)
      protocol-http1 (~> 0.8.0)
      protocol-http2 (~> 0.9.0)
    async-io (1.23.3)
      async (~> 1.14)
    async-postgres (0.1.0)
      async (~> 1.3)
      pg

I don't really have a solution at this time, is there perhaps a way I can preload all of these column datas? I looked at the falcon repo but didn't see anyone else running into this particular deadlock.

standalone rails in script issues with async-postgres

I run falcon on heroku and it works perfectly. I use MQTT for communication with some external devices in a low speed internet market.
[Rails 6, postgres 11]

As part of the startup process i have the normal rails webserver (heroku web running falcon) and a script (heroku worker) which access active record (boots rails) for running the MQTT handler. WHEN I install async-postgres the rails webserver continues to work but the script bombs out. The first problem was I had to encapsulate the code in a Async do end statement but after that was done all seems work well locally.

Now it fails in the ActionMailer code on heroku. The difference between local and heroku may be that I use Redis.

I suspect that I need to patch my actionMailer code with an async statement. But any suggestions will be appreciated. I will fiddle with it over the weekend and update this issue if I find a fix.

The basics of the script are:

Async do |task|
begin
  STDOUT.flush 
  mqttmgr = MQTTManager.new
  env, certificatePath, privateKeyPath, rootCAPath=mqttmgr.build_cert_paths(certdir,"fred")
  owner=Setting.first.application_owner if !Setting.first.nil? #FIRST AR/RAILS CALL
  puts "Please set application owner in settings" if Setting.first.nil?
  clientcount=1

  STDOUT.flush

config = {
  :host => "",
  :port =>,
  :env => env,
  :rootca => rootCAPath,
  :certpath => certificatePath,
  :keypath => privateKeyPath,
  :application_owner => owner,
  :keep_alive => 30,
}
STDOUT.flush
client=mqttmgr.start(config)
STDOUT.flush
mqttmgr.read_messages(client,config)

rescue Exception => ex
puts "#{Time.now}: Exception in MQTT Gateway: #{ex} at #{ex.backtrace.join("\n")}"
end

end #ASYNC

stack trace:

2020-01-10T08:52:59.692023+00:00 app[mqtt.1]: /app/vendor/bundle/ruby/2.6.0/gems/async-1.24.0/lib/async/task.rb:183:in current': No async task available! (RuntimeError) 2020-01-10T08:52:59.692052+00:00 app[mqtt.1]: from /app/vendor/bundle/ruby/2.6.0/gems/async-postgres-0.1.0/lib/async/postgres/pool.rb:30:in initialize'
2020-01-10T08:52:59.692119+00:00 app[mqtt.1]: from /app/vendor/bundle/ruby/2.6.0/gems/async-postgres-0.1.0/lib/async/postgres.rb:29:in new' 2020-01-10T08:52:59.692152+00:00 app[mqtt.1]: from /app/vendor/bundle/ruby/2.6.0/gems/async-postgres-0.1.0/lib/async/postgres.rb:29:in connect'
2020-01-10T08:52:59.692184+00:00 app[mqtt.1]: from /app/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0.rc2/lib/active_record/connection_adapters/postgresql_adapter.rb:46:in postgresql_connection' 2020-01-10T08:52:59.692209+00:00 app[mqtt.1]: from /app/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0.rc2/lib/active_record/connection_adapters/abstract/connection_pool.rb:879:in new_connection'
2020-01-10T08:52:59.692256+00:00 app[mqtt.1]: from /app/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0.rc2/lib/active_record/connection_adapters/abstract/connection_pool.rb:923:in checkout_new_connection' 2020-01-10T08:52:59.692305+00:00 app[mqtt.1]: from /app/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0.rc2/lib/active_record/connection_adapters/abstract/connection_pool.rb:902:in try_to_checkout_new_connection'
2020-01-10T08:52:59.692330+00:00 app[mqtt.1]: from /app/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0.rc2/lib/active_record/connection_adapters/abstract/connection_pool.rb:863:in acquire_connection' 2020-01-10T08:52:59.692354+00:00 app[mqtt.1]: from /app/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0.rc2/lib/active_record/connection_adapters/abstract/connection_pool.rb:587:in checkout'
2020-01-10T08:52:59.692378+00:00 app[mqtt.1]: from /app/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0.rc2/lib/active_record/connection_adapters/abstract/connection_pool.rb:431:in connection' 2020-01-10T08:52:59.692401+00:00 app[mqtt.1]: from /app/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0.rc2/lib/active_record/connection_adapters/abstract/connection_pool.rb:1106:in retrieve_connection'
2020-01-10T08:52:59.692440+00:00 app[mqtt.1]: from /app/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0.rc2/lib/active_record/connection_handling.rb:231:in retrieve_connection' 2020-01-10T08:52:59.692487+00:00 app[mqtt.1]: from /app/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0.rc2/lib/active_record/connection_handling.rb:199:in connection'
2020-01-10T08:52:59.692512+00:00 app[mqtt.1]: from /app/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0.rc2/lib/active_record/model_schema.rb:340:in table_exists?' 2020-01-10T08:52:59.692535+00:00 app[mqtt.1]: from /app/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0.rc2/lib/active_record/attribute_methods/primary_key.rb:92:in get_primary_key'
2020-01-10T08:52:59.692558+00:00 app[mqtt.1]: from /app/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0.rc2/lib/active_record/attribute_methods/primary_key.rb:80:in reset_primary_key' 2020-01-10T08:52:59.692582+00:00 app[mqtt.1]: from /app/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0.rc2/lib/active_record/attribute_methods/primary_key.rb:68:in primary_key'
2020-01-10T08:52:59.692620+00:00 app[mqtt.1]: from /app/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0.rc2/lib/active_record/relation/delegation.rb:90:in primary_key' 2020-01-10T08:52:59.692667+00:00 app[mqtt.1]: from /app/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0.rc2/lib/active_record/relation/finder_methods.rb:545:in ordered_relation'
2020-01-10T08:52:59.692692+00:00 app[mqtt.1]: from /app/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0.rc2/lib/active_record/relation/finder_methods.rb:511:in find_nth_with_limit' 2020-01-10T08:52:59.692716+00:00 app[mqtt.1]: from /app/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0.rc2/lib/active_record/relation/finder_methods.rb:504:in find_nth'
2020-01-10T08:52:59.692741+00:00 app[mqtt.1]: from /app/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0.rc2/lib/active_record/relation/finder_methods.rb:120:in first' 2020-01-10T08:52:59.692770+00:00 app[mqtt.1]: from /app/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0.rc2/lib/active_record/querying.rb:21:in first'
2020-01-10T08:52:59.692826+00:00 app[mqtt.1]: from /app/app/mailers/tms_mailer.rb:4:in `class:TmsMailer'

TESTING BOMBS AS WELL
sorry - just saw this error in the rspec.
Failure/Error: ActiveRecord::Migration.maintain_test_schema!
RuntimeError:
No async task available!

Cannot install by adding 'async-postgres' to our Gemfile

How to replicate:

  • Added `gem 'async-postgres' to the project's Gemfile
  • Ran bundle install

Got:
Could not find gem 'async-postgres' in any of the gem sources listed in your Gemfile.

I think async-postgres is not yet published on Rubygems

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.