-
Notifications
You must be signed in to change notification settings - Fork 4
/
worker.go
151 lines (126 loc) · 2.37 KB
/
worker.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
package dnsproxy
import (
"net"
"github.com/Asphaltt/hqu"
"github.com/miekg/dns"
)
type userPacket struct {
data []byte
addr *net.UDPAddr
}
type worker struct {
server *server
withCache bool
recvChan chan *userPacket
sendChan chan *userPacket
resolver iresolver
}
func newWorker(s *server) *worker {
w := &worker{
server: s,
withCache: s.config.WithCache,
recvChan: s.recvChan,
sendChan: s.sendChan,
}
w.resolver = newResolver(w, s.config.UpServers)
go w.run()
return w
}
func (w *worker) run() {
for {
upack, ok := <-w.recvChan
if !ok {
return
}
msg := new(dns.Msg)
err := msg.Unpack(upack.data)
if err != nil {
continue
}
if len(msg.Question) == 0 {
continue
}
// cached resolve
if w.withCache {
_msg, ok := w.resolveCache(msg)
if ok {
w.send(upack, _msg)
continue
}
}
// normally resolve
_msg, err := w.resolver.resolve(msg)
if err == nil {
w.send(upack, _msg)
continue
}
w.send(upack, msg)
}
}
func (w *worker) send(pkt *userPacket, msg *dns.Msg) {
msg.Response = true
msg.Rcode = dns.RcodeSuccess
if len(msg.Answer) == 0 {
msg.Rcode = dns.RcodeNameError
}
msg.RecursionAvailable = true
var err error
if pkt.data, err = msg.Pack(); err != nil {
return
}
w.sendChan <- pkt
}
func (w *worker) resolveCache(msg *dns.Msg) (*dns.Msg, bool) {
r, ok := w.server.cache.Get(getQuetion(msg))
if !ok {
return msg, false
}
msg.Answer = make([]dns.RR, len(r.Msg.Answer))
copy(msg.Answer, r.Msg.Answer)
return msg, len(msg.Answer) > 0
}
func (w *worker) close() {
w.resolver.close()
}
// -- worker pool
type workerPool struct {
server *server
workers *hqu.Stack
min, max int
}
func newWorkerPool(s *server) *workerPool {
p := &workerPool{
server: s,
workers: &hqu.Stack{},
min: s.config.WorkerPoolMin,
max: s.config.WorkerPoolMax,
}
for i := 0; i < s.config.WorkerPoolMin; i++ {
p.workers.Push(newWorker(s))
}
return p
}
func (wp *workerPool) openOne() {
if wp.size() < wp.max {
wp.workers.Push(newWorker(wp.server))
}
}
func (wp *workerPool) closeOne() {
if wp.size() > wp.min {
w, _ := wp.workers.Pop()
if w != nil {
w.(*worker).close()
}
}
}
func (wp *workerPool) size() int {
return wp.workers.Size()
}
func (wp *workerPool) close() {
wp.workers.Range(func(v interface{}) bool {
if v != nil {
v.(*worker).close()
}
return true
})
}