Skip to content
This repository has been archived by the owner on Dec 9, 2022. It is now read-only.

Be robust against postgresql not being available #5

Closed
Merovius opened this issue Jun 9, 2015 · 1 comment
Closed

Be robust against postgresql not being available #5

Merovius opened this issue Jun 9, 2015 · 1 comment
Assignees

Comments

@Merovius
Copy link
Contributor

Merovius commented Jun 9, 2015

During boot or restarts of postgresql, it might be, that the connection is temporarily unavailable. We should a) open connections on-demand (only open it, when we try to use it the first time) and b) retry a limited number of times (e.g. exponential backoff), when an error occured due to it closing unexpectedly. database/sql apparently doesn't expose a special error for that, so we have to investigate whether we can catch that in other ways (e.g. if it passes through an error about reading/writing to a closed socket) or need to do a catch-all, on error reopen connection thing.

This is the same issue as #5 and should be resolved in the same way.

@Merovius
Copy link
Contributor Author

Merovius commented Jul 1, 2015

After looking at http://godoc.org/database/sql#Open I think it should be enough to just open the connection on the first use. For that, I recommend:
• Adding a sync.Mutex as a global variable to protect db against concurrent changes
• Write a helper function, that Locks the mutex, checks if db is nil and if not, opens a connection. Then it returns the (possibly new) Pointer db and an error, if any occured
• Use this helper function in all sql-functions to get a *DB and use that.
• Remove the code from main, opening a connection.

Also, move to http://godoc.org/database/sql#DB.Ping instead of the SELECT 1 query.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants