优化事件-减少GC

This commit is contained in:
boyce
2020-11-02 16:50:45 +08:00
parent c98de9b1e9
commit 060095baea
2 changed files with 10 additions and 12 deletions

View File

@@ -41,7 +41,7 @@ type IEventProcessor interface {
addListen(eventType EventType,reciver IEventHandler) addListen(eventType EventType,reciver IEventHandler)
removeBindEvent(eventType EventType,reciver IEventHandler) removeBindEvent(eventType EventType,reciver IEventHandler)
removeListen(eventType EventType,reciver IEventHandler) removeListen(eventType EventType,reciver IEventHandler)
GetEventChan() chan *Event GetEventChan() chan Event
} }
type EventHandler struct { type EventHandler struct {
@@ -55,7 +55,7 @@ type EventHandler struct {
type EventProcessor struct { type EventProcessor struct {
eventChannel chan *Event eventChannel chan Event
locker sync.RWMutex locker sync.RWMutex
mapListenerEvent map[EventType]map[IEventProcessor]int //监听者信息 mapListenerEvent map[EventType]map[IEventProcessor]int //监听者信息
@@ -109,7 +109,6 @@ func (handler *EventHandler) Init(processor IEventProcessor){
handler.mapRegEvent =map[EventType]map[IEventProcessor]interface{}{} handler.mapRegEvent =map[EventType]map[IEventProcessor]interface{}{}
} }
func (processor *EventProcessor) SetEventChannel(channelNum int) bool{ func (processor *EventProcessor) SetEventChannel(channelNum int) bool{
processor.locker.Lock() processor.locker.Lock()
defer processor.locker.Unlock() defer processor.locker.Unlock()
@@ -121,7 +120,7 @@ func (processor *EventProcessor) SetEventChannel(channelNum int) bool{
channelNum = DefaultEventChannelLen channelNum = DefaultEventChannelLen
} }
processor.eventChannel = make(chan *Event,channelNum) processor.eventChannel = make(chan Event,channelNum)
return true return true
} }
@@ -182,6 +181,8 @@ func (processor *EventProcessor) UnRegEventReciverFun(eventType EventType,recive
} }
func (handler *EventHandler) Destroy(){ func (handler *EventHandler) Destroy(){
handler.locker.Lock()
defer handler.locker.Unlock()
for eventTyp,mapEventProcess := range handler.mapRegEvent { for eventTyp,mapEventProcess := range handler.mapRegEvent {
if mapEventProcess == nil { if mapEventProcess == nil {
continue continue
@@ -193,12 +194,12 @@ func (handler *EventHandler) Destroy(){
} }
} }
func (processor *EventProcessor) GetEventChan() chan *Event{ func (processor *EventProcessor) GetEventChan() chan Event{
processor.locker.Lock() processor.locker.Lock()
defer processor.locker.Unlock() defer processor.locker.Unlock()
if processor.eventChannel == nil { if processor.eventChannel == nil {
processor.eventChannel =make(chan *Event,DefaultEventChannelLen) processor.eventChannel =make(chan Event,DefaultEventChannelLen)
} }
return processor.eventChannel return processor.eventChannel
@@ -223,16 +224,13 @@ func (processor *EventProcessor) EventHandler(ev *Event) {
} }
} }
func (processor *EventProcessor) pushEvent(event *Event){ func (processor *EventProcessor) pushEvent(event *Event){
if len(processor.eventChannel)>=cap(processor.eventChannel){ if len(processor.eventChannel)>=cap(processor.eventChannel){
log.Error("event process channel is full.") log.Error("event process channel is full.")
return return
} }
processor.eventChannel<-event processor.eventChannel<-*event
} }
func (processor *EventProcessor) castEvent(event *Event){ func (processor *EventProcessor) castEvent(event *Event){

View File

@@ -130,7 +130,7 @@ func (s *Service) Run() {
if s.profiler!=nil { if s.profiler!=nil {
analyzer = s.profiler.Push(fmt.Sprintf("[Event]%d", int(ev.Type))) analyzer = s.profiler.Push(fmt.Sprintf("[Event]%d", int(ev.Type)))
} }
s.eventProcessor.EventHandler(ev) s.eventProcessor.EventHandler(&ev)
if analyzer!=nil { if analyzer!=nil {
analyzer.Pop() analyzer.Pop()
analyzer = nil analyzer = nil