prox5/daemons.go

157 lines
2.4 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"
"sync"
2022-05-30 10:42:18 +00:00
"sync/atomic"
"time"
)
2021-09-20 01:23:18 +00:00
func (s *Swamp) svcUp() {
2022-05-30 10:42:18 +00:00
atomic.AddInt32(&s.runningdaemons, 1)
2021-09-20 01:23:18 +00:00
}
func (s *Swamp) svcDown() {
2022-05-30 10:42:18 +00:00
atomic.AddInt32(&s.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
2021-09-20 08:49:06 +00:00
parent *Swamp
}
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{
2021-09-24 19:07:56 +00:00
Endpoint: sock,
lock: stateUnlocked,
parent: sm.parent,
2021-09-20 08:49:06 +00:00
}
sm.plot[sock].timesValidated.Store(0)
sm.plot[sock].timesBad.Store(0)
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
func (s *Swamp) mapBuilder() {
2022-05-30 10:42:18 +00:00
if s.pool.IsClosed() {
s.pool.Reboot()
}
s.dbgPrint("map builder started")
go func() {
2022-05-30 10:42:18 +00:00
defer s.dbgPrint("map builder paused")
for {
select {
2022-05-30 10:42:18 +00:00
case <-s.ctx.Done():
s.svcDown()
return
case in := <-inChan:
2021-10-25 10:32:59 +00:00
if p, ok := s.swampmap.add(in); !ok {
continue
} else {
s.Pending <- p
}
2021-09-20 01:23:18 +00:00
}
}
}()
s.conductor <- true
2021-09-20 01:23:18 +00:00
}
func (s *Swamp) recycling() int {
if !s.GetRecyclingStatus() {
return 0
}
if len(s.swampmap.plot) < 1 {
return 0
}
2022-05-30 10:42:18 +00:00
var count int
2021-09-23 13:16:23 +00:00
2021-11-24 12:01:11 +00:00
s.swampmap.mu.RLock()
defer s.swampmap.mu.RUnlock()
for _, sock := range s.swampmap.plot {
select {
2022-05-30 10:42:18 +00:00
case <-s.ctx.Done():
return 0
case s.Pending <- sock:
count++
}
}
return count
}
2021-09-20 01:23:18 +00:00
func (s *Swamp) jobSpawner() {
2021-10-25 09:57:38 +00:00
if s.pool.IsClosed() {
s.pool.Reboot()
}
2022-05-30 10:42:18 +00:00
2021-09-20 01:23:18 +00:00
s.dbgPrint("job spawner started")
2021-10-25 09:57:38 +00:00
defer s.dbgPrint("job spawner paused")
q := make(chan bool)
go func() {
for {
select {
2022-05-30 10:42:18 +00:00
case <-s.ctx.Done():
2021-10-25 09:57:38 +00:00
q <- true
2022-05-30 10:42:18 +00:00
s.svcDown()
2021-10-25 09:57:38 +00:00
return
case sock := <-s.Pending:
if err := s.pool.Submit(sock.validate); err != nil {
s.dbgPrint(ylw + err.Error() + rst)
}
default:
2021-11-23 12:08:22 +00:00
time.Sleep(25 * time.Millisecond)
count := s.recycling()
s.dbgPrint(ylw + "recycled " + strconv.Itoa(count) + " proxies from our map" + rst)
}
2021-09-20 01:23:18 +00:00
}
}()
s.svcUp()
2021-10-25 09:57:38 +00:00
<-q
s.pool.Release()
2021-09-20 01:23:18 +00:00
}