-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconn.go
240 lines (213 loc) · 4.41 KB
/
conn.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
package proxy
import (
"context"
"log"
"net"
"sync"
"time"
"golang.org/x/net/ipv4"
)
type Conn interface {
Run(ctx context.Context) error
Endpoint
}
type conn struct {
*connOptions
pconn *ipv4.PacketConn
}
func NewConn(pconn *ipv4.PacketConn, opts ...ConnOption) Conn {
o := getConnOptions(opts...)
if o.rch == nil {
o.rch = make(chan *Msg, o.rlen)
}
if o.wch == nil {
o.wch = make(chan *Msg, o.wlen)
}
if o.bufferpool == nil {
o.bufferpool = NewBufferPool(context.TODO(), o.mtu)
}
return &conn{
connOptions: o,
pconn: pconn,
}
}
func (it *conn) Rx() <-chan *Msg {
return it.rch
}
func (it *conn) Tx() chan<- *Msg {
return it.wch
}
func (it *conn) initMsgs(msgs Msgs) {
for i := range msgs {
msgs[i].Buffers[0] = it.bufferpool.Get().([]byte)
msgs[i].Addr = nil
}
}
func (it *conn) putMsgs(msgs Msgs) {
for i := range msgs {
buffer := msgs[i].Buffers[0]
it.bufferpool.Put(buffer[:cap(buffer)])
msgs[i].Buffers[0] = nil
}
}
func (it *conn) newMsgs() Msgs {
msgs := make(Msgs, it.size, it.size)
for i := range msgs {
msgs[i].Buffers = make([][]byte, 1)
}
return msgs
}
func (it *conn) Run(ctx context.Context) error {
wg := sync.WaitGroup{}
defer wg.Wait()
wg.Add(2)
go func() {
defer wg.Done()
it.RunWriteLoop(ctx)
}()
go func() {
defer wg.Done()
it.RunReadLoop(ctx)
}()
<-ctx.Done()
return ctx.Err()
}
func (it *conn) RunReadLoop(ctx context.Context) error {
msgs := it.newMsgs()
it.initMsgs(msgs)
for {
it.pconn.SetReadDeadline(time.Now().Add(time.Second))
n, err := it.pconn.ReadBatch(msgs, 0)
if err != nil {
if err, ok := err.(net.Error); ok && err.Timeout() {
if ctx.Err() != nil {
return ctx.Err()
}
continue
}
log.Fatalf("conn read loop err: %v\n", err)
}
for i := 0; i < n; i++ {
msg := Msg{Buffer: msgs[i].Buffers[0][:msgs[i].N], Addr: msgs[i].Addr.(*net.UDPAddr)}
select {
case it.rch <- &msg:
case <-ctx.Done():
return ctx.Err()
}
}
it.initMsgs(msgs[:n])
}
}
func (it *conn) batchwch(ctx context.Context, msgs Msgs) (int, error) {
select {
case msg := <-it.wch:
msgs[0].Buffers[0] = msg.Buffer
msgs[0].Addr = msg.Addr
case <-ctx.Done():
return 0, ctx.Err()
}
for i := 1; i < len(msgs); i++ {
select {
case msg := <-it.wch:
msgs[i].Buffers[0] = msg.Buffer
msgs[i].Addr = msg.Addr
case <-ctx.Done():
return i, ctx.Err()
// case <-time.After(time.Second):
// return i, nil
default:
return i, nil
}
}
return len(msgs), nil
}
func (it *conn) RunWriteLoop(ctx context.Context) error {
msgs := it.newMsgs()
for {
n, err := it.batchwch(ctx, msgs)
if err != nil {
return err
}
it.pconn.SetWriteDeadline(time.Now().Add(time.Second))
_, err = it.pconn.WriteBatch(msgs[:n], 0)
// TODO: handle return len here https://man7.org/linux/man-pages/man2/sendmmsg.2.html
if err != nil {
if err, ok := err.(net.Error); ok && err.Timeout() {
if ctx.Err() != nil {
return ctx.Err()
}
continue
}
log.Fatalf("conn write loop err: %v\n", err)
}
it.putMsgs(msgs[:n])
}
}
type ConnOption interface {
apply(*connOptions)
}
type funcConnOption struct {
f func(*connOptions)
}
func (it *funcConnOption) apply(o *connOptions) {
it.f(o)
}
func newFuncConnOption(f func(*connOptions)) ConnOption {
return &funcConnOption{f: f}
}
func getConnOptions(opts ...ConnOption) *connOptions {
o := &connOptions{
mtu: 2048,
size: 32,
rlen: 1024,
wlen: 1024,
}
for _, opt := range opts {
opt.apply(o)
}
return o
}
type connOptions struct {
mtu int
size int
rlen int
wlen int
rch chan *Msg
wch chan *Msg
bufferpool *Pool
}
func ConnMtu(mtu int) ConnOption {
return newFuncConnOption(func(o *connOptions) {
o.mtu = mtu
})
}
func ConnBatchSize(size int) ConnOption {
return newFuncConnOption(func(o *connOptions) {
o.size = size
})
}
func ConnReadQueueLength(rlen int) ConnOption {
return newFuncConnOption(func(o *connOptions) {
o.rlen = rlen
})
}
func ConnWriteQueueLength(wlen int) ConnOption {
return newFuncConnOption(func(o *connOptions) {
o.wlen = wlen
})
}
func ConnReadQueue(rch chan *Msg) ConnOption {
return newFuncConnOption(func(o *connOptions) {
o.rch = rch
})
}
func ConnWriteQueue(wch chan *Msg) ConnOption {
return newFuncConnOption(func(o *connOptions) {
o.wch = wch
})
}
func ConnBufferPool(pool *Pool) ConnOption {
return newFuncConnOption(func(o *connOptions) {
o.bufferpool = pool
})
}