Is this method for updating the pointer of a pool connection safe? #1752
Unanswered
curiosport
asked this question in
Q&A
Replies: 1 comment
-
You can safely pass the pointer around. If your assignment is simply initialization before the program begins operation it should be safe. But if you are actually switching pools while other operations are concurrently executing then you may have a problem. This is not specific to pgxpool. You would have the same issue with any shared value. You could run the race detector to check if your usage is safe. If not your simplest solution may be to use https://pkg.go.dev/sync/atomic#Pointer. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Within a package I want to use a global pointer as a connection to the database, I currently do this and have not had any problems:
With a
GetPool
function I getdbpool
in other packages. What happens in my example is thatdbpool
simply switches pointers (which from my perspective is safe to use in the long term if used in the right order).And I was wondering if it is safe to apply this other method:
In this last example the pointer is preserved, I have used it and had no problems either but I don't know what could happen in the long term, I really like this last option, but is it safe? This update is performed only once during the entire execution of the server.
The reason I ask this is because I noticed that the
Pool
structure has all its fields as non-exportable, and with this action:I could misconfigure dbpool.
Beta Was this translation helpful? Give feedback.
All reactions