Skip to content

Commit 9767099

Browse files
committed
Added close method for pool
1 parent d97796e commit 9767099

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

async_postgres.lua

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -826,6 +826,7 @@ end
826826
---@field url string **readonly** connection url
827827
---@field max number maximum number of clients in the pool (default: 10)
828828
---@field threshold number threshold of waiting :connect(...) acquire functions to create a new client (default: 5)
829+
---@field closed boolean **readonly** is pool closed
829830
---@field private clients PGClient[]
830831
---@field private queue { push: fun(self, f: function), prepend: fun(self, f: function), pop: (fun(self): function), size: fun(self): number }
831832
---@field private errorHandler function function that just calls self:onError(...)
@@ -931,6 +932,10 @@ end
931932
---@see PGClient.release for releasing client after you are done with it
932933
---@param callback fun(client: PGClient)
933934
function Pool:connect(callback)
935+
if self.closed then
936+
error("pool was closed")
937+
end
938+
934939
self.queue:push(callback)
935940
self:processQueue()
936941
end
@@ -1057,6 +1062,16 @@ function Pool:transaction(callback)
10571062
end)
10581063
end
10591064

1065+
--- Closes the pool and all clients in it
1066+
--- If `wait = true`, will wait until all queries are processed
1067+
---@param wait boolean?
1068+
function Pool:close(wait)
1069+
for _, client in ipairs(self.clients) do
1070+
client:close(wait)
1071+
end
1072+
self.closed = true
1073+
end
1074+
10601075
--- This **event** function is called whenever new client connection
10611076
--- was estabileshed.
10621077
--- You can run setup commands on a client.

0 commit comments

Comments
 (0)