-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathapp.go
executable file
·159 lines (134 loc) · 3.49 KB
/
app.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
package fastapi
import (
"log"
"net"
"runtime/debug"
"time"
fastway "github.com/funny/fastway/go"
"github.com/funny/link"
"github.com/funny/pprof"
"github.com/funny/slab"
)
type Handler interface {
InitSession(*link.Session) error
Transaction(*link.Session, Message, func())
}
type App struct {
serviceTypes []*ServiceType
services [256]Provider
timeRecoder *pprof.TimeRecorder
Pool slab.Pool
ReadBufSize int
SendChanSize int
MaxRecvSize int
MaxSendSize int
RecvTimeout time.Duration
SendTimeout time.Duration
}
func New() *App {
return &App{
timeRecoder: pprof.NewTimeRecorder(),
Pool: &slab.NoPool{},
ReadBufSize: 1024,
SendChanSize: 1024,
MaxRecvSize: 64 * 1024,
MaxSendSize: 64 * 1024,
}
}
func (app *App) handleSessoin(session *link.Session, handler Handler) {
defer session.Close()
if handler.InitSession(session) != nil {
return
}
for {
msg, err := session.Receive()
if err != nil {
return
}
req := msg.(Message)
handler.Transaction(session, req, func() {
startTime := time.Now()
app.services[req.ServiceID()].(Service).HandleRequest(session, req)
app.timeRecoder.Record(req.Identity(), time.Since(startTime))
})
}
}
func (app *App) TimeRecoder() *pprof.TimeRecorder {
return app.timeRecoder
}
func (app *App) Dial(network, address string) (*link.Session, error) {
return link.Dial(network, address, link.ProtocolFunc(app.newClientCodec), app.SendChanSize)
}
func (app *App) Listen(network, address string, handler Handler) (*link.Server, error) {
listener, err := net.Listen(network, address)
if err != nil {
return nil, err
}
return app.NewServer(listener, handler), nil
}
func (app *App) NewClient(conn net.Conn) *link.Session {
codec, _ := app.newClientCodec(conn)
return link.NewSession(codec, app.SendChanSize)
}
func (app *App) NewServer(listener net.Listener, handler Handler) *link.Server {
if handler == nil {
handler = &noHandler{}
}
return link.NewServer(
listener, link.ProtocolFunc(app.newServerCodec), app.SendChanSize,
link.HandlerFunc(func(session *link.Session) {
app.handleSessoin(session, handler)
}),
)
}
func (app *App) NewFastwayClient(conn net.Conn, cfg fastway.EndPointCfg) *fastway.EndPoint {
cfg.MsgFormat = &msgFormat{app.newResponse}
return fastway.NewClient(conn, cfg)
}
func (app *App) NewFastwayServer(conn net.Conn, cfg fastway.EndPointCfg, handler Handler) (*FastwayServer, error) {
cfg.MsgFormat = &msgFormat{app.newRequest}
endpoint, err := fastway.NewServer(conn, cfg)
if err != nil {
return nil, err
}
if handler == nil {
handler = &noHandler{}
}
return &FastwayServer{app, endpoint, handler}, nil
}
type FastwayServer struct {
app *App
endpoint *fastway.EndPoint
handler Handler
}
func (s *FastwayServer) Serve() error {
for {
session, err := s.endpoint.Accept()
if err != nil {
return err
}
go s.app.handleSessoin(session, s.handler)
}
}
func (s *FastwayServer) GetSession(sessionID uint64) *link.Session {
return s.endpoint.GetSession(sessionID)
}
func (s *FastwayServer) Stop() {
s.endpoint.Close()
}
type noHandler struct {
}
func (t *noHandler) DropSession(session *link.Session) {
}
func (t *noHandler) InitSession(session *link.Session) error {
return nil
}
func (t *noHandler) Transaction(session *link.Session, req Message, work func()) {
defer func() {
if err := recover(); err != nil {
log.Printf("fastapi: unhandled panic when processing '%s' - '%s'", req.Identity(), err)
log.Println(string(debug.Stack()))
}
}()
work()
}