pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Scott Ribe <scott_ribe@killerbytes.com>
To: Craig Ringer <craig@postnewspapers.com.au>
Cc: PG-General Mailing List <pgsql-general@postgresql.org>
Subject: Re: Cheapest way to poll for notifications?
Date: Thu, 10 Dec 2009 16:20:28 -0700
Message-ID: <C746D04C.CAE15%scott_ribe@killerbytes.com> (raw)
In-Reply-To: <4B211E6F.1070700@postnewspapers.com.au>

> Right?

Right. The way I do it, very roughly:


 Pqconnectdb(...)
 PQexec( mDbConn, "listen notify" );
 int dbsock = PQsocket( mDbConn );
 mKq = kqueue();
 struct kevent kev[1], kevs[1];
 EV_SET( &kev[0], dbsock, EVFILT_READ, EV_ADD | EV_ENABLE, 0, 0, 0 );
 kevent( mKq, kev, 1, NULL, 0, NULL );
 while( true ) {
   bool needsnotify = false;
   int evtcnt = kevent( mKq, NULL, 0, kevs, 1, NULL );
   if( evtcnt == 1 && kevs[i].filter == EVFILT_READ && kevs[i].ident ==
dbsock ) {
     while( true ) {
       PQconsumeInput( mDbConn );
       PGnotify * notify = PQnotifies( mDbConn );
       if( notify )  {
         free( notify );
         needsnotify = true;
       }
       else
         break;
     }
   }

  if( needsnotify )
    // go off & query the db & handle updates here
 }   


Of course you could also use select to wait on the socket.

-- 
Scott Ribe
scott_ribe@killerbytes.com
http://www.killerbytes.com/
(303) 722-0567 voice





view thread (9+ messages)  latest in thread

Message-ID: <C746D04C.CAE15%scott_ribe@killerbytes.com>
Permalink:  ../C746D04C.CAE15%25scott_ribe@killerbytes.com/
Also on:    postgresql.org/message-id/C746D04C.CAE15%scott_ribe@killerbytes.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-jdbc@postgresql.org
  Cc: scott_ribe@killerbytes.com, craig@postnewspapers.com.au, pgsql-general@postgresql.org
  Subject: Re: Cheapest way to poll for notifications?
  In-Reply-To: <C746D04C.CAE15%scott_ribe@killerbytes.com>
  References: <4B211E6F.1070700@postnewspapers.com.au> <C746D04C.CAE15%scott_ribe@killerbytes.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox