2016-10-22 07:21:28 +00:00
|
|
|
// Accounting and limiting reader
|
|
|
|
// Unix specific functions.
|
|
|
|
|
2021-09-09 12:25:25 +00:00
|
|
|
//go:build darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris
|
2016-10-22 07:21:28 +00:00
|
|
|
|
2018-01-12 16:30:54 +00:00
|
|
|
package accounting
|
2016-10-22 07:21:28 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
2018-01-12 16:30:54 +00:00
|
|
|
|
2019-07-28 17:47:38 +00:00
|
|
|
"github.com/rclone/rclone/fs"
|
2016-10-22 07:21:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// startSignalHandler() sets a signal handler to catch SIGUSR2 and toggle throttling.
|
2020-07-04 16:20:54 +00:00
|
|
|
func (tb *tokenBucket) startSignalHandler() {
|
2016-10-22 07:21:28 +00:00
|
|
|
signals := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(signals, syscall.SIGUSR2)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
// This runs forever, but blocks until the signal is received.
|
|
|
|
for {
|
|
|
|
<-signals
|
2023-06-22 16:59:24 +00:00
|
|
|
|
|
|
|
func() {
|
|
|
|
tb.mu.Lock()
|
|
|
|
defer tb.mu.Unlock()
|
|
|
|
|
|
|
|
// if there's no bandwidth limit configured now, do nothing
|
|
|
|
if !tb.currLimit.Bandwidth.IsSet() {
|
|
|
|
fs.Debugf(nil, "SIGUSR2 received but no bandwidth limit configured right now, ignoring")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
tb.toggledOff = !tb.toggledOff
|
|
|
|
tb.curr, tb.prev = tb.prev, tb.curr
|
|
|
|
s := "disabled"
|
|
|
|
if !tb.curr._isOff() {
|
|
|
|
s = "enabled"
|
|
|
|
}
|
|
|
|
|
|
|
|
fs.Logf(nil, "Bandwidth limit %s by user", s)
|
|
|
|
}()
|
2016-10-22 07:21:28 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|