go - Increment field value in different records avoiding race condition in Golang -
my golang code gets different records database using goroutines, , increments value in determinated field in record.
i can avoid race status if utilize mutex or channels, have bottleneck because every access database waits until previous access done.
i think should 1 mutex every different record, instead 1 mutex all.
how it?
thanks reply.
in comments said using couchbase. if record wish update consists of integer, can utilize built in atomic increment functionality bucket.incr
.
if value part of larger document, can utilize database's "check , set" functionality. in essence, want create loop following:
retrieve record updated along cas value usingbucket.gets
modify document returned (1) needed. store modified document using bucket.cas
, passing cas value retrieved in (1). if (4) succeeds, break out of loop. otherwise, start 1 time again @ (1). note of import retrieve document fresh each time in loop. if update fails due wrong cas value, means document updated between read , write.
go
No comments:
Post a Comment