2022-08-04 08:04:30 +00:00
|
|
|
# Nimbus
|
2024-06-07 21:39:58 +00:00
|
|
|
# Copyright (c) 2021-2024 Status Research & Development GmbH
|
2022-07-21 12:14:41 +00:00
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0)
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or
|
|
|
|
# http://opensource.org/licenses/MIT)
|
|
|
|
# at your option. This file may not be copied, modified, or distributed
|
|
|
|
# except according to those terms.
|
|
|
|
|
2022-08-04 08:04:30 +00:00
|
|
|
## Worker peers scheduler template
|
|
|
|
## ===============================
|
|
|
|
##
|
|
|
|
## Public descriptors
|
|
|
|
|
2023-04-04 13:36:18 +00:00
|
|
|
{.push raises: [].}
|
|
|
|
|
2022-07-21 12:14:41 +00:00
|
|
|
import
|
2024-10-01 09:19:29 +00:00
|
|
|
eth/p2p
|
2022-07-21 12:14:41 +00:00
|
|
|
|
|
|
|
type
|
2022-08-04 08:04:30 +00:00
|
|
|
BuddyRunState* = enum
|
|
|
|
Running = 0 ## Running, default state
|
|
|
|
Stopped ## Stopped or about stopping
|
|
|
|
ZombieStop ## Abandon/ignore (wait for pushed out of LRU table)
|
|
|
|
ZombieRun ## Extra zombie state to potentially recover from
|
2022-07-21 12:14:41 +00:00
|
|
|
|
2022-09-30 08:22:14 +00:00
|
|
|
BuddyCtrlRef* = ref object
|
2022-07-21 12:14:41 +00:00
|
|
|
## Control and state settings
|
2023-03-02 09:57:58 +00:00
|
|
|
runState: BuddyRunState ## Access with getters
|
2022-07-21 12:14:41 +00:00
|
|
|
|
2022-08-04 08:04:30 +00:00
|
|
|
BuddyRef*[S,W] = ref object
|
|
|
|
## Worker peer state descriptor.
|
2023-03-02 09:57:58 +00:00
|
|
|
ctx*: CtxRef[S] ## Shared data descriptor back reference
|
|
|
|
peer*: Peer ## Reference to eth p2pProtocol entry
|
|
|
|
ctrl*: BuddyCtrlRef ## Control and state settings
|
|
|
|
only*: W ## Worker peer specific data
|
2022-07-21 12:14:41 +00:00
|
|
|
|
2022-08-04 08:04:30 +00:00
|
|
|
CtxRef*[S] = ref object
|
|
|
|
## Shared state among all syncing peer workers (aka buddies.)
|
2024-11-01 19:18:41 +00:00
|
|
|
noisyLog*: bool ## Hold back `trace` and `debug` msgs if `false`
|
2023-03-02 09:57:58 +00:00
|
|
|
poolMode*: bool ## Activate `runPool()` workers if set `true`
|
|
|
|
daemon*: bool ## Enable global background job
|
|
|
|
pool*: S ## Shared context for all worker peers
|
2022-07-21 12:14:41 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2022-08-04 08:04:30 +00:00
|
|
|
proc `$`*[S,W](worker: BuddyRef[S,W]): string =
|
|
|
|
$worker.peer & "$" & $worker.ctrl.runState
|
2022-07-21 12:14:41 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public getters, `BuddyRunState` execution control functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2022-09-30 08:22:14 +00:00
|
|
|
proc state*(ctrl: BuddyCtrlRef): BuddyRunState =
|
2022-07-21 12:14:41 +00:00
|
|
|
## Getter (logging only, details of `BuddyCtrl` are private)
|
|
|
|
ctrl.runState
|
|
|
|
|
2022-09-30 08:22:14 +00:00
|
|
|
proc running*(ctrl: BuddyCtrlRef): bool =
|
2022-07-21 12:14:41 +00:00
|
|
|
## Getter, if `true` if `ctrl.state()` is `Running`
|
|
|
|
ctrl.runState == Running
|
|
|
|
|
2022-09-30 08:22:14 +00:00
|
|
|
proc stopped*(ctrl: BuddyCtrlRef): bool =
|
2022-07-21 12:14:41 +00:00
|
|
|
## Getter, if `true`, if `ctrl.state()` is not `Running`
|
2022-11-08 18:56:04 +00:00
|
|
|
ctrl.runState != Running
|
2022-07-21 12:14:41 +00:00
|
|
|
|
2022-09-30 08:22:14 +00:00
|
|
|
proc zombie*(ctrl: BuddyCtrlRef): bool =
|
2022-07-21 12:14:41 +00:00
|
|
|
## Getter, `true` if `ctrl.state()` is `Zombie` (i.e. not `running()` and
|
|
|
|
## not `stopped()`)
|
|
|
|
ctrl.runState in {ZombieStop, ZombieRun}
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public setters, `BuddyRunState` execution control functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2022-09-30 08:22:14 +00:00
|
|
|
proc `zombie=`*(ctrl: BuddyCtrlRef; value: bool) =
|
2022-07-21 12:14:41 +00:00
|
|
|
## Setter
|
|
|
|
if value:
|
|
|
|
case ctrl.runState:
|
|
|
|
of Running:
|
|
|
|
ctrl.runState = ZombieRun
|
|
|
|
of Stopped:
|
|
|
|
ctrl.runState = ZombieStop
|
|
|
|
else:
|
|
|
|
discard
|
|
|
|
else:
|
|
|
|
case ctrl.runState:
|
|
|
|
of ZombieRun:
|
|
|
|
ctrl.runState = Running
|
|
|
|
of ZombieStop:
|
|
|
|
ctrl.runState = Stopped
|
|
|
|
else:
|
|
|
|
discard
|
|
|
|
|
2022-09-30 08:22:14 +00:00
|
|
|
proc `stopped=`*(ctrl: BuddyCtrlRef; value: bool) =
|
2022-07-21 12:14:41 +00:00
|
|
|
## Setter
|
|
|
|
if value:
|
|
|
|
case ctrl.runState:
|
|
|
|
of Running:
|
|
|
|
ctrl.runState = Stopped
|
|
|
|
else:
|
|
|
|
discard
|
|
|
|
else:
|
|
|
|
case ctrl.runState:
|
|
|
|
of Stopped:
|
|
|
|
ctrl.runState = Running
|
|
|
|
else:
|
|
|
|
discard
|
|
|
|
|
2023-04-04 13:36:18 +00:00
|
|
|
proc `forceRun=`*(ctrl: BuddyCtrlRef; value: bool) =
|
|
|
|
## Setter, gets out of `Zombie` jail/locked state with `true argument.
|
|
|
|
if value:
|
|
|
|
ctrl.runState = Running
|
|
|
|
else:
|
|
|
|
ctrl.stopped = true
|
|
|
|
|
2022-07-21 12:14:41 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|