Fix non-windows/non-unix builds for bwlimit/SIGUSR2 feature and add a mutex
The race detector complained whenever SIGUSR2 was sent to rclone so this adds a mutex to prevent concurrent access.
This commit is contained in:
parent
5454f2abd0
commit
e65059e431
3 changed files with 22 additions and 31 deletions
|
@ -18,8 +18,8 @@ import (
|
|||
// Globals
|
||||
var (
|
||||
Stats = NewStats()
|
||||
tokenBucketMu sync.Mutex // protects the token bucket variables
|
||||
tokenBucket *tb.Bucket
|
||||
origTokenBucket = tokenBucket
|
||||
prevTokenBucket = tokenBucket
|
||||
)
|
||||
|
||||
|
@ -28,13 +28,11 @@ func startTokenBucket() {
|
|||
if bwLimit > 0 {
|
||||
tokenBucket = tb.NewBucket(int64(bwLimit), 100*time.Millisecond)
|
||||
Log(nil, "Starting bandwidth limiter at %vBytes/s", &bwLimit)
|
||||
}
|
||||
origTokenBucket = tokenBucket
|
||||
prevTokenBucket = tokenBucket
|
||||
|
||||
// Start the SIGUSR2 signal handler to toggle bandwidth.
|
||||
// This function does nothing in windows systems.
|
||||
startSignalHandler()
|
||||
// Start the SIGUSR2 signal handler to toggle bandwidth.
|
||||
// This function does nothing in windows systems.
|
||||
startSignalHandler()
|
||||
}
|
||||
}
|
||||
|
||||
// stringSet holds a set of strings
|
||||
|
@ -341,19 +339,14 @@ func (acc *Account) read(in io.Reader, p []byte) (n int, err error) {
|
|||
|
||||
Stats.Bytes(int64(n))
|
||||
|
||||
// Log bandwidth limiter status change.
|
||||
if tokenBucket != prevTokenBucket {
|
||||
s := "disabled"
|
||||
if tokenBucket != nil {
|
||||
s = "enabled"
|
||||
}
|
||||
Log(nil, "Bandwidth limit %s by user", s)
|
||||
prevTokenBucket = tokenBucket
|
||||
}
|
||||
// Get the token bucket in use
|
||||
tokenBucketMu.Lock()
|
||||
tb := tokenBucket
|
||||
tokenBucketMu.Unlock()
|
||||
|
||||
// Limit the transfer speed if required
|
||||
if tokenBucket != nil {
|
||||
tokenBucket.Wait(int64(n))
|
||||
if tb != nil {
|
||||
tb.Wait(int64(n))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
// Accounting and limiting reader
|
||||
// Windows specific functions.
|
||||
// Non-unix specific functions.
|
||||
|
||||
// +build windows
|
||||
// +build !darwin,!dragonfly,!freebsd,!linux,!netbsd,!openbsd,!solaris
|
||||
|
||||
package fs
|
||||
|
||||
// startSignalHandler() is Unix specific and does nothing under windows
|
||||
// startSignalHandler() is Unix specific and does nothing under non-Unix
|
||||
// platforms.
|
||||
func startSignalHandler() {}
|
|
@ -1,7 +1,7 @@
|
|||
// Accounting and limiting reader
|
||||
// Unix specific functions.
|
||||
|
||||
// +build darwin dragonfly freebsd linux netbsd openbsd
|
||||
// +build darwin dragonfly freebsd linux netbsd openbsd solaris
|
||||
|
||||
package fs
|
||||
|
||||
|
@ -13,11 +13,6 @@ import (
|
|||
|
||||
// startSignalHandler() sets a signal handler to catch SIGUSR2 and toggle throttling.
|
||||
func startSignalHandler() {
|
||||
// Don't do anything if no bandwidth limits requested.
|
||||
if bwLimit <= 0 {
|
||||
return
|
||||
}
|
||||
|
||||
signals := make(chan os.Signal, 1)
|
||||
signal.Notify(signals, syscall.SIGUSR2)
|
||||
|
||||
|
@ -25,11 +20,14 @@ func startSignalHandler() {
|
|||
// This runs forever, but blocks until the signal is received.
|
||||
for {
|
||||
<-signals
|
||||
if tokenBucket == nil {
|
||||
tokenBucket = origTokenBucket
|
||||
} else {
|
||||
tokenBucket = nil
|
||||
tokenBucketMu.Lock()
|
||||
tokenBucket, prevTokenBucket = prevTokenBucket, tokenBucket
|
||||
s := "disabled"
|
||||
if tokenBucket != nil {
|
||||
s = "enabled"
|
||||
}
|
||||
tokenBucketMu.Unlock()
|
||||
Log(nil, "Bandwidth limit %s by user", s)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue