mirror of
https://github.com/duanhf2012/origin.git
synced 2026-02-03 22:45:13 +08:00
修改事件处理机制
This commit is contained in:
@@ -10,7 +10,7 @@ import (
|
|||||||
const Default_EventChannelLen = 10000
|
const Default_EventChannelLen = 10000
|
||||||
|
|
||||||
//事件接受器
|
//事件接受器
|
||||||
type EventReciver func(event *Event) error
|
type EventReciverFunc func(event *Event)
|
||||||
|
|
||||||
type Event struct {
|
type Event struct {
|
||||||
Type EventType
|
Type EventType
|
||||||
@@ -19,10 +19,12 @@ type Event struct {
|
|||||||
|
|
||||||
type IEventProcessor interface {
|
type IEventProcessor interface {
|
||||||
NotifyEvent(*Event)
|
NotifyEvent(*Event)
|
||||||
OnEventHandler(event *Event) error
|
|
||||||
SetEventReciver(eventProcessor IEventProcessor)
|
SetEventReciver(eventProcessor IEventProcessor)
|
||||||
GetEventReciver() IEventProcessor
|
GetEventReciver() IEventProcessor
|
||||||
SetEventChanNum(num int32) bool
|
SetEventChanNum(num int32) bool
|
||||||
|
RegEventReciverFunc(eventType EventType,reciverFunc EventReciverFunc)
|
||||||
|
UnRegEventReciverFun(eventType EventType)
|
||||||
}
|
}
|
||||||
|
|
||||||
type EventProcessor struct {
|
type EventProcessor struct {
|
||||||
@@ -32,6 +34,15 @@ type EventProcessor struct {
|
|||||||
|
|
||||||
eventChanNumLocker sync.RWMutex
|
eventChanNumLocker sync.RWMutex
|
||||||
eventChanNum int32
|
eventChanNum int32
|
||||||
|
mapEventReciverFunc map[EventType]EventReciverFunc
|
||||||
|
}
|
||||||
|
|
||||||
|
func (slf *EventProcessor) RegEventReciverFunc(eventType EventType,reciverFunc EventReciverFunc){
|
||||||
|
slf.mapEventReciverFunc[eventType] = reciverFunc
|
||||||
|
}
|
||||||
|
|
||||||
|
func (slf *EventProcessor) UnRegEventReciverFun(eventType EventType){
|
||||||
|
delete(slf.mapEventReciverFunc,eventType)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (slf *EventProcessor) NotifyEvent(pEvent *Event) {
|
func (slf *EventProcessor) NotifyEvent(pEvent *Event) {
|
||||||
@@ -41,9 +52,7 @@ func (slf *EventProcessor) NotifyEvent(pEvent *Event) {
|
|||||||
slf.EventChan <-pEvent
|
slf.EventChan <-pEvent
|
||||||
}
|
}
|
||||||
|
|
||||||
func (slf *EventProcessor) OnEventHandler(event *Event) error{
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (slf *EventProcessor) GetEventChan() chan *Event{
|
func (slf *EventProcessor) GetEventChan() chan *Event{
|
||||||
slf.eventChanNumLocker.Lock()
|
slf.eventChanNumLocker.Lock()
|
||||||
@@ -85,7 +94,7 @@ type IHttpEventData interface {
|
|||||||
Handle()
|
Handle()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (slf *EventProcessor) EventHandler(processor IEventProcessor,ev *Event) {
|
func (slf *EventProcessor) EventHandler(ev *Event) {
|
||||||
defer func() {
|
defer func() {
|
||||||
if r := recover(); r != nil {
|
if r := recover(); r != nil {
|
||||||
buf := make([]byte, 4096)
|
buf := make([]byte, 4096)
|
||||||
@@ -99,7 +108,11 @@ func (slf *EventProcessor) EventHandler(processor IEventProcessor,ev *Event) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
processor.OnEventHandler(ev)
|
if fun,ok := slf.mapEventReciverFunc[ev.Type];ok == false{
|
||||||
|
return
|
||||||
|
}else{
|
||||||
|
fun(ev)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (slf *EventProcessor) innerEventHandler(ev *Event) bool {
|
func (slf *EventProcessor) innerEventHandler(ev *Event) bool {
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package service
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/duanhf2012/origin/event"
|
|
||||||
"github.com/duanhf2012/origin/log"
|
"github.com/duanhf2012/origin/log"
|
||||||
"github.com/duanhf2012/origin/profiler"
|
"github.com/duanhf2012/origin/profiler"
|
||||||
"github.com/duanhf2012/origin/rpc"
|
"github.com/duanhf2012/origin/rpc"
|
||||||
@@ -130,7 +129,7 @@ func (slf *Service) Run() {
|
|||||||
if slf.profiler!=nil {
|
if slf.profiler!=nil {
|
||||||
analyzer = slf.profiler.Push(fmt.Sprintf("Event_%d", int(ev.Type)))
|
analyzer = slf.profiler.Push(fmt.Sprintf("Event_%d", int(ev.Type)))
|
||||||
}
|
}
|
||||||
slf.EventHandler(slf.this.(event.IEventProcessor),ev)
|
slf.EventHandler(ev)
|
||||||
if analyzer!=nil {
|
if analyzer!=nil {
|
||||||
analyzer.Pop()
|
analyzer.Pop()
|
||||||
analyzer = nil
|
analyzer = nil
|
||||||
|
|||||||
Reference in New Issue
Block a user