map 的一个神奇的问题
使用下面的一段代码来统计链接的访问情况,使用方法就是用 Incr 每次访问加 1 单独测试的时候,比如用 wrk 来压测都挺正常的 神奇的是,一换成 nginx,c.data 里面的 key 就变得不正常,把他打印出来,发现有很多相同的 key 比如本来就只有一个 key 的,他会出现 key: 1, key: 2, key: 1, key: 1 这种,搞不懂。。。
type Counter struct { sync.RWMutex data map[string]int } func (c *Counter) Get(key string) int { c.RLock() count := c.data[key] c.RUnlock() return count } func (c *Counter) Incr(key string) int { c.Lock() c.data[key]++ count := c.data[key] c.Unlock() return count } func (c *Counter) Delete(key string) { c.Lock() delete(c.data, key) c.Unlock() }