From 4fadba9bcb2f9c03f82f98141ad7ea7fae131a98 Mon Sep 17 00:00:00 2001 From: albertony <12441419+albertony@users.noreply.github.com> Date: Sat, 18 Jan 2025 00:45:10 +0100 Subject: [PATCH] config: make parsing of duration value of global flags consistent (more) --- cmd/bisync/cmd.go | 4 +-- cmd/bisync/lockfile.go | 10 +++---- cmd/test/changenotify/changenotify.go | 6 ++-- cmd/test/info/info.go | 6 ++-- fs/rc/jobs/job.go | 6 ++-- fs/rc/jobs/job_test.go | 6 ++-- fs/rc/rc.go | 8 +++--- lib/http/server.go | 40 +++++++++++++-------------- 8 files changed, 43 insertions(+), 43 deletions(-) diff --git a/cmd/bisync/cmd.go b/cmd/bisync/cmd.go index c8b2136c4..8ec2d393a 100644 --- a/cmd/bisync/cmd.go +++ b/cmd/bisync/cmd.go @@ -55,7 +55,7 @@ type Options struct { Compare CompareOpt CompareFlag string DebugName string - MaxLock time.Duration + MaxLock fs.Duration ConflictResolve Prefer ConflictLoser ConflictLoserAction ConflictSuffixFlag string @@ -146,7 +146,7 @@ func init() { flags.BoolVarP(cmdFlags, &Opt.Compare.NoSlowHash, "no-slow-hash", "", Opt.Compare.NoSlowHash, "Ignore listing checksums only on backends where they are slow", "") flags.BoolVarP(cmdFlags, &Opt.Compare.SlowHashSyncOnly, "slow-hash-sync-only", "", Opt.Compare.SlowHashSyncOnly, "Ignore slow checksums for listings and deltas, but still consider them during sync calls.", "") flags.BoolVarP(cmdFlags, &Opt.Compare.DownloadHash, "download-hash", "", Opt.Compare.DownloadHash, "Compute hash by downloading when otherwise unavailable. (warning: may be slow and use lots of data!)", "") - flags.DurationVarP(cmdFlags, &Opt.MaxLock, "max-lock", "", Opt.MaxLock, "Consider lock files older than this to be expired (default: 0 (never expire)) (minimum: 2m)", "") + flags.FVarP(cmdFlags, &Opt.MaxLock, "max-lock", "", "Consider lock files older than this to be expired (default: 0 (never expire)) (minimum: 2m)", "") flags.FVarP(cmdFlags, &Opt.ConflictResolve, "conflict-resolve", "", "Automatically resolve conflicts by preferring the version that is: "+ConflictResolveList+" (default: none)", "") flags.FVarP(cmdFlags, &Opt.ConflictLoser, "conflict-loser", "", "Action to take on the loser of a sync conflict (when there is a winner) or on both files (when there is no winner): "+ConflictLoserList+" (default: num)", "") flags.StringVarP(cmdFlags, &Opt.ConflictSuffixFlag, "conflict-suffix", "", Opt.ConflictSuffixFlag, "Suffix to use when renaming a --conflict-loser. Can be either one string or two comma-separated strings to assign different suffixes to Path1/Path2. (default: 'conflict')", "") diff --git a/cmd/bisync/lockfile.go b/cmd/bisync/lockfile.go index 876b9cde6..90e8c2166 100644 --- a/cmd/bisync/lockfile.go +++ b/cmd/bisync/lockfile.go @@ -14,7 +14,7 @@ import ( "github.com/rclone/rclone/lib/terminal" ) -const basicallyforever = 200 * 365 * 24 * time.Hour +const basicallyforever = fs.Duration(200 * 365 * 24 * time.Hour) var stopRenewal func() @@ -66,9 +66,9 @@ func (b *bisyncRun) removeLockFile() { } func (b *bisyncRun) setLockFileExpiration() { - if b.opt.MaxLock > 0 && b.opt.MaxLock < 2*time.Minute { + if b.opt.MaxLock > 0 && b.opt.MaxLock < fs.Duration(2*time.Minute) { fs.Logf(nil, Color(terminal.YellowFg, "--max-lock cannot be shorter than 2 minutes (unless 0.) Changing --max-lock from %v to %v"), b.opt.MaxLock, 2*time.Minute) - b.opt.MaxLock = 2 * time.Minute + b.opt.MaxLock = fs.Duration(2 * time.Minute) } else if b.opt.MaxLock <= 0 { b.opt.MaxLock = basicallyforever } @@ -80,7 +80,7 @@ func (b *bisyncRun) renewLockFile() { data.Session = b.basePath data.PID = strconv.Itoa(os.Getpid()) data.TimeRenewed = time.Now() - data.TimeExpires = time.Now().Add(b.opt.MaxLock) + data.TimeExpires = time.Now().Add(time.Duration(b.opt.MaxLock)) // save data file df, err := os.Create(b.lockFile) @@ -131,7 +131,7 @@ func (b *bisyncRun) startLockRenewal() func() { wg.Add(1) go func() { defer wg.Done() - ticker := time.NewTicker(b.opt.MaxLock - time.Minute) + ticker := time.NewTicker(time.Duration(b.opt.MaxLock) - time.Minute) for { select { case <-ticker.C: diff --git a/cmd/test/changenotify/changenotify.go b/cmd/test/changenotify/changenotify.go index 27d17ba30..2afb5165d 100644 --- a/cmd/test/changenotify/changenotify.go +++ b/cmd/test/changenotify/changenotify.go @@ -14,13 +14,13 @@ import ( ) var ( - pollInterval = 10 * time.Second + pollInterval = fs.Duration(10 * time.Second) ) func init() { test.Command.AddCommand(commandDefinition) cmdFlags := commandDefinition.Flags() - flags.DurationVarP(cmdFlags, &pollInterval, "poll-interval", "", pollInterval, "Time to wait between polling for changes", "") + flags.FVarP(cmdFlags, &pollInterval, "poll-interval", "", "Time to wait between polling for changes", "") } var commandDefinition = &cobra.Command{ @@ -39,7 +39,7 @@ var commandDefinition = &cobra.Command{ if do := features.ChangeNotify; do != nil { pollChan := make(chan time.Duration) do(ctx, changeNotify, pollChan) - pollChan <- pollInterval + pollChan <- time.Duration(pollInterval) fs.Logf(nil, "Waiting for changes, polling every %v", pollInterval) } else { return errors.New("poll-interval is not supported by this remote") diff --git a/cmd/test/info/info.go b/cmd/test/info/info.go index 83bcdfee8..2bfeb0b26 100644 --- a/cmd/test/info/info.go +++ b/cmd/test/info/info.go @@ -40,7 +40,7 @@ var ( checkStreaming bool checkBase32768 bool all bool - uploadWait time.Duration + uploadWait fs.Duration positionLeftRe = regexp.MustCompile(`(?s)^(.*)-position-left-([[:xdigit:]]+)$`) positionMiddleRe = regexp.MustCompile(`(?s)^position-middle-([[:xdigit:]]+)-(.*)-$`) positionRightRe = regexp.MustCompile(`(?s)^position-right-([[:xdigit:]]+)-(.*)$`) @@ -52,7 +52,7 @@ func init() { flags.StringVarP(cmdFlags, &writeJSON, "write-json", "", "", "Write results to file", "") flags.BoolVarP(cmdFlags, &checkNormalization, "check-normalization", "", false, "Check UTF-8 Normalization", "") flags.BoolVarP(cmdFlags, &checkControl, "check-control", "", false, "Check control characters", "") - flags.DurationVarP(cmdFlags, &uploadWait, "upload-wait", "", 0, "Wait after writing a file", "") + flags.FVarP(cmdFlags, &uploadWait, "upload-wait", "", "Wait after writing a file", "") flags.BoolVarP(cmdFlags, &checkLength, "check-length", "", false, "Check max filename length", "") flags.BoolVarP(cmdFlags, &checkStreaming, "check-streaming", "", false, "Check uploads with indeterminate file size", "") flags.BoolVarP(cmdFlags, &checkBase32768, "check-base32768", "", false, "Check can store all possible base32768 characters", "") @@ -204,7 +204,7 @@ func (r *results) writeFile(path string) (fs.Object, error) { src := object.NewStaticObjectInfo(path, time.Now(), int64(len(contents)), true, nil, r.f) obj, err := r.f.Put(r.ctx, bytes.NewBufferString(contents), src) if uploadWait > 0 { - time.Sleep(uploadWait) + time.Sleep(time.Duration(uploadWait)) } return obj, err } diff --git a/fs/rc/jobs/job.go b/fs/rc/jobs/job.go index ceca2cf78..0153acc5f 100644 --- a/fs/rc/jobs/job.go +++ b/fs/rc/jobs/job.go @@ -147,7 +147,7 @@ func (jobs *Jobs) kickExpire() { jobs.mu.Lock() defer jobs.mu.Unlock() if !jobs.expireRunning { - time.AfterFunc(jobs.opt.JobExpireInterval, jobs.Expire) + time.AfterFunc(time.Duration(jobs.opt.JobExpireInterval), jobs.Expire) jobs.expireRunning = true } } @@ -159,13 +159,13 @@ func (jobs *Jobs) Expire() { now := time.Now() for ID, job := range jobs.jobs { job.mu.Lock() - if job.Finished && now.Sub(job.EndTime) > jobs.opt.JobExpireDuration { + if job.Finished && now.Sub(job.EndTime) > time.Duration(jobs.opt.JobExpireDuration) { delete(jobs.jobs, ID) } job.mu.Unlock() } if len(jobs.jobs) != 0 { - time.AfterFunc(jobs.opt.JobExpireInterval, jobs.Expire) + time.AfterFunc(time.Duration(jobs.opt.JobExpireInterval), jobs.Expire) jobs.expireRunning = true } else { jobs.expireRunning = false diff --git a/fs/rc/jobs/job_test.go b/fs/rc/jobs/job_test.go index ad5744f3a..dcce3879b 100644 --- a/fs/rc/jobs/job_test.go +++ b/fs/rc/jobs/job_test.go @@ -24,7 +24,7 @@ func TestNewJobs(t *testing.T) { func TestJobsKickExpire(t *testing.T) { testy.SkipUnreliable(t) jobs := newJobs() - jobs.opt.JobExpireInterval = time.Millisecond + jobs.opt.JobExpireInterval = fs.Duration(time.Millisecond) assert.Equal(t, false, jobs.expireRunning) jobs.kickExpire() jobs.mu.Lock() @@ -41,7 +41,7 @@ func TestJobsExpire(t *testing.T) { ctx := context.Background() wait := make(chan struct{}) jobs := newJobs() - jobs.opt.JobExpireInterval = time.Millisecond + jobs.opt.JobExpireInterval = fs.Duration(time.Millisecond) assert.Equal(t, false, jobs.expireRunning) var gotJobID int64 var gotJob *Job @@ -64,7 +64,7 @@ func TestJobsExpire(t *testing.T) { assert.Equal(t, 1, len(jobs.jobs)) jobs.mu.Lock() job.mu.Lock() - job.EndTime = time.Now().Add(-rc.Opt.JobExpireDuration - 60*time.Second) + job.EndTime = time.Now().Add(-time.Duration(rc.Opt.JobExpireDuration) - 60*time.Second) assert.Equal(t, true, jobs.expireRunning) job.mu.Unlock() jobs.mu.Unlock() diff --git a/fs/rc/rc.go b/fs/rc/rc.go index e41ac9546..0442bd50d 100644 --- a/fs/rc/rc.go +++ b/fs/rc/rc.go @@ -75,12 +75,12 @@ var OptionsInfo = fs.Options{{ Groups: "RC,Metrics", }, { Name: "rc_job_expire_duration", - Default: 60 * time.Second, + Default: fs.Duration(60 * time.Second), Help: "Expire finished async jobs older than this value", Groups: "RC", }, { Name: "rc_job_expire_interval", - Default: 10 * time.Second, + Default: fs.Duration(10 * time.Second), Help: "Interval to check for expired async jobs", Groups: "RC", }, { @@ -120,8 +120,8 @@ type Options struct { MetricsHTTP libhttp.Config `config:"metrics"` MetricsAuth libhttp.AuthConfig `config:"metrics"` MetricsTemplate libhttp.TemplateConfig `config:"metrics"` - JobExpireDuration time.Duration `config:"rc_job_expire_duration"` - JobExpireInterval time.Duration `config:"rc_job_expire_interval"` + JobExpireDuration fs.Duration `config:"rc_job_expire_duration"` + JobExpireInterval fs.Duration `config:"rc_job_expire_interval"` } // Opt is the default values used for Options diff --git a/lib/http/server.go b/lib/http/server.go index 57ce8b8bb..def23e01c 100644 --- a/lib/http/server.go +++ b/lib/http/server.go @@ -120,11 +120,11 @@ var ConfigInfo = fs.Options{{ Help: "IPaddress:Port or :Port to bind server to", }, { Name: "server_read_timeout", - Default: 1 * time.Hour, + Default: fs.Duration(1 * time.Hour), Help: "Timeout for server reading data", }, { Name: "server_write_timeout", - Default: 1 * time.Hour, + Default: fs.Duration(1 * time.Hour), Help: "Timeout for server writing data", }, { Name: "max_header_bytes", @@ -158,25 +158,25 @@ var ConfigInfo = fs.Options{{ // Config contains options for the http Server type Config struct { - ListenAddr []string `config:"addr"` // Port to listen on - BaseURL string `config:"baseurl"` // prefix to strip from URLs - ServerReadTimeout time.Duration `config:"server_read_timeout"` // Timeout for server reading data - ServerWriteTimeout time.Duration `config:"server_write_timeout"` // Timeout for server writing data - MaxHeaderBytes int `config:"max_header_bytes"` // Maximum size of request header - TLSCert string `config:"cert"` // Path to TLS PEM public key certificate file (can also include intermediate/CA certificates) - TLSKey string `config:"key"` // Path to TLS PEM private key file - TLSCertBody []byte `config:"-"` // TLS PEM public key certificate body (can also include intermediate/CA certificates), ignores TLSCert - TLSKeyBody []byte `config:"-"` // TLS PEM private key body, ignores TLSKey - ClientCA string `config:"client_ca"` // Path to TLS PEM CA file with certificate authorities to verify clients with - MinTLSVersion string `config:"min_tls_version"` // MinTLSVersion contains the minimum TLS version that is acceptable. - AllowOrigin string `config:"allow_origin"` // AllowOrigin sets the Access-Control-Allow-Origin header + ListenAddr []string `config:"addr"` // Port to listen on + BaseURL string `config:"baseurl"` // prefix to strip from URLs + ServerReadTimeout fs.Duration `config:"server_read_timeout"` // Timeout for server reading data + ServerWriteTimeout fs.Duration `config:"server_write_timeout"` // Timeout for server writing data + MaxHeaderBytes int `config:"max_header_bytes"` // Maximum size of request header + TLSCert string `config:"cert"` // Path to TLS PEM public key certificate file (can also include intermediate/CA certificates) + TLSKey string `config:"key"` // Path to TLS PEM private key file + TLSCertBody []byte `config:"-"` // TLS PEM public key certificate body (can also include intermediate/CA certificates), ignores TLSCert + TLSKeyBody []byte `config:"-"` // TLS PEM private key body, ignores TLSKey + ClientCA string `config:"client_ca"` // Path to TLS PEM CA file with certificate authorities to verify clients with + MinTLSVersion string `config:"min_tls_version"` // MinTLSVersion contains the minimum TLS version that is acceptable. + AllowOrigin string `config:"allow_origin"` // AllowOrigin sets the Access-Control-Allow-Origin header } // AddFlagsPrefix adds flags for the httplib func (cfg *Config) AddFlagsPrefix(flagSet *pflag.FlagSet, prefix string) { flags.StringArrayVarP(flagSet, &cfg.ListenAddr, prefix+"addr", "", cfg.ListenAddr, "IPaddress:Port, :Port or [unix://]/path/to/socket to bind server to", prefix) - flags.DurationVarP(flagSet, &cfg.ServerReadTimeout, prefix+"server-read-timeout", "", cfg.ServerReadTimeout, "Timeout for server reading data", prefix) - flags.DurationVarP(flagSet, &cfg.ServerWriteTimeout, prefix+"server-write-timeout", "", cfg.ServerWriteTimeout, "Timeout for server writing data", prefix) + flags.FVarP(flagSet, &cfg.ServerReadTimeout, prefix+"server-read-timeout", "", "Timeout for server reading data", prefix) + flags.FVarP(flagSet, &cfg.ServerWriteTimeout, prefix+"server-write-timeout", "", "Timeout for server writing data", prefix) flags.IntVarP(flagSet, &cfg.MaxHeaderBytes, prefix+"max-header-bytes", "", cfg.MaxHeaderBytes, "Maximum size of request header", prefix) flags.StringVarP(flagSet, &cfg.TLSCert, prefix+"cert", "", cfg.TLSCert, "Path to TLS PEM public key certificate file (can also include intermediate/CA certificates)", prefix) flags.StringVarP(flagSet, &cfg.TLSKey, prefix+"key", "", cfg.TLSKey, "Path to TLS PEM private key file", prefix) @@ -198,8 +198,8 @@ func AddHTTPFlagsPrefix(flagSet *pflag.FlagSet, prefix string, cfg *Config) { func DefaultCfg() Config { return Config{ ListenAddr: []string{"127.0.0.1:8080"}, - ServerReadTimeout: 1 * time.Hour, - ServerWriteTimeout: 1 * time.Hour, + ServerReadTimeout: fs.Duration(1 * time.Hour), + ServerWriteTimeout: fs.Duration(1 * time.Hour), MaxHeaderBytes: 4096, MinTLSVersion: "tls1.0", } @@ -272,8 +272,8 @@ func newInstance(ctx context.Context, s *Server, listener net.Listener, tlsCfg * listener: listener, httpServer: &http.Server{ Handler: s.mux, - ReadTimeout: s.cfg.ServerReadTimeout, - WriteTimeout: s.cfg.ServerWriteTimeout, + ReadTimeout: time.Duration(s.cfg.ServerReadTimeout), + WriteTimeout: time.Duration(s.cfg.ServerWriteTimeout), MaxHeaderBytes: s.cfg.MaxHeaderBytes, ReadHeaderTimeout: 10 * time.Second, // time to send the headers IdleTimeout: 60 * time.Second, // time to keep idle connections open