new pgconn_wait() method for adding asynchronous notification support

Issue #10 resolved
Michael Granger repo owner created an issue

Via Sameer Rayachoti in [[|Rubyforge Patch 25298]] (formatting mine):

I wanted to take advantage of Postgres's async notification in a Rails web app. My goal was the build a queueing system that didn't rely on polling. The dequeuer could {{{LISTEN}}} for notifications, and the enqueuer could enqueue stuff and issue a {{{NOTIFY}}}. Most of the underlying support was already in place. But I needed a method exposed by the adapter for waiting on activity on the socket. So, there wasn't any support for getting the dequeuer to sleep until it's woken up by a notification. The attached patchfile adds such a method. It's called {{{pgconn_wait()}}}. It's similar to {{{pgconn_block()}}}, but {{{pgconn_block()}}} doesn't block if the server is not busy, and consumes input both before and after the loop.

Below is some sample ruby code that demonstrates how this method can be used to get asynchronous notification working.

The module is intended to be mixed-in to an Rails ActiveRecord class:



module Notifiable def notify(what) connection.execute("NOTIFY #{what}") end

def listen(what)
    connection.execute("LISTEN #{what}")

def wait(timeout)
    gotsomething = connection.raw_connection.wait(timeout)
    return false unless gotsomething

def notifies
    notices = []
    loop do
        notice = connection.raw_connection.notifies
        return notices if notice.nil?
        notices << notice

end }}}

Comments (1)

  1. Michael Granger reporter

    I think this functionality is completely covered by the new PGconn#wait_for_notify method that'll be in the next release, so I'm closing this. If you feel it's not covered, please either reopen this ticket and give details of what you need that isn't addressed by #wait_for_notify or open a new ticket with an up-to-date patch.

  2. Log in to comment