2022-05-23 01:05:50 +00:00
|
|
|
package prox5
|
2021-09-12 02:59:05 +00:00
|
|
|
|
2022-05-23 01:20:40 +00:00
|
|
|
import (
|
2022-05-30 10:42:18 +00:00
|
|
|
"context"
|
2022-05-23 01:20:40 +00:00
|
|
|
"errors"
|
2022-06-26 02:51:42 +00:00
|
|
|
"sync/atomic"
|
2022-05-23 01:20:40 +00:00
|
|
|
)
|
2021-09-18 03:47:11 +00:00
|
|
|
|
2022-10-16 10:53:04 +00:00
|
|
|
// engineState represents the current state of our ProxyEngine.
|
|
|
|
type engineState uint32
|
2021-09-23 07:38:00 +00:00
|
|
|
|
|
|
|
const (
|
2022-05-23 01:20:40 +00:00
|
|
|
// StateRunning means the proxy pool is currently taking in proxys and validating them, and is available to dispense proxies.
|
2022-10-16 10:53:04 +00:00
|
|
|
StateRunning engineState = iota
|
|
|
|
// StatePaused means the proxy pool has been with ProxyEngine.Pause() and may be resumed with ProxyEngine.Resume()
|
2022-05-23 01:20:40 +00:00
|
|
|
StatePaused
|
|
|
|
// StateNew means the proxy pool has never been started.
|
|
|
|
StateNew
|
2021-09-23 07:38:00 +00:00
|
|
|
)
|
|
|
|
|
2022-10-16 10:53:04 +00:00
|
|
|
// Start starts our proxy pool operations. Trying to start a running ProxyEngine will return an error.
|
|
|
|
func (p5 *ProxyEngine) Start() error {
|
2022-09-22 23:48:08 +00:00
|
|
|
if atomic.LoadUint32(&p5.Status) != uint32(StateNew) {
|
|
|
|
return p5.Resume()
|
2021-09-18 03:47:11 +00:00
|
|
|
}
|
2022-09-22 23:48:08 +00:00
|
|
|
p5.startDaemons()
|
2021-09-20 01:23:18 +00:00
|
|
|
return nil
|
2021-09-14 07:35:04 +00:00
|
|
|
}
|
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-10-16 10:53:04 +00:00
|
|
|
- You will be able to start the proxy pool again with ProxyEngine.Resume(), it will have the same Statistics, options, and ratelimits.
|
2022-08-28 13:12:48 +00:00
|
|
|
- During pause you are still able to dispense proxies.
|
|
|
|
- Options may be changed and proxy lists may be loaded when paused.
|
2022-10-16 10:53:04 +00:00
|
|
|
- Pausing an already paused ProxyEngine is a nonop.
|
2021-09-13 19:18:18 +00:00
|
|
|
*/
|
2022-10-16 10:53:04 +00:00
|
|
|
func (p5 *ProxyEngine) Pause() error {
|
2022-09-22 23:48:08 +00:00
|
|
|
if !p5.IsRunning() {
|
2022-06-26 02:51:42 +00:00
|
|
|
return errors.New("not running")
|
2021-09-13 19:16:26 +00:00
|
|
|
}
|
2021-10-23 14:26:23 +00:00
|
|
|
|
2022-09-22 23:48:08 +00:00
|
|
|
p5.dbgPrint(simpleString("pausing proxy pool"))
|
2021-10-23 14:26:23 +00:00
|
|
|
|
2022-09-22 23:48:08 +00:00
|
|
|
p5.quit()
|
2021-09-23 07:38:00 +00:00
|
|
|
|
2022-09-22 23:48:08 +00:00
|
|
|
atomic.StoreUint32(&p5.Status, uint32(StatePaused))
|
2021-09-20 12:29:28 +00:00
|
|
|
return nil
|
2021-09-13 19:08:41 +00:00
|
|
|
}
|
|
|
|
|
2022-10-16 10:53:04 +00:00
|
|
|
func (p5 *ProxyEngine) startDaemons() {
|
2022-09-22 23:48:08 +00:00
|
|
|
go p5.jobSpawner()
|
2022-09-29 04:26:10 +00:00
|
|
|
atomic.StoreUint32(&p5.Status, uint32(StateRunning))
|
2021-10-23 17:25:28 +00:00
|
|
|
}
|
|
|
|
|
2022-10-16 10:53:04 +00:00
|
|
|
// Resume will resume pause proxy pool operations, attempting to resume a running ProxyEngine is returns an error.
|
|
|
|
func (p5 *ProxyEngine) Resume() error {
|
2022-09-22 23:48:08 +00:00
|
|
|
if p5.IsRunning() {
|
2022-05-30 10:42:18 +00:00
|
|
|
return errors.New("already running")
|
2021-09-13 19:16:26 +00:00
|
|
|
}
|
2022-09-22 23:48:08 +00:00
|
|
|
p5.ctx, p5.quit = context.WithCancel(context.Background())
|
|
|
|
p5.startDaemons()
|
2021-09-18 03:47:11 +00:00
|
|
|
return nil
|
2021-09-13 19:08:41 +00:00
|
|
|
}
|