Class | EventMachine::Protocols::Postgres3 |
In: |
lib/em/protocols/postgres3.rb
|
Parent: | EventMachine::Connection |
PROVISIONAL IMPLEMENTATION of an evented Postgres client. This implements version 3 of the Postgres wire protocol, which will work with any Postgres version from roughly 7.4 onward.
Objective: we want to access Postgres databases without requiring threads. Until now this has been a problem because the Postgres client implementations have all made use of blocking I/O calls, which is incompatible with a thread-free evented model.
But rather than re-implement the Postgres Wire3 protocol, we‘re taking advantage of the existing postgres-pr library, which was originally written by Michael Neumann but (at this writing) appears to be no longer maintained. Still, it‘s in basically a production-ready state, and the wire protocol isn‘t that complicated anyway.
We need to monkeypatch StringIO because it lacks the readbytes method needed by postgres-pr.
We‘re tucking in a bunch of require statements that may not be present in garden-variety EM installations. Until we find a good way to only require these if a program requires postgres, this file will need to be required explicitly.
The StringIO monkeypatch is lifted verbatim from the standard library readbytes.rb, which adds method readbytes directly to class IO. But StringIO is not a subclass of IO.
We cloned the handling of postgres messages from lib/postgres-pr/connection.rb in the postgres-pr library, and modified it for event-handling.
TODO: The password handling in dispatch_conn_message is totally incomplete.
We return Deferrables from the user-level operations surfaced by this interface. Experimentally, we‘re using the pattern of always returning a boolean value as the first argument of a deferrable callback to indicate success or failure. This is instead of the traditional pattern of calling Deferrable#succeed or fail, and requiring the user to define both a callback and an errback function.
EM.run { db = EM.connect_unix_domain( "/tmp/.s.PGSQL.5432", EM::P::Postgres3 ) db.connect( dbname, username, psw ).callback do |status| if status db.query( "select * from some_table" ).callback do |status, result, errors| if status result.rows.each do |row| p row end end end end end }