panjf2000 / gnet

🚀 gnet is a high-performance, lightweight, non-blocking, event-driven networking framework written in pure Go.

Home Page:https://gnet.host

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[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 放在里面就行了。

它只会提供网络应用所需的最核心的功能和最精简的 APIs,而且 gnet 也并没有打算变成一个无所不包的网络框架

这是 gnet 设计宗旨,只提供必需的功能,这个 issue 里的需求在我看来再业务层用现有的 API 就能实现,而且实现也不复杂,所以没必要放进框架里,所以暂时不考虑。