/
handler.go
248 lines (226 loc) · 5.94 KB
/
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*--------------------------------------------------------*\
| |
| hprose |
| |
| Official WebSite: https://hprose.com |
| |
| rpc/socket/handler.go |
| |
| LastModified: Nov 22, 2022 |
| Author: Ma Bingyao <andot@hprose.com> |
| |
\*________________________________________________________*/
package socket
import (
"context"
"crypto/tls"
"io"
"math"
"net"
"reflect"
"time"
"github.com/hprose/hprose-golang/v3/internal/convert"
"github.com/hprose/hprose-golang/v3/rpc/core"
)
type Handler struct {
Service *core.Service
Pool core.WorkerPool
OnAccept func(net.Conn) net.Conn
OnClose func(net.Conn)
OnError func(net.Conn, error)
}
// BindContext to the http server.
func (h *Handler) BindContext(ctx context.Context, server core.Server) {
go h.bind(ctx, server.(net.Listener))
}
func (h *Handler) bind(ctx context.Context, listener net.Listener) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
var tempDelay time.Duration // how long to sleep on accept failure
for {
conn, err := listener.Accept()
if err != nil {
tempDelay = nextTempDelay(err, h.onError, tempDelay)
if tempDelay > 0 {
continue
}
return
}
tempDelay = 0
go h.Serve(ctx, conn)
}
}
func (h *Handler) onAccept(conn net.Conn) net.Conn {
if h.OnAccept != nil {
return h.OnAccept(conn)
}
return conn
}
func (h *Handler) onClose(conn net.Conn) {
if h.OnClose != nil {
h.OnClose(conn)
}
}
func (h *Handler) onError(conn net.Conn, err error) {
if h.OnError != nil {
h.OnError(conn, err)
}
}
func (h *Handler) reportError(ctx context.Context, errChan chan error, err error) {
select {
case <-ctx.Done():
case errChan <- err:
default:
}
}
func (h *Handler) sendResponse(ctx context.Context, queue chan data, index int, body []byte, err error) {
select {
case <-ctx.Done():
case queue <- data{
Index: index,
Body: body,
Error: err,
}:
}
}
func (h *Handler) getServiceContext(conn net.Conn) *core.ServiceContext {
serviceContext := core.NewServiceContext(h.Service)
serviceContext.Items().Set("conn", conn)
serviceContext.LocalAddr = conn.LocalAddr()
serviceContext.RemoteAddr = conn.RemoteAddr()
serviceContext.Handler = h
return serviceContext
}
func (h *Handler) run(ctx context.Context, queue chan data, index int, body []byte) {
var err error
defer func() {
if e := recover(); e != nil {
err = core.NewPanicError(e)
}
h.sendResponse(ctx, queue, index, body, err)
}()
body, err = h.Service.Handle(ctx, body)
}
func (h *Handler) task(ctx context.Context, queue chan data, index int, body []byte) func() {
return func() {
h.run(ctx, queue, index, body)
}
}
func (h *Handler) catch(ctx context.Context, errChan chan error) {
if e := recover(); e != nil {
h.reportError(ctx, errChan, core.NewPanicError(e))
}
}
func (h *Handler) receive(ctx context.Context, conn net.Conn, queue chan data, errChan chan error) {
defer h.catch(ctx, errChan)
var header [12]byte
for {
select {
case <-ctx.Done():
return
default:
if _, err := io.ReadAtLeast(conn, header[:], 12); err != nil {
h.reportError(ctx, errChan, err)
return
}
length, index, ok := parseHeader(header)
if length == 0 && index == -1 && !ok {
h.reportError(ctx, errChan, core.InvalidRequestError{})
return
}
if length > h.Service.MaxRequestLength {
h.sendResponse(ctx, queue, index, nil, core.ErrRequestEntityTooLarge)
return
}
body := make([]byte, length)
if _, err := io.ReadAtLeast(conn, body, length); err != nil {
h.reportError(ctx, errChan, err)
return
}
if h.Pool != nil {
h.Pool.Submit(h.task(core.WithContext(ctx, h.getServiceContext(conn)), queue, index, body))
} else {
go h.run(core.WithContext(ctx, h.getServiceContext(conn)), queue, index, body)
}
}
}
}
func (h *Handler) send(ctx context.Context, conn net.Conn, queue chan data, errChan chan error) {
defer h.catch(ctx, errChan)
for {
select {
case <-ctx.Done():
return
case response := <-queue:
index, body, e := response.Index, response.Body, response.Error
if e != nil {
index |= math.MinInt32
if e == core.ErrRequestEntityTooLarge {
body = convert.ToUnsafeBytes(core.RequestEntityTooLarge)
} else {
body = convert.ToUnsafeBytes(e.Error())
}
}
header := makeHeader(len(body), index)
_, err := conn.Write(header[:])
if err == nil {
_, err = conn.Write(body)
}
if err != nil {
h.reportError(ctx, errChan, err)
return
}
if e != nil {
h.reportError(ctx, errChan, e)
return
}
}
}
}
func (h *Handler) Serve(ctx context.Context, conn net.Conn) {
if conn = h.onAccept(conn); conn == nil {
return
}
ctx, cancel := context.WithCancel(ctx)
var err error
defer func() {
cancel()
if e := recover(); e != nil {
err = core.NewPanicError(e)
}
if err != nil {
h.onError(conn, err)
}
h.onClose(conn)
conn.Close()
}()
queue := make(chan data)
errChan := make(chan error, 1)
go h.receive(ctx, conn, queue, errChan)
go h.send(ctx, conn, queue, errChan)
select {
case <-ctx.Done():
err = ctx.Err()
case err = <-errChan:
}
}
type handlerFactory struct {
serverTypes []reflect.Type
}
func (factory handlerFactory) ServerTypes() []reflect.Type {
return factory.serverTypes
}
func (factory handlerFactory) New(service *core.Service) core.Handler {
return &Handler{
Service: service,
}
}
func RegisterHandler() {
core.RegisterHandler("socket", handlerFactory{
[]reflect.Type{
reflect.TypeOf((*net.TCPListener)(nil)),
reflect.TypeOf((*net.UnixListener)(nil)),
reflect.TypeOf(tls.NewListener(nil, nil)),
},
})
}