diff --git a/fs/accounting.go b/fs/accounting.go index 37cb9f71e..f04d2e423 100644 --- a/fs/accounting.go +++ b/fs/accounting.go @@ -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 } diff --git a/fs/accounting_windows.go b/fs/accounting_other.go similarity index 58% rename from fs/accounting_windows.go rename to fs/accounting_other.go index 3aa030901..48fb37411 100644 --- a/fs/accounting_windows.go +++ b/fs/accounting_other.go @@ -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() {} diff --git a/fs/accounting_unix.go b/fs/accounting_unix.go index a14310505..3680fd6a1 100644 --- a/fs/accounting_unix.go +++ b/fs/accounting_unix.go @@ -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) } }() }