prox5/daemons.go

170 lines
2.8 KiB
Go
Raw Normal View History

2022-05-23 01:05:50 +00:00
package prox5
2021-09-20 01:23:18 +00:00
import (
2021-09-20 08:49:06 +00:00
"errors"
"strconv"
2022-07-25 06:23:12 +00:00
"strings"
"sync"
2022-05-30 10:42:18 +00:00
"sync/atomic"
"time"
"git.tcp.direct/kayos/prox5/internal/pools"
)
2021-09-20 01:23:18 +00:00
2022-06-26 02:51:42 +00:00
func (pe *ProxyEngine) svcUp() {
atomic.AddInt32(&pe.runningdaemons, 1)
2021-09-20 01:23:18 +00:00
}
2022-06-26 02:51:42 +00:00
func (pe *ProxyEngine) svcDown() {
atomic.AddInt32(&pe.runningdaemons, -1)
2021-09-20 01:23:18 +00:00
}
type swampMap struct {
2021-09-20 08:49:06 +00:00
plot map[string]*Proxy
2021-09-23 13:16:23 +00:00
mu *sync.RWMutex
2022-06-26 02:51:42 +00:00
parent *ProxyEngine
}
func (sm swampMap) add(sock string) (*Proxy, bool) {
sm.mu.Lock()
defer sm.mu.Unlock()
if sm.exists(sock) {
return nil, false
}
sm.plot[sock] = &Proxy{
2022-06-28 02:27:52 +00:00
Endpoint: sock,
proto: protoNULL,
lastValidated: time.UnixMilli(0),
timesValidated: 0,
timesBad: 0,
parent: sm.parent,
lock: stateUnlocked,
2021-09-20 08:49:06 +00:00
}
return sm.plot[sock], true
}
func (sm swampMap) exists(sock string) bool {
if _, ok := sm.plot[sock]; !ok {
return false
}
return true
}
2021-09-20 08:49:06 +00:00
func (sm swampMap) delete(sock string) error {
sm.mu.Lock()
defer sm.mu.Unlock()
2021-09-20 08:49:06 +00:00
if !sm.exists(sock) {
return errors.New("proxy does not exist in map")
}
sm.plot[sock] = nil
2021-09-20 08:49:06 +00:00
delete(sm.plot, sock)
return nil
}
func (sm swampMap) clear() {
sm.mu.Lock()
defer sm.mu.Unlock()
2022-05-30 10:42:18 +00:00
for key := range sm.plot {
delete(sm.plot, key)
}
}
2021-09-20 01:23:18 +00:00
2022-06-26 02:51:42 +00:00
func (pe *ProxyEngine) mapBuilder() {
if pe.pool.IsClosed() {
pe.pool.Reboot()
2022-05-30 10:42:18 +00:00
}
2022-07-25 06:23:12 +00:00
pe.dbgPrint(simpleString("map builder started"))
go func() {
2022-07-25 06:23:12 +00:00
defer pe.dbgPrint(simpleString("map builder paused"))
for {
select {
2022-06-26 02:51:42 +00:00
case <-pe.ctx.Done():
pe.svcDown()
2022-05-30 10:42:18 +00:00
return
case in := <-inChan:
2022-06-26 02:51:42 +00:00
if p, ok := pe.swampmap.add(in); !ok {
continue
} else {
2022-06-26 02:51:42 +00:00
pe.Pending <- p
}
2022-08-28 16:37:40 +00:00
default:
pe.recycling()
2021-09-20 01:23:18 +00:00
}
}
}()
2022-06-26 02:51:42 +00:00
pe.conductor <- true
2021-09-20 01:23:18 +00:00
}
2022-06-26 02:51:42 +00:00
func (pe *ProxyEngine) recycling() int {
if !pe.GetRecyclingStatus() {
return 0
}
2022-06-26 02:51:42 +00:00
if len(pe.swampmap.plot) < 1 {
return 0
}
2022-05-30 10:42:18 +00:00
var count int
2021-09-23 13:16:23 +00:00
2022-06-26 02:51:42 +00:00
pe.swampmap.mu.RLock()
defer pe.swampmap.mu.RUnlock()
2021-11-24 12:01:11 +00:00
2022-06-26 02:51:42 +00:00
for _, sock := range pe.swampmap.plot {
select {
2022-06-26 02:51:42 +00:00
case <-pe.ctx.Done():
2022-05-30 10:42:18 +00:00
return 0
2022-06-26 02:51:42 +00:00
case pe.Pending <- sock:
count++
2022-08-28 16:37:40 +00:00
default:
continue
}
}
return count
}
2022-06-26 02:51:42 +00:00
func (pe *ProxyEngine) jobSpawner() {
if pe.pool.IsClosed() {
pe.pool.Reboot()
2021-10-25 09:57:38 +00:00
}
2022-05-30 10:42:18 +00:00
2022-07-25 06:23:12 +00:00
pe.dbgPrint(simpleString("job spawner started"))
defer pe.dbgPrint(simpleString("job spawner paused"))
2021-10-25 09:57:38 +00:00
q := make(chan bool)
go func() {
for {
select {
2022-06-26 02:51:42 +00:00
case <-pe.ctx.Done():
2021-10-25 09:57:38 +00:00
q <- true
2022-06-26 02:51:42 +00:00
pe.svcDown()
2021-10-25 09:57:38 +00:00
return
2022-06-26 02:51:42 +00:00
case sock := <-pe.Pending:
if err := pe.pool.Submit(sock.validate); err != nil {
2022-07-25 06:23:12 +00:00
pe.dbgPrint(simpleString(err.Error()))
}
default:
2021-11-23 12:08:22 +00:00
time.Sleep(25 * time.Millisecond)
2022-06-26 02:51:42 +00:00
count := pe.recycling()
buf := pools.CopABuffer.Get().(*strings.Builder)
2022-07-25 06:23:12 +00:00
buf.WriteString("recycled ")
buf.WriteString(strconv.Itoa(count))
buf.WriteString(" proxies from our map")
pe.dbgPrint(buf)
}
2021-09-20 01:23:18 +00:00
}
}()
2022-06-26 02:51:42 +00:00
pe.svcUp()
2021-10-25 09:57:38 +00:00
<-q
2022-06-26 02:51:42 +00:00
pe.pool.Release()
2021-09-20 01:23:18 +00:00
}