[Feature]: gnet.Conn 增加一个并发安全的map
cat3306 opened this issue · comments
Description of new feature
gnet.Conn 增加一个并发安全的map,并且提供存储,删除,获取API接口
Scenarios for new feature
方便存储一些用户自定义数据,而不需存储在全局的map中。类似gin.Context keys一样
gin
// Keys is a key/value pair exclusively for the context of each request.
Keys map[string]any
Breaking changes or not?
No
Code snippets (optional)
func (c *conn) SetProperty(key string, value interface{}) {
c.mu.Lock()
if c.properties == nil {
c.properties = make(map[string]interface{})
}
c.properties[key] = value
c.mu.Unlock()
}
func (c *conn) GetProperty(key string) (value interface{}, exists bool) {
c.mu.RLock()
value, exists = c.properties[key]
c.mu.RUnlock()
return
}
func (c *conn) DelProperty(key string) {
c.mu.Lock()
defer c.mu.Unlock()
delete(c.properties, key)
}
Alternatives for new feature
None.
Additional context (optional)
None.
用 SetContext
/Context
不就行了?你把 map 放在里面就行了。