1
0
mirror of https://github.com/rclone/rclone.git synced 2025-01-29 21:04:23 +02:00

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:
Nick Craig-Wood 2016-10-30 19:20:16 +00:00
parent 5454f2abd0
commit e65059e431
3 changed files with 22 additions and 31 deletions

View File

@ -18,8 +18,8 @@ import (
// Globals // Globals
var ( var (
Stats = NewStats() Stats = NewStats()
tokenBucketMu sync.Mutex // protects the token bucket variables
tokenBucket *tb.Bucket tokenBucket *tb.Bucket
origTokenBucket = tokenBucket
prevTokenBucket = tokenBucket prevTokenBucket = tokenBucket
) )
@ -28,13 +28,11 @@ func startTokenBucket() {
if bwLimit > 0 { if bwLimit > 0 {
tokenBucket = tb.NewBucket(int64(bwLimit), 100*time.Millisecond) tokenBucket = tb.NewBucket(int64(bwLimit), 100*time.Millisecond)
Log(nil, "Starting bandwidth limiter at %vBytes/s", &bwLimit) Log(nil, "Starting bandwidth limiter at %vBytes/s", &bwLimit)
}
origTokenBucket = tokenBucket
prevTokenBucket = tokenBucket
// Start the SIGUSR2 signal handler to toggle bandwidth. // Start the SIGUSR2 signal handler to toggle bandwidth.
// This function does nothing in windows systems. // This function does nothing in windows systems.
startSignalHandler() startSignalHandler()
}
} }
// stringSet holds a set of strings // 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)) Stats.Bytes(int64(n))
// Log bandwidth limiter status change. // Get the token bucket in use
if tokenBucket != prevTokenBucket { tokenBucketMu.Lock()
s := "disabled" tb := tokenBucket
if tokenBucket != nil { tokenBucketMu.Unlock()
s = "enabled"
}
Log(nil, "Bandwidth limit %s by user", s)
prevTokenBucket = tokenBucket
}
// Limit the transfer speed if required // Limit the transfer speed if required
if tokenBucket != nil { if tb != nil {
tokenBucket.Wait(int64(n)) tb.Wait(int64(n))
} }
return return
} }

View File

@ -1,10 +1,10 @@
// Accounting and limiting reader // Accounting and limiting reader
// Windows specific functions. // Non-unix specific functions.
// +build windows // +build !darwin,!dragonfly,!freebsd,!linux,!netbsd,!openbsd,!solaris
package fs package fs
// startSignalHandler() is Unix specific and does nothing under windows // startSignalHandler() is Unix specific and does nothing under non-Unix
// platforms. // platforms.
func startSignalHandler() {} func startSignalHandler() {}

View File

@ -1,7 +1,7 @@
// Accounting and limiting reader // Accounting and limiting reader
// Unix specific functions. // Unix specific functions.
// +build darwin dragonfly freebsd linux netbsd openbsd // +build darwin dragonfly freebsd linux netbsd openbsd solaris
package fs package fs
@ -13,11 +13,6 @@ import (
// startSignalHandler() sets a signal handler to catch SIGUSR2 and toggle throttling. // startSignalHandler() sets a signal handler to catch SIGUSR2 and toggle throttling.
func startSignalHandler() { func startSignalHandler() {
// Don't do anything if no bandwidth limits requested.
if bwLimit <= 0 {
return
}
signals := make(chan os.Signal, 1) signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGUSR2) signal.Notify(signals, syscall.SIGUSR2)
@ -25,11 +20,14 @@ func startSignalHandler() {
// This runs forever, but blocks until the signal is received. // This runs forever, but blocks until the signal is received.
for { for {
<-signals <-signals
if tokenBucket == nil { tokenBucketMu.Lock()
tokenBucket = origTokenBucket tokenBucket, prevTokenBucket = prevTokenBucket, tokenBucket
} else { s := "disabled"
tokenBucket = nil if tokenBucket != nil {
s = "enabled"
} }
tokenBucketMu.Unlock()
Log(nil, "Bandwidth limit %s by user", s)
} }
}() }()
} }