2016-10-22 15:21:28 +08:00
|
|
|
// Accounting and limiting reader
|
|
|
|
// Unix specific functions.
|
|
|
|
|
2021-09-09 20:25:25 +08:00
|
|
|
//go:build darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris
|
2016-10-31 03:20:16 +08:00
|
|
|
// +build darwin dragonfly freebsd linux netbsd openbsd solaris
|
2016-10-22 15:21:28 +08:00
|
|
|
|
2018-01-13 00:30:54 +08:00
|
|
|
package accounting
|
2016-10-22 15:21:28 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
2018-01-13 00:30:54 +08:00
|
|
|
|
2019-07-29 01:47:38 +08:00
|
|
|
"github.com/rclone/rclone/fs"
|
2016-10-22 15:21:28 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// startSignalHandler() sets a signal handler to catch SIGUSR2 and toggle throttling.
|
2020-07-05 00:20:54 +08:00
|
|
|
func (tb *tokenBucket) startSignalHandler() {
|
2016-10-22 15:21:28 +08: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
|
2020-07-05 00:20:54 +08:00
|
|
|
tb.mu.Lock()
|
|
|
|
tb.toggledOff = !tb.toggledOff
|
|
|
|
tb.curr, tb.prev = tb.prev, tb.curr
|
2016-10-31 03:20:16 +08:00
|
|
|
s := "disabled"
|
2020-07-05 00:20:54 +08:00
|
|
|
if !tb.curr._isOff() {
|
2016-10-31 03:20:16 +08:00
|
|
|
s = "enabled"
|
2016-10-22 15:21:28 +08:00
|
|
|
}
|
2020-07-05 00:20:54 +08:00
|
|
|
tb.mu.Unlock()
|
2018-01-13 00:30:54 +08:00
|
|
|
fs.Logf(nil, "Bandwidth limit %s by user", s)
|
2016-10-22 15:21:28 +08:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|