@@ -23,30 +23,31 @@ import (
2323"time"
2424)
2525
26+ const renameRetryInterval = 50 * time .Millisecond
27+ const renameRetryDuration = 2 * time .Second
28+
2629type rotateOpts struct {
27- renameRetryDuration time.Duration
28- renameRetryInterval time.Duration
30+ enableRetries bool
2931}
3032
3133type RotateOpt func (* rotateOpts )
3234
33- func WithRenameRetries ( duration , interval time. Duration ) RotateOpt {
35+ func DisableRetries ( ) RotateOpt {
3436return func (opts * rotateOpts ) {
35- opts .renameRetryDuration = duration
36- opts .renameRetryInterval = interval
37+ opts .enableRetries = false
3738}
3839}
3940
4041func rename (src , dst string , options rotateOpts ) error {
41- // Perform a regular (non-retrying) rename unless all retry options are specified .
42- if options .renameRetryDuration == 0 || options . renameRetryInterval == 0 {
42+ // Perform a regular (non-retrying) rename if retries are disabled .
43+ if ! options .enableRetries {
4344return os .Rename (src , dst )
4445}
4546
4647// Attempt rename with retries every options.RenameRetryInterval until options.RenameRetryDuration
4748// has elapsed. This is useful in cases where the destination file may be locked or in use.
4849var err error
49- for start := time .Now (); time .Since (start ) < options . renameRetryDuration ; time .Sleep (options . renameRetryInterval ) {
50+ for start := time .Now (); time .Since (start ) < renameRetryDuration ; time .Sleep (renameRetryInterval ) {
5051err = os .Rename (src , dst )
5152if err == nil {
5253// Rename succeeded; no more retries needed
@@ -55,7 +56,7 @@ func rename(src, dst string, options rotateOpts) error {
5556}
5657
5758if err != nil {
58- return fmt .Errorf ("failed to rename %s to %s after %v: %w" , src , dst , options . renameRetryDuration , err )
59+ return fmt .Errorf ("failed to rename %s to %s after %v: %w" , src , dst , renameRetryDuration , err )
5960}
6061
6162return nil
0 commit comments