summaryrefslogtreecommitdiffstats
path: root/lem/postgres/queued.lua
diff options
context:
space:
mode:
authorAsbjørn Sloth Tønnesen <ast@2e8.dk>2020-06-03 20:27:20 +0000
committerAsbjørn Sloth Tønnesen <ast@2e8.dk>2020-06-03 20:27:20 +0000
commitfdfecee2044cd1ea80012cdf9f0b4cf96089deb0 (patch)
tree78c9b899df18041710e09d16cb78e7518e653846 /lem/postgres/queued.lua
parentb2a3f5508a52b87b9e556b30c26ab19fc0e4d591 (diff)
downloadlem-postgres-fdfecee2044cd1ea80012cdf9f0b4cf96089deb0.tar.gz
lem-postgres-fdfecee2044cd1ea80012cdf9f0b4cf96089deb0.tar.xz
lem-postgres-fdfecee2044cd1ea80012cdf9f0b4cf96089deb0.zip
Add support for asynchronous notifications
If a notification handler is set using db:set_notifier(cb) then the event loop will be kept alive until db:close() is called or the event loop. Prior to this a database connection would never keep the event loop running. https://www.postgresql.org/docs/current/libpq-notify.html Signed-off-by: Asbjørn Sloth Tønnesen <ast@2e8.dk>
Diffstat (limited to 'lem/postgres/queued.lua')
-rw-r--r--lem/postgres/queued.lua26
1 files changed, 20 insertions, 6 deletions
diff --git a/lem/postgres/queued.lua b/lem/postgres/queued.lua
index 2f597f4..f405dc0 100644
--- a/lem/postgres/queued.lua
+++ b/lem/postgres/queued.lua
@@ -1,6 +1,7 @@
--
-- This file is part of lem-postgres.
-- Copyright 2011 Emil Renner Berthing
+-- Copyright 2020 Asbjørn Sloth Tønnesen
--
-- lem-postgres is free software: you can redistribute it and/or
-- modify it under the terms of the GNU General Public License as
@@ -22,18 +23,32 @@ local postgres = require 'lem.postgres'
local remove = table.remove
local thisthread, suspend, resume = utils.thisthread, utils.suspend, utils.resume
+local Connection = postgres.Connection
+
local QConnection = {}
QConnection.__index = QConnection
+function QConnection:set_notifier(cb)
+ Connection.set_notifier(self.conn, cb)
+end
+
+local function cancel_all(t)
+ for i = 1, t.n - 1 do
+ resume(t[i])
+ t[i] = nil
+ end
+end
+
function QConnection:close()
local ok, err = self.conn:close()
- for i = 1, self.n - 1 do
- resume(self[i])
- self[i] = nil
- end
+ cancel_all(self)
return ok, err
end
-QConnection.__gc = QConnection.close
+
+function QConnection:__gc()
+ cancel_all(self)
+ self.conn = nil
+end
local function lock(self)
local n = self.n
@@ -64,7 +79,6 @@ local function wrap(method)
end
end
-local Connection = postgres.Connection
QConnection.exec = wrap(Connection.exec)
QConnection.prepare = wrap(Connection.prepare)
QConnection.run = wrap(Connection.run)