-
Notifications
You must be signed in to change notification settings - Fork 6
/
daemons.go
144 lines (123 loc) · 2.65 KB
/
daemons.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
package prox5
import (
"errors"
"strconv"
"time"
"git.tcp.direct/kayos/common/entropy"
cmap "github.com/orcaman/concurrent-map/v2"
)
type proxyMap struct {
plot cmap.ConcurrentMap[string, *Proxy]
parent *ProxyEngine
}
func (sm proxyMap) add(sock string) (*Proxy, bool) {
sm.plot.SetIfAbsent(sock, &Proxy{
Endpoint: sock,
protocol: newImmutableProto(),
lastValidated: time.UnixMilli(0),
timesValidated: 0,
timesBad: 0,
parent: sm.parent,
lock: stateUnlocked,
})
return sm.plot.Get(sock)
}
func (sm proxyMap) delete(sock string) error {
if _, ok := sm.plot.Get(sock); !ok {
return errors.New("proxy not found")
}
sm.plot.Remove(sock)
return nil
}
func (sm proxyMap) clear() {
sm.plot.Clear()
}
func (p5 *ProxyEngine) recycling() int {
if !p5.recycleMu.TryLock() {
return 0
}
defer p5.recycleMu.Unlock()
switch {
case !p5.GetRecyclingStatus(), p5.proxyMap.plot.Count() < 1:
return 0
default:
select {
case <-p5.recycleTimer.C:
break
default:
return 0
}
}
var count = 0
switch p5.GetRecyclerShuffleStatus() {
case true:
var tuples []cmap.Tuple[string, *Proxy]
for tuple := range p5.proxyMap.plot.IterBuffered() {
tuples = append(tuples, tuple)
}
entropy.GetOptimizedRand().Shuffle(len(tuples), func(i, j int) {
tuples[i], tuples[j] = tuples[j], tuples[i]
})
for _, tuple := range tuples {
p5.Pending.add(tuple.Val)
count++
}
case false:
for tuple := range p5.proxyMap.plot.IterBuffered() {
p5.Pending.add(tuple.Val)
count++
}
}
return count
}
func (p5 *ProxyEngine) jobSpawner() {
p5.pool.Reboot()
p5.dbgPrint(simpleString("job spawner started"))
q := make(chan bool, 1)
go func() {
for {
if !p5.IsRunning() {
q <- true
return
}
// select {
// case <-p5.ctx.Done():
// default:
// }
p5.Pending.RLock()
if p5.Pending.Len() < 1 {
p5.Pending.RUnlock()
count := p5.recycling()
switch {
case count > 0:
buf := strs.Get()
buf.MustWriteString("recycled ")
buf.MustWriteString(strconv.Itoa(count))
buf.MustWriteString(" proxies from our map")
p5.dbgPrint(buf)
default:
time.Sleep(time.Millisecond * 100)
}
continue
} else {
p5.Pending.RUnlock()
}
var sock *Proxy
p5.Pending.Lock()
switch p5.GetRecyclingStatus() {
case true:
el := p5.Pending.Front()
p5.Pending.MoveToBack(el)
sock = el.Value.(*Proxy)
}
p5.Pending.Unlock()
_ = p5.scale()
if err := p5.pool.Submit(sock.validate); err != nil {
p5.dbgPrint(simpleString(err.Error()))
}
}
}()
<-q
p5.dbgPrint(simpleString("job spawner paused"))
p5.pool.Release()
}