zinx icon indicating copy to clipboard operation
zinx copied to clipboard

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

Open trash-boy opened this issue 1 year ago • 2 comments

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,还有是不是需要加一个写锁,防止并发安全的问题,望解答!!!

trash-boy avatar Dec 14 '23 03:12 trash-boy

8031702544717_ pic_hd

aceld avatar Dec 14 '23 09:12 aceld