--
type RouterLock struct {
lock sync.RWMutex
RouterIdcs map[string]string
}
// get the map's value
func (this *RouterLock) Get(key string) (v string) {
this.lock.RLock()
defer this.lock.RUnlock()
if v, ok := this.RouterIdcs[key]; ok {
return v
}
return ""
}
//update the map, this function is activated when the data changed.
func LoadRouter() {RouterL.lock.Lock()
defer RouterL.lock.Unlock()resp, err := http.Get(beego.AppConfig.String("routerurl"))if err != nil {beego.Critical("http get info error")return}
defer resp.Body.Close()body, err := ioutil.ReadAll(resp.Body)err = json.Unmarshal(body, &RouterL.RouterIdcs)if err != nil {beego.Critical("json.Unmarshal routerurl error:", err)}
}
--
-Simon Watt
Ian
--
When two goroutines hold their respective locks which are pointed to by two different keys, they can update the map simultaneously.// goroutine1locks[1].Lock()m[1] = 1locks[1].Unlock()// goroutine2locks[2].Lock()m[2] = 2locks[2].Unlock()How could the map be goroutine-safe?Did I misread the code?
--
Does Go have some sort of concept equivalent to generics that would allow you to code a generic ConcurrentMap implementation where the underlying map key/value types could be pluggable?
Interesting. I will take your basic RWMutex implementation and fool around with it. It's a bit painful to have to create a different implementation for every type we need to store, but we only have < 10 of them, so it should be OK.So, here is an interesting follow up question:If I load a massive amount of data into (let's say 64GB), should I expect some massive GC pauses from the Go runtime?
Hi everyone,first, sorry for the newbie question, but despite searching on the web I am not sure I fully understand how to best implement what we need.We basically have a close-to-realtime app (SLA in milliseconds) that relies heavily on in-memory caching (no I/O hits to DB when replying to a request).The cache is used mostly for reads, but there is a background process that periodically refreshes it from the DB.So we basically have a single WRITER, multiple READERs.In our Java app, this is a simple use of the built-in ConcurrentMap object that allows for high performance in-memory maps that are thread safe.How would be go about implementing the same paradigm in Go?Is the built in map object thread safe?If I update a key value in it while another reader is reading the same key what is going to happen?
Are we supposed to wrap it all via locks?
How do channels fit into this?etc., etc.We are basically evaluating Go as an alternative to Java for some of our real-time needs and this is sort of the first big hurdle that we're not surehow to handle to ensure maximum performance (which is our primary concern).Thanks in advanceJacek--
--
If your map is read only, you don't need to lock it. It sounds like you produce a series of read only maps and that the background process will produce a new one (as opposed to modifying the old one) so you could use a scheme like this:
That would be a good case for the distributed rwmutex:Unfortunately it is not supported now...
Yes, per-CPU mutexes avoid contention (not thread blocking, but
low-level contention on mutex cache-line). It should be much faster
than plain rwmutex for read-mostly data. I observed up to 300x speedup
on synthetic benchmark.
That's not required. Just read-lock distributed rwmutex, read from
global map. No shards.
The distributed rwmutex does not have real per-CPU data, it hasper-Machine data, Machine is a Go runtime concept that is roughly
equivalent to OS threads.