|
161 | 161 | ---@field url string **readonly** connection url
|
162 | 162 | ---@field connecting boolean **readonly** is client connecting to the database
|
163 | 163 | ---@field closed boolean **readonly** is client closed (to change it to true use `:close()`)
|
164 |
| ----@field maxRetries number maximum number of retries to reconnect to the database if connection was lost (set to 0 to disable) |
165 |
| ----@field private retryAttempted number number of attempts to reconnect to the database |
166 | 164 | ---@field private conn PGconn native connection object (do not use it directly, otherwise be careful not to store it anywhere else, otherwise closing connection will be impossible)
|
167 | 165 | ---@field private queries { push: fun(self, q: PGQuery), prepend: fun(self, q: PGQuery), pop: (fun(self): PGQuery), size: fun(self): number } list of queries
|
168 | 166 | ---@field package acquired boolean
|
@@ -274,39 +272,10 @@ function Client:wait()
|
274 | 272 | return self.conn:wait()
|
275 | 273 | end
|
276 | 274 |
|
277 |
| ---- Tries to reconnect to the database, |
278 |
| ---- if maximum number will be reached, we will stop trying to reconnect |
279 |
| ---- |
280 |
| ---- WARNING! If number of attempts is exceeded, first query will be failed |
281 |
| ----@private |
282 |
| -function Client:tryReconnect() |
283 |
| - if self.retryAttempted < self.maxRetries and not self.closed then |
284 |
| - self.retryAttempted = self.retryAttempted + 1 |
285 |
| - self:reset(function(ok) |
286 |
| - if not ok and not self:connected() then |
287 |
| - timer.Simple(5, function() |
288 |
| - self:tryReconnect() |
289 |
| - end) |
290 |
| - end |
291 |
| - end) |
292 |
| - else |
293 |
| - local query = self.queries:pop() |
294 |
| - query.callback(false, "connection to the databse was lost, maximum number of retries exceeded") |
295 |
| - end |
296 |
| -end |
297 |
| - |
298 | 275 | ---@private
|
299 | 276 | ---@param query PGQuery
|
300 | 277 | function Client:runQuery(query)
|
301 | 278 | local function callback(ok, result, errdata)
|
302 |
| - -- if connection was lost, put query back into the queue (it was popped before) |
303 |
| - -- and try to reconnect |
304 |
| - if not ok and not self:connected() and self.retryAttempted < self.maxRetries then |
305 |
| - self.queries:prepend(query) |
306 |
| - self:tryReconnect() |
307 |
| - return |
308 |
| - end |
309 |
| - |
310 | 279 | xpcall(query.callback, ErrorNoHaltWithStack, ok, result, errdata)
|
311 | 280 | self:processQueue()
|
312 | 281 | end
|
@@ -680,8 +649,6 @@ function async_postgres.Client(url)
|
680 | 649 | url = url,
|
681 | 650 | connecting = false,
|
682 | 651 | queries = Queue.new(),
|
683 |
| - maxRetries = math.huge, |
684 |
| - retryAttempted = 0, |
685 | 652 | }, Client)
|
686 | 653 | end
|
687 | 654 |
|
|
0 commit comments