Skip to content

Commit

Permalink
Return last ping error.
Browse files Browse the repository at this point in the history
  • Loading branch information
iegomez committed Feb 11, 2021
1 parent 2e0c63b commit 1efdaab
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ auth_opt_pg_aclquery select distinct 'application/' || a.id || '/#' from "user"
```

**DB connect tries**: on startup, the plugin will try to connect and ping the DB a max number of times or forever depending on `pg_connect_tries` option.
**DB connect tries**: on startup, depending on `pg_connect_tries` option, the plugin will try to connect and ping the DB a max number of times or forever every 2 seconds.
By default it will try to reconnect forever to maintain backwards compatibility and avoid issues when `mosquitto` starts before the DB service does,
but you may choose to ping a max amount of times by setting any positive number.
If given 0, the DB will try to connect only once, which would be the same as setting the option to 1.
Expand Down Expand Up @@ -682,7 +682,7 @@ Acl query:
SELECT topic FROM acl WHERE (username = ?) AND rw = ?
```

**DB connect tries**: on startup, the plugin will try to connect and ping the DB a max number of times or forever depending on `mysql_connect_tries` option.
**DB connect tries**: on startup, depending on `mysql_connect_tries` option, the plugin will try to connect and ping the DB a max number of times or forever every 2 seconds.
By default it will try to reconnect forever to maintain backwards compatibility and avoid issues when `mosquitto` starts before the DB service does,
but you may choose to ping a max amount of times by setting any positive number.
If given 0, the DB will try to connect only once, which would be the same as setting the option to 1.
Expand Down Expand Up @@ -759,7 +759,7 @@ sqlite_superquery SELECT COUNT(*) FROM account WHERE username = ? AND super = 1
sqlite_aclquery SELECT topic FROM acl WHERE (username = ?) AND rw >= ?
```

**DB connect tries**: on startup, the plugin will try to connect and ping the DB a max number of times or forever depending on `sqlite_connect_tries` option.
**DB connect tries**: on startup, depending on `sqlite_connect_tries` option, the plugin will try to connect and ping the DB a max number of times or forever every 2 seconds.
By default it will try to reconnect forever to maintain backwards compatibility and avoid issues when `mosquitto` starts before the DB service does,
but you may choose to ping a max amount of times by setting any positive number.
If given 0, the DB will try to connect only once, which would be the same as setting the option to 1.
Expand Down
6 changes: 3 additions & 3 deletions backends/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,20 @@ func OpenDatabase(dsn, engine string, tries int) (*sqlx.DB, error) {

for tries != 0 {
if err = db.Ping(); err != nil {
log.Errorf("ping database error, will retry in 2s: %s", err)
log.Errorf("ping database %s error, will retry in 2s: %s", engine, err)
time.Sleep(2 * time.Second)
} else {
break
}

// No need to decrease when pinging forever, i.e. when tries < 0.
if tries > 0 {
tries--
}
}

// Return last ping error when done trying.
if tries == 0 {
return nil, fmt.Errorf("couldn't ping database %s", engine)
return nil, fmt.Errorf("couldn't ping database %s: %s", engine, err)
}

return db, nil
Expand Down

0 comments on commit 1efdaab

Please sign in to comment.