prox5/conductor.go

76 lines
1.9 KiB
Go
Raw Normal View History

2022-05-23 01:05:50 +00:00
package prox5
2021-09-12 02:59:05 +00:00
import (
2022-05-30 10:42:18 +00:00
"context"
"errors"
2022-07-25 06:23:12 +00:00
"strings"
2022-06-26 02:51:42 +00:00
"sync/atomic"
)
2022-06-26 02:51:42 +00:00
// SwampStatus represents the current state of our ProxyEngine.
type SwampStatus uint32
const (
// StateRunning means the proxy pool is currently taking in proxys and validating them, and is available to dispense proxies.
StateRunning SwampStatus = iota
2022-06-26 02:51:42 +00:00
// StatePaused means the proxy pool has been with ProxyEngine.Pause() and may be resumed with Swamp.Resume()
StatePaused
// StateNew means the proxy pool has never been started.
StateNew
)
2022-06-26 02:51:42 +00:00
// Start starts our proxy pool operations. Trying to start a running ProxyEngine will return an error.
func (pe *ProxyEngine) Start() error {
if atomic.LoadUint32(&pe.Status) != uint32(StateNew) {
return pe.Resume()
}
2022-06-26 02:51:42 +00:00
pe.startDaemons()
2021-09-20 01:23:18 +00:00
return nil
}
2021-09-15 05:09:43 +00:00
2021-09-13 19:18:18 +00:00
/*
Pause will cease the creation of any new proxy validation operations.
2022-06-26 02:51:42 +00:00
* You will be able to start the proxy pool again with Swamp.Resume(), it will have the same Statistics, options, and ratelimits.
2021-09-13 19:18:18 +00:00
* During pause you are still able to dispense proxies.
* Options may be changed and proxy lists may be loaded when paused.
2022-06-26 02:51:42 +00:00
* Pausing an already paused ProxyEngine is a nonop.
2021-09-13 19:18:18 +00:00
*/
2022-06-26 02:51:42 +00:00
func (pe *ProxyEngine) Pause() error {
if !pe.IsRunning() {
return errors.New("not running")
}
2022-07-25 06:23:12 +00:00
buf := copABuffer.Get().(*strings.Builder)
buf.WriteString("pausing...")
pe.dbgPrint(buf)
2022-06-26 02:51:42 +00:00
pe.quit()
2022-06-26 02:51:42 +00:00
atomic.StoreUint32(&pe.Status, uint32(StatePaused))
2021-09-20 12:29:28 +00:00
return nil
}
2022-06-26 02:51:42 +00:00
func (pe *ProxyEngine) startDaemons() {
go pe.mapBuilder()
<-pe.conductor
pe.svcUp()
go pe.jobSpawner()
for {
2022-06-26 02:51:42 +00:00
if pe.IsRunning() {
atomic.StoreUint32(&pe.Status, uint32(StateRunning))
break
}
}
}
2022-06-26 02:51:42 +00:00
// Resume will resume pause proxy pool operations, attempting to resume a running ProxyEngine is returns an error.
func (pe *ProxyEngine) Resume() error {
if pe.IsRunning() {
2022-05-30 10:42:18 +00:00
return errors.New("already running")
}
2022-06-26 02:51:42 +00:00
pe.ctx, pe.quit = context.WithCancel(context.Background())
pe.startDaemons()
return nil
}