Concurrency: race conditions and mutex locks#153
Concurrency: race conditions and mutex locks#153bennyscetbun wants to merge 5 commits intogo-gorp:masterfrom
Conversation
|
Do we need a separate mutex per method on TableMap? What if we just had a single mutex and used it on each of the relevant methods Any other opinions? |
|
The idea was to avoid a single lock(in case that we ve got a lot of goroutine doing different things). |
|
That makes sense, although the contention would likely still occur with 4 mutexes instead of one. If we're worried about contention (and it's a valid concern I think) we may want to move the lock inside that the code that checks for empty string. For example: |
|
That makes sense. Thanks. |
|
I think it must be before The racing condition was on plan.query ;) And that way we avoid to run twice the creation code. By the way thanks for your lib! |
|
I think we need to take a good look at this, it is very important to make sure gorp is concurrency safe! |
a9f5c20 to
361a068
Compare
Previously, plans were initialised through a racy assignment. Now they are initialised through a sync.Once, which is more clearly race free and also makes the intent clear (it is initialisation). With this change it's also necessary to make the plan more pointer-like, since if you pass a struct around by value with a lock in it, you are going to have a bad time. This was pointed out to me by the gometalinter. Fixes go-gorp#296. Supercedes go-gorp#153.
Previously, plans were initialised through a racy assignment. Now they are initialised through a sync.Once, which is more clearly race free and also makes the intent clear (it is initialisation). With this change it's also necessary to make the plan more pointer-like, since if you pass a struct around by value with a lock in it, you are going to have a bad time. This was pointed out to me by the gometalinter. Fixes go-gorp#296. Supercedes go-gorp#153.
|
Superseded by #301, which uses |
Adding mutex on every Plan of a table to avoid race condition when using goroutine and Gorp.