github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/settings/duration.go (about)

     1  // Copyright 2017 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package settings
    12  
    13  import (
    14  	"time"
    15  
    16  	"github.com/cockroachdb/errors"
    17  )
    18  
    19  // DurationSetting is the interface of a setting variable that will be
    20  // updated automatically when the corresponding cluster-wide setting
    21  // of type "duration" is updated.
    22  type DurationSetting struct {
    23  	common
    24  	defaultValue time.Duration
    25  	validateFn   func(time.Duration) error
    26  }
    27  
    28  var _ extendedSetting = &DurationSetting{}
    29  
    30  // Get retrieves the duration value in the setting.
    31  func (d *DurationSetting) Get(sv *Values) time.Duration {
    32  	return time.Duration(sv.getInt64(d.slotIdx))
    33  }
    34  
    35  func (d *DurationSetting) String(sv *Values) string {
    36  	return EncodeDuration(d.Get(sv))
    37  }
    38  
    39  // Encoded returns the encoded value of the current value of the setting.
    40  func (d *DurationSetting) Encoded(sv *Values) string {
    41  	return d.String(sv)
    42  }
    43  
    44  // EncodedDefault returns the encoded value of the default value of the setting.
    45  func (d *DurationSetting) EncodedDefault() string {
    46  	return EncodeDuration(d.defaultValue)
    47  }
    48  
    49  // Typ returns the short (1 char) string denoting the type of setting.
    50  func (*DurationSetting) Typ() string {
    51  	return "d"
    52  }
    53  
    54  // Validate that a value conforms with the validation function.
    55  func (d *DurationSetting) Validate(v time.Duration) error {
    56  	if d.validateFn != nil {
    57  		if err := d.validateFn(v); err != nil {
    58  			return err
    59  		}
    60  	}
    61  	return nil
    62  }
    63  
    64  // Override changes the setting without validation and also overrides the
    65  // default value.
    66  //
    67  // For testing usage only.
    68  func (d *DurationSetting) Override(sv *Values, v time.Duration) {
    69  	sv.setInt64(d.slotIdx, int64(v))
    70  	sv.setDefaultOverrideInt64(d.slotIdx, int64(v))
    71  }
    72  
    73  func (d *DurationSetting) set(sv *Values, v time.Duration) error {
    74  	if err := d.Validate(v); err != nil {
    75  		return err
    76  	}
    77  	sv.setInt64(d.slotIdx, int64(v))
    78  	return nil
    79  }
    80  
    81  func (d *DurationSetting) setToDefault(sv *Values) {
    82  	// See if the default value was overridden.
    83  	ok, val, _ := sv.getDefaultOverride(d.slotIdx)
    84  	if ok {
    85  		// As per the semantics of override, these values don't go through
    86  		// validation.
    87  		_ = d.set(sv, time.Duration(val))
    88  		return
    89  	}
    90  	if err := d.set(sv, d.defaultValue); err != nil {
    91  		panic(err)
    92  	}
    93  }
    94  
    95  // RegisterDurationSetting defines a new setting with type duration.
    96  func RegisterDurationSetting(key, desc string, defaultValue time.Duration) *DurationSetting {
    97  	return RegisterValidatedDurationSetting(key, desc, defaultValue, nil)
    98  }
    99  
   100  // RegisterPublicDurationSetting defines a new setting with type
   101  // duration and makes it public.
   102  func RegisterPublicDurationSetting(key, desc string, defaultValue time.Duration) *DurationSetting {
   103  	s := RegisterValidatedDurationSetting(key, desc, defaultValue, nil)
   104  	s.SetVisibility(Public)
   105  	return s
   106  }
   107  
   108  // RegisterPublicNonNegativeDurationSetting defines a new setting with
   109  // type duration and makes it public.
   110  func RegisterPublicNonNegativeDurationSetting(
   111  	key, desc string, defaultValue time.Duration,
   112  ) *DurationSetting {
   113  	s := RegisterNonNegativeDurationSetting(key, desc, defaultValue)
   114  	s.SetVisibility(Public)
   115  	return s
   116  }
   117  
   118  // RegisterPublicNonNegativeDurationSettingWithMaximum defines a new setting with
   119  // type duration, makes it public, and sets a maximum value.
   120  // The maximum value is an allowed value.
   121  func RegisterPublicNonNegativeDurationSettingWithMaximum(
   122  	key, desc string, defaultValue time.Duration, maxValue time.Duration,
   123  ) *DurationSetting {
   124  	s := RegisterValidatedDurationSetting(key, desc, defaultValue, func(v time.Duration) error {
   125  		if v < 0 {
   126  			return errors.Errorf("cannot set %s to a negative duration: %s", key, v)
   127  		}
   128  		if v > maxValue {
   129  			return errors.Errorf("cannot set %s to a value larger than %s", key, maxValue)
   130  		}
   131  		return nil
   132  	})
   133  	s.SetVisibility(Public)
   134  	return s
   135  }
   136  
   137  // RegisterNonNegativeDurationSetting defines a new setting with type duration.
   138  func RegisterNonNegativeDurationSetting(
   139  	key, desc string, defaultValue time.Duration,
   140  ) *DurationSetting {
   141  	return RegisterValidatedDurationSetting(key, desc, defaultValue, func(v time.Duration) error {
   142  		if v < 0 {
   143  			return errors.Errorf("cannot set %s to a negative duration: %s", key, v)
   144  		}
   145  		return nil
   146  	})
   147  }
   148  
   149  // RegisterValidatedDurationSetting defines a new setting with type duration.
   150  func RegisterValidatedDurationSetting(
   151  	key, desc string, defaultValue time.Duration, validateFn func(time.Duration) error,
   152  ) *DurationSetting {
   153  	if validateFn != nil {
   154  		if err := validateFn(defaultValue); err != nil {
   155  			panic(errors.Wrap(err, "invalid default"))
   156  		}
   157  	}
   158  	setting := &DurationSetting{
   159  		defaultValue: defaultValue,
   160  		validateFn:   validateFn,
   161  	}
   162  	register(key, desc, setting)
   163  	return setting
   164  }