Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RouterSlices中Handlers的意义还存在逻辑错误 #291

Open
trash-boy opened this issue Dec 14, 2023 · 1 comment
Open

RouterSlices中Handlers的意义还存在逻辑错误 #291

trash-boy opened this issue Dec 14, 2023 · 1 comment

Comments

@trash-boy
Copy link
Contributor

func (r *RouterSlices) AddHandler(msgId uint32, Handlers ...ziface.RouterHandler) {
	// 1. Check if the API handler method bound to the current msg already exists
	if _, ok := r.Apis[msgId]; ok {
		panic("repeated api , msgId = " + strconv.Itoa(int(msgId)))
	}

	finalSize := len(r.Handlers) + len(Handlers)
	mergedHandlers := make([]ziface.RouterHandler, finalSize)
	copy(mergedHandlers, r.Handlers)
	copy(mergedHandlers[len(r.Handlers):], Handlers)
	r.Apis[msgId] = append(r.Apis[msgId], mergedHandlers...)
}
type RouterSlices struct {
    Apis     map[uint32][]ziface.RouterHandler
    Handlers []ziface.RouterHandler
    sync.RWMutex
}

我不理解这个结构体中Handlers的意义,我的理解应该是每一个msgid有一个自己的handler切片,但是可以添加handler,为什么要判断当前msgid是否在map中存在,此外为什么新的msgID需要加上原来的r.Handlers,还有是不是需要加一个写锁,防止并发安全的问题,望解答!!!

@aceld
Copy link
Owner

aceld commented Dec 14, 2023

8031702544717_ pic_hd

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants