-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
45 lines (38 loc) · 801 Bytes
/
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
package giao
import (
"sync"
)
type Handler struct {
Handle func(in Msg, session Session)
InputPool Pool
}
type Msg interface {
Size() int
MarshalTo(data []byte) (n int, err error)
Unmarshal(data []byte) error
}
type ChainMsgHandler func(in Msg, session Session) bool
type PipelineHandler struct {
Handler
hs []ChainMsgHandler
}
func NewPipelineHandler(reqPool *sync.Pool) *PipelineHandler {
ph := &PipelineHandler{}
ph.InputPool = reqPool
ph.hs = make([]ChainMsgHandler, 0)
return ph
}
func (p *PipelineHandler) Append(h ChainMsgHandler) *PipelineHandler {
p.hs = append(p.hs, h)
return p
}
func (p *PipelineHandler) Build() *Handler {
p.Handle = func(req Msg, session Session) {
for _, h := range p.hs {
if !h(req, session) {
break
}
}
}
return &p.Handler
}