caching - In Go, what data types are safe to read and write to after retrieving it from a concurrently accessible structure? -
caching - In Go, what data types are safe to read and write to after retrieving it from a concurrently accessible structure? -
probably not clear question i'm after here, allow me clarify. exercise in concurrency, i'm playing around writing cache needs accessible multiple simultaneous requests. cache content of type interface{}, can include anything, including slices, maps, , structs. when grab method, rlock on while reading , homecoming content , finish deferred runlock.
this works fine numbers , strings , other values automatically copied on return. i'm concerned slices, maps, , structs not copied, such thing returned, if read or modified if copy, altering info in cache , doing outside of mutex.
of course, that's problem under race conditions. don't want homecoming not safe alter , pass set method update. here questions:
1) right in assuming these info types nowadays problems scenario this?
2) how might 1 go resolving issue, create method values can freely manipulated without fear of failure under race conditions?
you right in assuming kinds of info types references , pointers structs can cause problem reasons i'll talk below.
i see 2 problems dealing with. first, need protect cache concurrent access such cache in right state. if mutating cache , using "write" lock, cache maintain integrity when beingness changed in way. also, long take "read" lock when reading cache guaranteed read out of cache same integrity. stands, locks protecting cache only work @ protecting cache itself. these locks not protect items stored within cache.
this sec issue dealing with: assuming cache protected, think happen if 2 separate goroutines synchronized operation cache. don't have object @ same time if somehow end "getting" pointer struct or reference map/slice means potentially both mutate same object of both holding references to. manifests sec problem describing.
so options?
only store value types have observed can limiting and/or expensive because must copied. only store custom types synchronized making sure take appropriate locks on when mutated or read. make cache smarter has concept of ownership gladly homecoming object cache , ever allow 1 goroutine "hold" onto until goroutine finished. other goroutines have wait object released until previous goroutine has finished it. either that, or design fail , homecoming if tried item unavailable. this concept used build distributed locks in client server architecture there many clients want access object , distributed lock ensures 1 client can ever hold lock.consider concept of ownership important. might say: utilize channels, prepare everything. can end in same boat if send reference type or pointer struct 5 different channels. 5 different channels perchance mutate same object holding onto. uh oh! same problem manifests again. why it's of import when pass item channel giving ownership not mutate it.
as told me today...concurrent programming hard , there additional patterns out there seek hope gives more insight problem dealing with. 1 thing know there isn't bullet-proof reply this, much of depend on nature of how application behaves ultimately.
caching concurrency go mutex race-condition
Comments
Post a Comment