2022-05-23 01:05:50 +00:00
|
|
|
package prox5
|
2021-09-20 01:23:18 +00:00
|
|
|
|
2021-09-20 05:57:42 +00:00
|
|
|
import (
|
2021-09-20 08:49:06 +00:00
|
|
|
"errors"
|
2022-09-03 15:01:35 +00:00
|
|
|
"strconv"
|
|
|
|
"time"
|
2022-12-27 12:47:37 +00:00
|
|
|
|
|
|
|
cmap "github.com/orcaman/concurrent-map/v2"
|
2021-09-20 05:57:42 +00:00
|
|
|
)
|
2021-09-20 01:23:18 +00:00
|
|
|
|
2022-10-16 10:53:04 +00:00
|
|
|
type proxyMap struct {
|
2022-12-27 12:47:37 +00:00
|
|
|
plot cmap.ConcurrentMap[string, *Proxy]
|
2022-10-16 10:53:04 +00:00
|
|
|
parent *ProxyEngine
|
2021-09-20 07:05:21 +00:00
|
|
|
}
|
|
|
|
|
2022-10-16 10:53:04 +00:00
|
|
|
func (sm proxyMap) add(sock string) (*Proxy, bool) {
|
2022-12-27 12:47:37 +00:00
|
|
|
sm.plot.SetIfAbsent(sock, &Proxy{
|
2022-06-28 02:27:52 +00:00
|
|
|
Endpoint: sock,
|
2022-09-22 23:24:35 +00:00
|
|
|
protocol: newImmutableProto(),
|
2022-06-28 02:27:52 +00:00
|
|
|
lastValidated: time.UnixMilli(0),
|
|
|
|
timesValidated: 0,
|
|
|
|
timesBad: 0,
|
|
|
|
parent: sm.parent,
|
|
|
|
lock: stateUnlocked,
|
2022-12-27 12:47:37 +00:00
|
|
|
})
|
2021-10-23 14:33:06 +00:00
|
|
|
|
2022-12-27 12:47:37 +00:00
|
|
|
return sm.plot.Get(sock)
|
2021-09-20 07:05:21 +00:00
|
|
|
}
|
|
|
|
|
2022-10-16 10:53:04 +00:00
|
|
|
func (sm proxyMap) delete(sock string) error {
|
2022-12-27 12:47:37 +00:00
|
|
|
if _, ok := sm.plot.Get(sock); !ok {
|
|
|
|
return errors.New("proxy not found")
|
2021-09-20 08:49:06 +00:00
|
|
|
}
|
2022-12-27 12:47:37 +00:00
|
|
|
sm.plot.Remove(sock)
|
2021-09-20 08:49:06 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-16 10:53:04 +00:00
|
|
|
func (sm proxyMap) clear() {
|
2022-12-27 12:47:37 +00:00
|
|
|
sm.plot.Clear()
|
2021-09-20 07:05:21 +00:00
|
|
|
}
|
2021-09-20 01:23:18 +00:00
|
|
|
|
2022-10-16 10:53:04 +00:00
|
|
|
func (p5 *ProxyEngine) recycling() int {
|
2022-12-27 12:55:03 +00:00
|
|
|
switch {
|
|
|
|
case !p5.GetRecyclingStatus(), p5.proxyMap.plot.Count() < 1:
|
2021-09-23 07:38:00 +00:00
|
|
|
return 0
|
2022-12-27 12:55:03 +00:00
|
|
|
default:
|
2021-09-23 07:38:00 +00:00
|
|
|
}
|
2021-10-23 14:58:03 +00:00
|
|
|
|
2022-12-27 12:55:03 +00:00
|
|
|
var count = 0
|
|
|
|
var printedFull = false
|
2021-11-24 12:01:11 +00:00
|
|
|
|
2022-12-27 12:47:37 +00:00
|
|
|
for tuple := range p5.proxyMap.plot.IterBuffered() {
|
2021-09-23 07:38:00 +00:00
|
|
|
select {
|
2022-09-22 23:45:15 +00:00
|
|
|
case <-p5.ctx.Done():
|
2022-05-30 10:42:18 +00:00
|
|
|
return 0
|
2022-12-27 12:47:37 +00:00
|
|
|
case p5.Pending <- tuple.Val:
|
2021-09-23 07:38:00 +00:00
|
|
|
count++
|
2022-09-03 15:01:35 +00:00
|
|
|
default:
|
2022-12-27 12:47:37 +00:00
|
|
|
if !printedFull {
|
|
|
|
p5.DebugLogger.Print("recycling channel is full!")
|
|
|
|
printedFull = true
|
|
|
|
}
|
2022-08-28 16:37:40 +00:00
|
|
|
continue
|
2021-09-23 07:38:00 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-23 14:58:03 +00:00
|
|
|
|
2021-09-23 07:38:00 +00:00
|
|
|
return count
|
|
|
|
}
|
|
|
|
|
2022-10-16 10:53:04 +00:00
|
|
|
func (p5 *ProxyEngine) jobSpawner() {
|
2022-09-22 23:45:15 +00:00
|
|
|
if p5.pool.IsClosed() {
|
|
|
|
p5.pool.Reboot()
|
2021-10-25 09:57:38 +00:00
|
|
|
}
|
2022-05-30 10:42:18 +00:00
|
|
|
|
2022-09-22 23:45:15 +00:00
|
|
|
p5.dbgPrint(simpleString("job spawner started"))
|
|
|
|
defer p5.dbgPrint(simpleString("job spawner paused"))
|
2021-10-25 09:57:38 +00:00
|
|
|
|
|
|
|
q := make(chan bool)
|
2021-10-23 17:25:28 +00:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
2022-09-22 23:45:15 +00:00
|
|
|
case <-p5.ctx.Done():
|
2021-10-25 09:57:38 +00:00
|
|
|
q <- true
|
|
|
|
return
|
2022-09-22 23:45:15 +00:00
|
|
|
case sock := <-p5.Pending:
|
2022-10-16 12:56:40 +00:00
|
|
|
p5.scale()
|
2022-09-22 23:45:15 +00:00
|
|
|
if err := p5.pool.Submit(sock.validate); err != nil {
|
|
|
|
p5.dbgPrint(simpleString(err.Error()))
|
2021-10-23 17:25:28 +00:00
|
|
|
}
|
2022-09-03 15:01:35 +00:00
|
|
|
|
|
|
|
default:
|
2022-09-23 22:57:46 +00:00
|
|
|
time.Sleep(500 * time.Millisecond)
|
2022-09-22 23:45:15 +00:00
|
|
|
count := p5.recycling()
|
2022-10-16 06:45:19 +00:00
|
|
|
buf := strs.Get()
|
|
|
|
buf.MustWriteString("recycled ")
|
|
|
|
buf.MustWriteString(strconv.Itoa(count))
|
|
|
|
buf.MustWriteString(" proxies from our map")
|
2022-09-22 23:45:15 +00:00
|
|
|
p5.dbgPrint(buf)
|
2021-09-23 07:38:00 +00:00
|
|
|
}
|
2021-09-20 01:23:18 +00:00
|
|
|
}
|
2021-10-23 17:25:28 +00:00
|
|
|
}()
|
|
|
|
|
2021-10-25 09:57:38 +00:00
|
|
|
<-q
|
2022-09-22 23:45:15 +00:00
|
|
|
p5.pool.Release()
|
2021-09-20 01:23:18 +00:00
|
|
|
}
|