Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

db_postgres: automatic reconnect #9

Open
FedericoCeratto opened this issue Mar 22, 2020 · 3 comments
Open

db_postgres: automatic reconnect #9

FedericoCeratto opened this issue Mar 22, 2020 · 3 comments

Comments

@FedericoCeratto
Copy link
Member

db_postgres should detect connection error and reconnect automatically using pqreset()
Ideally with:

  • exponential backoff
  • configurable start interval
  • configurable number of retries or timeout (set to 0 to disable reconnect)
  • enabled by default
@juancarlospaco
Copy link
Contributor

Having a general purpose retry with exponential backoff on std lib would be nice.
You can use it for other stuff like httpclient downloads, etc.

@FedericoCeratto
Copy link
Member Author

Also a simple thread-safe connection pool.

@francescop
Copy link

How would you implement this?
On my end I wrote a simple function that uses libpq's PQreset function and it does what is supposed to do - the tryExec somehow always returns false, but i am investigating that.

it works means: it reconnects correctly

Something like this:

let db* = open("localhost", "postgres", "", "nim_prologue_quickstart")

proc keepAlive*() {.thread.} =
  {.gcsafe.}:
    while true:
      if not db.tryExec(sql"SELECT 1"):
        echo "db error, reconnecting..."
        pqreset(db) # this could be added to the stdlib
      sleep(1000)

On the stlib something like this could be added:

proc reset*(db: DbConn) {.tags: [DbEffect].} =
  # resets the database connection.
  pqreset(db) # pqreset is already defined in lib/wrappers/postgres.nim

Now, I don't know if something like this is acceptable, if you can create threads on the stdlib, if using channels to pass the db connection status is an option, etc

@ringabout ringabout transferred this issue from nim-lang/Nim Dec 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants