storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/cmd/config/scanner/scanner.go (about)

     1  /*
     2   * MinIO Cloud Storage, (C) 2020-2021 MinIO, Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package scanner
    18  
    19  import (
    20  	"strconv"
    21  	"time"
    22  
    23  	"storj.io/minio/cmd/config"
    24  	"storj.io/minio/pkg/env"
    25  )
    26  
    27  // Compression environment variables
    28  const (
    29  	Delay   = "delay"
    30  	MaxWait = "max_wait"
    31  	Cycle   = "cycle"
    32  
    33  	EnvDelay         = "MINIO_SCANNER_DELAY"
    34  	EnvCycle         = "MINIO_SCANNER_CYCLE"
    35  	EnvDelayLegacy   = "MINIO_CRAWLER_DELAY"
    36  	EnvMaxWait       = "MINIO_SCANNER_MAX_WAIT"
    37  	EnvMaxWaitLegacy = "MINIO_CRAWLER_MAX_WAIT"
    38  )
    39  
    40  // Config represents the heal settings.
    41  type Config struct {
    42  	// Delay is the sleep multiplier.
    43  	Delay float64 `json:"delay"`
    44  	// MaxWait is maximum wait time between operations
    45  	MaxWait time.Duration
    46  	// Cycle is the time.Duration between each scanner cycles
    47  	Cycle time.Duration
    48  }
    49  
    50  var (
    51  	// DefaultKVS - default KV config for heal settings
    52  	DefaultKVS = config.KVS{
    53  		config.KV{
    54  			Key:   Delay,
    55  			Value: "10",
    56  		},
    57  		config.KV{
    58  			Key:   MaxWait,
    59  			Value: "15s",
    60  		},
    61  		config.KV{
    62  			Key:   Cycle,
    63  			Value: "1m",
    64  		},
    65  	}
    66  
    67  	// Help provides help for config values
    68  	Help = config.HelpKVS{
    69  		config.HelpKV{
    70  			Key:         Delay,
    71  			Description: `scanner delay multiplier, defaults to '10.0'`,
    72  			Optional:    true,
    73  			Type:        "float",
    74  		},
    75  		config.HelpKV{
    76  			Key:         MaxWait,
    77  			Description: `maximum wait time between operations, defaults to '15s'`,
    78  			Optional:    true,
    79  			Type:        "duration",
    80  		},
    81  		config.HelpKV{
    82  			Key:         Cycle,
    83  			Description: `time duration between scanner cycles, defaults to '1m'`,
    84  			Optional:    true,
    85  			Type:        "duration",
    86  		},
    87  	}
    88  )
    89  
    90  // LookupConfig - lookup config and override with valid environment settings if any.
    91  func LookupConfig(kvs config.KVS) (cfg Config, err error) {
    92  	if err = config.CheckValidKeys(config.ScannerSubSys, kvs, DefaultKVS); err != nil {
    93  		return cfg, err
    94  	}
    95  	delay := env.Get(EnvDelayLegacy, "")
    96  	if delay == "" {
    97  		delay = env.Get(EnvDelay, kvs.Get(Delay))
    98  	}
    99  	cfg.Delay, err = strconv.ParseFloat(delay, 64)
   100  	if err != nil {
   101  		return cfg, err
   102  	}
   103  	maxWait := env.Get(EnvMaxWaitLegacy, "")
   104  	if maxWait == "" {
   105  		maxWait = env.Get(EnvMaxWait, kvs.Get(MaxWait))
   106  	}
   107  	cfg.MaxWait, err = time.ParseDuration(maxWait)
   108  	if err != nil {
   109  		return cfg, err
   110  	}
   111  
   112  	cfg.Cycle, err = time.ParseDuration(env.Get(EnvCycle, kvs.Get(Cycle)))
   113  	if err != nil {
   114  		return cfg, err
   115  	}
   116  	return cfg, nil
   117  }