-
Notifications
You must be signed in to change notification settings - Fork 0
/
anet.go
67 lines (57 loc) · 1.3 KB
/
anet.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
package anet
import (
"context"
"net"
"strings"
"time"
"github.com/google/uuid"
)
func CreateListener(network, addr string) (net.Listener, error) {
return net.Listen(network, addr)
}
func NewEventLoop(onRequest OnRequest, ops ...Option) (EventLoop, error) {
opts := &options{
onRequest: onRequest,
}
for _, do := range ops {
do.f(opts)
}
return &eventLoop{
id: uuid.New().String()[:8],
opts: opts,
}, nil
}
type EventLoop interface {
Serve(ln net.Listener) error
Shutdown(ctx context.Context) error
}
type OnRequest func(ctx context.Context, connection Connection) error
type eventLoop struct {
id string
opts *options
ln net.Listener
}
func (evl *eventLoop) Serve(ln net.Listener) error {
evl.ln = ln
for {
conn, err := evl.ln.Accept()
if err != nil {
if strings.Contains(err.Error(), "closed") {
log.Warnf("[eventloop %s] eventloop quit since listener closed", evl.id)
return nil
}
log.Warnf("[evetloop %s] listener accepted with error, wait 10 ms for retry", evl.id)
time.Sleep(10 * time.Millisecond)
continue
}
go evl.onAccept(conn)
}
}
func (evl *eventLoop) onAccept(conn net.Conn) {
connection := &connection{}
connection.init(conn, evl.opts)
connection.run()
}
func (evl *eventLoop) Shutdown(_ context.Context) error {
return evl.ln.Close()
}