mirror of
https://github.com/safing/portbase
synced 2025-09-04 03:29:59 +00:00
Merge pull request #206 from safing/feature/module-sleep-mode
Add support for module sleep mode
This commit is contained in:
commit
bfb439adeb
7 changed files with 146 additions and 12 deletions
|
@ -121,14 +121,14 @@ func writeMetricsTo(ctx context.Context, url string) error {
|
||||||
|
|
||||||
func metricsWriter(ctx context.Context) error {
|
func metricsWriter(ctx context.Context) error {
|
||||||
pushURL := pushOption()
|
pushURL := pushOption()
|
||||||
ticker := time.NewTicker(1 * time.Minute)
|
ticker := module.NewSleepyTicker(1*time.Minute, 0)
|
||||||
defer ticker.Stop()
|
defer ticker.Stop()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
return nil
|
return nil
|
||||||
case <-ticker.C:
|
case <-ticker.Wait():
|
||||||
err := writeMetricsTo(ctx, pushURL)
|
err := writeMetricsTo(ctx, pushURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -20,6 +20,8 @@ var (
|
||||||
// modulesLocked locks `modules` during starting.
|
// modulesLocked locks `modules` during starting.
|
||||||
modulesLocked = abool.New()
|
modulesLocked = abool.New()
|
||||||
|
|
||||||
|
sleepMode = abool.NewBool(false)
|
||||||
|
|
||||||
moduleStartTimeout = 2 * time.Minute
|
moduleStartTimeout = 2 * time.Minute
|
||||||
moduleStopTimeout = 1 * time.Minute
|
moduleStopTimeout = 1 * time.Minute
|
||||||
|
|
||||||
|
@ -37,6 +39,8 @@ type Module struct {
|
||||||
enabled *abool.AtomicBool
|
enabled *abool.AtomicBool
|
||||||
enabledAsDependency *abool.AtomicBool
|
enabledAsDependency *abool.AtomicBool
|
||||||
status uint8
|
status uint8
|
||||||
|
sleepMode *abool.AtomicBool
|
||||||
|
sleepWaitingChannel chan time.Time
|
||||||
|
|
||||||
// failure status
|
// failure status
|
||||||
failureStatus uint8
|
failureStatus uint8
|
||||||
|
@ -100,6 +104,43 @@ func (m *Module) Dependencies() []*Module {
|
||||||
return m.depModules
|
return m.depModules
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Sleep enables or disables sleep mode.
|
||||||
|
func (m *Module) Sleep(enable bool) {
|
||||||
|
set := m.sleepMode.SetToIf(!enable, enable)
|
||||||
|
if !set {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
m.Lock()
|
||||||
|
defer m.Unlock()
|
||||||
|
|
||||||
|
if enable {
|
||||||
|
m.sleepWaitingChannel = make(chan time.Time)
|
||||||
|
} else {
|
||||||
|
// Notify all waiting tasks that we are not sleeping anymore.
|
||||||
|
close(m.sleepWaitingChannel)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsSleeping returns true if sleep mode is enabled.
|
||||||
|
func (m *Module) IsSleeping() bool {
|
||||||
|
return m.sleepMode.IsSet()
|
||||||
|
}
|
||||||
|
|
||||||
|
// WaitIfSleeping returns channel that will signal when it exits sleep mode.
|
||||||
|
// The channel will always return a zero-value time.Time.
|
||||||
|
// It uses time.Time to be easier dropped in to replace a time.Ticker.
|
||||||
|
func (m *Module) WaitIfSleeping() <-chan time.Time {
|
||||||
|
m.RLock()
|
||||||
|
defer m.RUnlock()
|
||||||
|
return m.sleepWaitingChannel
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewSleepyTicker returns new sleepyTicker that will respect the modules sleep mode.
|
||||||
|
func (m *Module) NewSleepyTicker(normalDuration, sleepDuration time.Duration) *SleepyTicker {
|
||||||
|
return newSleepyTicker(m, normalDuration, sleepDuration)
|
||||||
|
}
|
||||||
|
|
||||||
func (m *Module) prep(reports chan *report) {
|
func (m *Module) prep(reports chan *report) {
|
||||||
// check and set intermediate status
|
// check and set intermediate status
|
||||||
m.Lock()
|
m.Lock()
|
||||||
|
@ -343,6 +384,8 @@ func initNewModule(name string, prep, start, stop func() error, dependencies ...
|
||||||
Name: name,
|
Name: name,
|
||||||
enabled: abool.NewBool(false),
|
enabled: abool.NewBool(false),
|
||||||
enabledAsDependency: abool.NewBool(false),
|
enabledAsDependency: abool.NewBool(false),
|
||||||
|
sleepMode: abool.NewBool(true),
|
||||||
|
sleepWaitingChannel: make(chan time.Time),
|
||||||
prepFn: prep,
|
prepFn: prep,
|
||||||
startFn: start,
|
startFn: start,
|
||||||
stopFn: stop,
|
stopFn: stop,
|
||||||
|
@ -358,6 +401,9 @@ func initNewModule(name string, prep, start, stop func() error, dependencies ...
|
||||||
depNames: dependencies,
|
depNames: dependencies,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Sleep mode is disabled by default
|
||||||
|
newModule.Sleep(false)
|
||||||
|
|
||||||
return newModule
|
return newModule
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -380,3 +426,21 @@ func initDependencies() error {
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetSleepMode enables or disables sleep mode for all the modules.
|
||||||
|
func SetSleepMode(enabled bool) {
|
||||||
|
// Update all modules
|
||||||
|
for _, m := range modules {
|
||||||
|
m.Sleep(enabled)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if differs with the old state.
|
||||||
|
set := sleepMode.SetToIf(!enabled, enabled)
|
||||||
|
if set {
|
||||||
|
// Send signal to the task schedular.
|
||||||
|
select {
|
||||||
|
case notifyTaskScheduler <- struct{}{}:
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
59
modules/sleepyticker.go
Normal file
59
modules/sleepyticker.go
Normal file
|
@ -0,0 +1,59 @@
|
||||||
|
package modules
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
|
// SleepyTicker is wrapper over time.Ticker that respects the sleep mode of the module.
|
||||||
|
type SleepyTicker struct {
|
||||||
|
ticker time.Ticker
|
||||||
|
module *Module
|
||||||
|
normalDuration time.Duration
|
||||||
|
sleepDuration time.Duration
|
||||||
|
sleepMode bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// newSleepyTicker returns a new SleepyTicker. This is a wrapper of the standard time.Ticker but it respects modules.Module sleep mode. Check https://pkg.go.dev/time#Ticker.
|
||||||
|
// If sleepDuration is set to 0 ticker will not tick during sleep.
|
||||||
|
func newSleepyTicker(module *Module, normalDuration time.Duration, sleepDuration time.Duration) *SleepyTicker {
|
||||||
|
st := &SleepyTicker{
|
||||||
|
ticker: *time.NewTicker(normalDuration),
|
||||||
|
module: module,
|
||||||
|
normalDuration: normalDuration,
|
||||||
|
sleepDuration: sleepDuration,
|
||||||
|
sleepMode: false,
|
||||||
|
}
|
||||||
|
|
||||||
|
return st
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wait waits until the module is not in sleep mode and returns time.Ticker.C channel.
|
||||||
|
func (st *SleepyTicker) Wait() <-chan time.Time {
|
||||||
|
sleepModeEnabled := st.module.sleepMode.IsSet()
|
||||||
|
|
||||||
|
// Update Sleep mode
|
||||||
|
if sleepModeEnabled != st.sleepMode {
|
||||||
|
st.enterSleepMode(sleepModeEnabled)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wait if until sleep mode exits only if sleepDuration is set to 0.
|
||||||
|
if sleepModeEnabled && st.sleepDuration == 0 {
|
||||||
|
return st.module.WaitIfSleeping()
|
||||||
|
}
|
||||||
|
|
||||||
|
return st.ticker.C
|
||||||
|
}
|
||||||
|
|
||||||
|
// Stop turns off a ticker. After Stop, no more ticks will be sent. Stop does not close the channel, to prevent a concurrent goroutine reading from the channel from seeing an erroneous "tick".
|
||||||
|
func (st *SleepyTicker) Stop() {
|
||||||
|
st.ticker.Stop()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (st *SleepyTicker) enterSleepMode(enabled bool) {
|
||||||
|
st.sleepMode = enabled
|
||||||
|
if enabled {
|
||||||
|
if st.sleepDuration > 0 {
|
||||||
|
st.ticker.Reset(st.sleepDuration)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
st.ticker.Reset(st.normalDuration)
|
||||||
|
}
|
||||||
|
}
|
|
@ -51,9 +51,9 @@ var (
|
||||||
|
|
||||||
waitForever chan time.Time
|
waitForever chan time.Time
|
||||||
|
|
||||||
queueIsFilled = make(chan struct{}, 1) // kick off queue handler
|
queueIsFilled = make(chan struct{}, 1) // kick off queue handler
|
||||||
recalculateNextScheduledTask = make(chan struct{}, 1)
|
notifyTaskScheduler = make(chan struct{}, 1)
|
||||||
taskTimeslot = make(chan struct{})
|
taskTimeslot = make(chan struct{})
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -410,7 +410,7 @@ func (t *Task) addToSchedule(overtime bool) {
|
||||||
// notify scheduler
|
// notify scheduler
|
||||||
defer func() {
|
defer func() {
|
||||||
select {
|
select {
|
||||||
case recalculateNextScheduledTask <- struct{}{}:
|
case notifyTaskScheduler <- struct{}{}:
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
@ -515,10 +515,21 @@ func taskScheduleHandler() {
|
||||||
}
|
}
|
||||||
|
|
||||||
for {
|
for {
|
||||||
|
|
||||||
|
if sleepMode.IsSet() {
|
||||||
|
select {
|
||||||
|
case <-shutdownSignal:
|
||||||
|
return
|
||||||
|
case <-notifyTaskScheduler:
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case <-shutdownSignal:
|
case <-shutdownSignal:
|
||||||
return
|
return
|
||||||
case <-recalculateNextScheduledTask:
|
case <-notifyTaskScheduler:
|
||||||
|
continue
|
||||||
case <-waitUntilNextScheduledTask():
|
case <-waitUntilNextScheduledTask():
|
||||||
scheduleLock.Lock()
|
scheduleLock.Lock()
|
||||||
|
|
||||||
|
|
|
@ -6,14 +6,14 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func cleaner(ctx context.Context) error { //nolint:unparam // Conforms to worker interface
|
func cleaner(ctx context.Context) error { //nolint:unparam // Conforms to worker interface
|
||||||
ticker := time.NewTicker(1 * time.Second)
|
ticker := module.NewSleepyTicker(1*time.Second, 0)
|
||||||
defer ticker.Stop()
|
defer ticker.Stop()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
return nil
|
return nil
|
||||||
case <-ticker.C:
|
case <-ticker.Wait():
|
||||||
deleteExpiredNotifs()
|
deleteExpiredNotifs()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,7 +23,7 @@ type Flag struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewBroadcastFlag returns a new BroadcastFlag.
|
// NewBroadcastFlag returns a new BroadcastFlag.
|
||||||
// In the initial state, the flag is not set and the singal does not trigger.
|
// In the initial state, the flag is not set and the signal does not trigger.
|
||||||
func NewBroadcastFlag() *BroadcastFlag {
|
func NewBroadcastFlag() *BroadcastFlag {
|
||||||
return &BroadcastFlag{
|
return &BroadcastFlag{
|
||||||
flag: abool.New(),
|
flag: abool.New(),
|
||||||
|
@ -33,7 +33,7 @@ func NewBroadcastFlag() *BroadcastFlag {
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewFlag returns a new Flag that listens to this broadcasting flag.
|
// NewFlag returns a new Flag that listens to this broadcasting flag.
|
||||||
// In the initial state, the flag is set and the singal triggers.
|
// In the initial state, the flag is set and the signal triggers.
|
||||||
// You can call Refresh immediately to get the current state from the
|
// You can call Refresh immediately to get the current state from the
|
||||||
// broadcasting flag.
|
// broadcasting flag.
|
||||||
func (bf *BroadcastFlag) NewFlag() *Flag {
|
func (bf *BroadcastFlag) NewFlag() *Flag {
|
||||||
|
|
|
@ -26,6 +26,6 @@ func (di *Info) AddPlatformInfo(_ context.Context) {
|
||||||
UseCodeSection|AddContentLineBreaks,
|
UseCodeSection|AddContentLineBreaks,
|
||||||
fmt.Sprintf("SDK: %d", info.SDK),
|
fmt.Sprintf("SDK: %d", info.SDK),
|
||||||
fmt.Sprintf("Device: %s %s (%s)", info.Manufacturer, info.Brand, info.Board),
|
fmt.Sprintf("Device: %s %s (%s)", info.Manufacturer, info.Brand, info.Board),
|
||||||
fmt.Sprintf("App: %s: %s(%d) %s", info.ApplicationID, info.VersionName, info.VersionCode, info.BuildType))
|
fmt.Sprintf("App: %s: %s %s", info.ApplicationID, info.VersionName, info.BuildType))
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue