github.com/divyam234/rclone@v1.64.1/fs/accounting/accounting_unix.go (about)

     1  // Accounting and limiting reader
     2  // Unix specific functions.
     3  
     4  //go:build darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris
     5  // +build darwin dragonfly freebsd linux netbsd openbsd solaris
     6  
     7  package accounting
     8  
     9  import (
    10  	"os"
    11  	"os/signal"
    12  	"syscall"
    13  
    14  	"github.com/divyam234/rclone/fs"
    15  )
    16  
    17  // startSignalHandler() sets a signal handler to catch SIGUSR2 and toggle throttling.
    18  func (tb *tokenBucket) startSignalHandler() {
    19  	signals := make(chan os.Signal, 1)
    20  	signal.Notify(signals, syscall.SIGUSR2)
    21  
    22  	go func() {
    23  		// This runs forever, but blocks until the signal is received.
    24  		for {
    25  			<-signals
    26  
    27  			func() {
    28  				tb.mu.Lock()
    29  				defer tb.mu.Unlock()
    30  
    31  				// if there's no bandwidth limit configured now, do nothing
    32  				if !tb.currLimit.Bandwidth.IsSet() {
    33  					fs.Debugf(nil, "SIGUSR2 received but no bandwidth limit configured right now, ignoring")
    34  					return
    35  				}
    36  
    37  				tb.toggledOff = !tb.toggledOff
    38  				tb.curr, tb.prev = tb.prev, tb.curr
    39  				s := "disabled"
    40  				if !tb.curr._isOff() {
    41  					s = "enabled"
    42  				}
    43  
    44  				fs.Logf(nil, "Bandwidth limit %s by user", s)
    45  			}()
    46  		}
    47  	}()
    48  }