mirror of
https://github.com/TecharoHQ/anubis.git
synced 2025-08-04 02:08:59 -04:00

* feat(lib/policy/expressions): add system load average to bot expression inputs This lets Anubis dynamically react to system load in order to increase and decrease the required level of scrutiny. High load? More scrutiny required. Low load? Less scrutiny required. * docs: spell system correctly Signed-off-by: Xe Iaso <me@xeiaso.net> * Update metadata check-spelling run (pull_request) for Xe/load-average Signed-off-by: check-spelling-bot <check-spelling-bot@users.noreply.github.com> on-behalf-of: @check-spelling <check-spelling-bot@check-spelling.dev> * fix(default-config): don't enable low load average feature by default Signed-off-by: Xe Iaso <me@xeiaso.net> --------- Signed-off-by: Xe Iaso <me@xeiaso.net> Signed-off-by: check-spelling-bot <check-spelling-bot@users.noreply.github.com> Signed-off-by: Xe Iaso <xe.iaso@techaro.lol>
70 lines
1.1 KiB
Go
70 lines
1.1 KiB
Go
package expressions
|
|
|
|
import (
|
|
"context"
|
|
"log/slog"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/shirou/gopsutil/v4/load"
|
|
)
|
|
|
|
type loadAvg struct {
|
|
lock sync.RWMutex
|
|
data *load.AvgStat
|
|
}
|
|
|
|
func (l *loadAvg) updateThread(ctx context.Context) {
|
|
ticker := time.NewTicker(15 * time.Second)
|
|
defer ticker.Stop()
|
|
|
|
l.update()
|
|
|
|
for {
|
|
select {
|
|
case <-ticker.C:
|
|
l.update()
|
|
case <-ctx.Done():
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
func (l *loadAvg) update() {
|
|
l.lock.Lock()
|
|
defer l.lock.Unlock()
|
|
|
|
var err error
|
|
l.data, err = load.Avg()
|
|
if err != nil {
|
|
slog.Debug("can't get load average", "err", err)
|
|
}
|
|
}
|
|
|
|
var (
|
|
globalLoadAvg *loadAvg
|
|
)
|
|
|
|
func init() {
|
|
globalLoadAvg = &loadAvg{}
|
|
go globalLoadAvg.updateThread(context.Background())
|
|
}
|
|
|
|
func Load1() float64 {
|
|
globalLoadAvg.lock.RLock()
|
|
defer globalLoadAvg.lock.RUnlock()
|
|
return globalLoadAvg.data.Load1
|
|
}
|
|
|
|
func Load5() float64 {
|
|
globalLoadAvg.lock.RLock()
|
|
defer globalLoadAvg.lock.RUnlock()
|
|
return globalLoadAvg.data.Load5
|
|
}
|
|
|
|
func Load15() float64 {
|
|
globalLoadAvg.lock.RLock()
|
|
defer globalLoadAvg.lock.RUnlock()
|
|
return globalLoadAvg.data.Load15
|
|
}
|