-
Notifications
You must be signed in to change notification settings - Fork 17
/
turbo_handler.go
203 lines (169 loc) · 5.05 KB
/
turbo_handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package turbo
import (
"errors"
"time"
)
//处理器
type IEventProcessor interface {
Process(ctx *DefaultPipelineContext, event IEvent) error
//事件类型判断
TypeAssert(event IEvent) bool
}
//处理器接口
type IHandler interface {
GetName() string //获得当前处理的handler名称
HandleEvent(ctx *DefaultPipelineContext, event IEvent) error
//检查是否可以处理改event
AcceptEvent(event IEvent) bool
}
//处理向后的事件的handler
type IBackwardHandler interface {
IHandler
HandleBackward(ctx *DefaultPipelineContext, event IBackwardEvent) error
}
//处理向前的handler
type IForwardHandler interface {
IHandler
HandleForward(ctx *DefaultPipelineContext, event IForwardEvent) error
}
var ERROR_INVALID_EVENT_TYPE error = errors.New("ERROR INVALID EVENT TYPE")
//-------------基本的forward处理
type BaseForwardHandler struct {
IForwardHandler
processor IEventProcessor //类型判断的实现
name string
}
func NewBaseForwardHandler(name string, processor IEventProcessor) BaseForwardHandler {
return BaseForwardHandler{
name: name,
processor: processor}
}
func (self *BaseForwardHandler) GetName() string {
return self.name
}
//检查是否可以处理改event
func (self *BaseForwardHandler) AcceptEvent(event IEvent) bool {
//是否可以处理当前按的event,再去判断具体的可处理事件类型
_, ok := event.(IForwardEvent)
return ok
}
func (self *BaseForwardHandler) HandleForward(ctx *DefaultPipelineContext, event IForwardEvent) error {
//处理逻辑成功则向后传递
if !self.processor.TypeAssert(event) {
ctx.SendForward(event)
return nil
} else {
now := time.Now().Unix()
err := self.processor.Process(ctx, event)
cost := time.Now().Unix() - now
if cost > 100 {
log.Warnf("BaseForwardHandler|%s|cost:%d", self.GetName(), cost)
}
return err
}
}
func (self *BaseForwardHandler) HandleEvent(ctx *DefaultPipelineContext, event IEvent) error {
return self.HandleForward(ctx, event)
}
//-------------基本的backward处理
type BaseBackwardHandler struct {
IBackwardHandler
processor IEventProcessor //类型判断的实现
name string
}
func NewBaseBackwardHandler(name string, processor IEventProcessor) BaseBackwardHandler {
return BaseBackwardHandler{
name: name,
processor: processor}
}
func (self *BaseBackwardHandler) GetName() string {
return self.name
}
//检查是否可以处理改event
func (self *BaseBackwardHandler) AcceptEvent(event IEvent) bool {
//是否可以处理当前按的event,再去判断具体的可处理事件类型
_, ok := event.(IBackwardEvent)
return ok
}
func (self *BaseBackwardHandler) HandleBackward(ctx *DefaultPipelineContext, event IBackwardEvent) error {
//处理逻辑成功则向后传递
if !self.processor.TypeAssert(event) {
ctx.SendBackward(event)
return nil
} else {
now := time.Now().Unix()
err := self.processor.Process(ctx, event)
cost := time.Now().Unix() - now
if cost > 100 {
log.Warnf("BaseBackwardHandler|%s|cost:%d", self.GetName(), cost)
}
return err
}
}
func (self *BaseBackwardHandler) HandleEvent(ctx *DefaultPipelineContext, event IEvent) error {
return self.HandleBackward(ctx, event)
}
//----------------DoubleSided
type BaseDoubleSidedHandler struct {
IBackwardHandler
IForwardHandler
processor IEventProcessor //类型判断的实现
name string
}
func NewBaseDoubleSidedHandler(name string, processor IEventProcessor) BaseDoubleSidedHandler {
return BaseDoubleSidedHandler{
name: name,
processor: processor}
}
func (self *BaseDoubleSidedHandler) GetName() string {
return self.name
}
//检查是否可以处理改event
func (self *BaseDoubleSidedHandler) AcceptEvent(event IEvent) bool {
//是否可以处理当前按的event,再去判断具体的可处理事件类型
_, bok := event.(IBackwardEvent)
_, fok := event.(IForwardEvent)
return bok || fok
}
func (self *BaseDoubleSidedHandler) HandleBackward(ctx *DefaultPipelineContext, event IBackwardEvent) error {
//处理逻辑成功则向后传递
if !self.processor.TypeAssert(event) {
ctx.SendBackward(event)
return nil
} else {
now := time.Now().Unix()
err := self.processor.Process(ctx, event)
cost := time.Now().Unix() - now
if cost > 100 {
log.Warnf("BaseDoubleSidedHandler|%s|cost:%d", self.GetName(), cost)
}
return err
}
}
func (self *BaseDoubleSidedHandler) HandleForward(ctx *DefaultPipelineContext, event IForwardEvent) error {
//处理逻辑成功则向后传递
if !self.processor.TypeAssert(event) {
ctx.SendForward(event)
return nil
} else {
now := time.Now().Unix()
err := self.processor.Process(ctx, event)
cost := time.Now().Unix() - now
if cost > 100 {
log.Warnf("BaseDoubleSidedHandler|%s|cost:%d", self.GetName(), cost)
}
return err
}
}
func (self *BaseDoubleSidedHandler) HandleEvent(ctx *DefaultPipelineContext, event IEvent) error {
fe, ok := event.(IForwardEvent)
if ok {
return self.HandleForward(ctx, fe)
} else {
be, ok := event.(IBackwardEvent)
if ok {
return self.HandleBackward(ctx, be)
}
}
return errors.New("ILLEGAL EVENT TYPE ")
}