github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/apiserver/config.go (about)

     1  // Copyright 2017 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package apiserver
     5  
     6  import (
     7  	"time"
     8  
     9  	"github.com/juju/errors"
    10  )
    11  
    12  // These vars define how we rate limit incoming connections.
    13  const (
    14  	defaultLogSinkRateLimitBurst  = 1000
    15  	defaultLogSinkRateLimitRefill = time.Millisecond
    16  )
    17  
    18  // LogSinkConfig holds parameters to control the API server's
    19  // logsink endpoint behaviour.
    20  type LogSinkConfig struct {
    21  	// DBLoggerBufferSize is the capacity of the database logger's buffer.
    22  	DBLoggerBufferSize int
    23  
    24  	// DBLoggerFlushInterval is the amount of time to allow a log record
    25  	// to sit in the buffer before being flushed to the database.
    26  	DBLoggerFlushInterval time.Duration
    27  
    28  	// RateLimitBurst defines the number of log messages that will be let
    29  	// through before we start rate limiting.
    30  	RateLimitBurst int64
    31  
    32  	// RateLimitRefill defines the rate at which log messages will be let
    33  	// through once the initial burst amount has been depleted.
    34  	RateLimitRefill time.Duration
    35  }
    36  
    37  // Validate validates the logsink endpoint configuration.
    38  func (cfg LogSinkConfig) Validate() error {
    39  	if cfg.DBLoggerBufferSize <= 0 || cfg.DBLoggerBufferSize > 1000 {
    40  		return errors.NotValidf("DBLoggerBufferSize %d <= 0 or > 1000", cfg.DBLoggerBufferSize)
    41  	}
    42  	if cfg.DBLoggerFlushInterval <= 0 || cfg.DBLoggerFlushInterval > 10*time.Second {
    43  		return errors.NotValidf("DBLoggerFlushInterval %s <= 0 or > 10 seconds", cfg.DBLoggerFlushInterval)
    44  	}
    45  	if cfg.RateLimitBurst <= 0 {
    46  		return errors.NotValidf("RateLimitBurst %d <= 0", cfg.RateLimitBurst)
    47  	}
    48  	if cfg.RateLimitRefill <= 0 {
    49  		return errors.NotValidf("RateLimitRefill %s <= 0", cfg.RateLimitRefill)
    50  	}
    51  	return nil
    52  }
    53  
    54  // DefaultLogSinkConfig returns a LogSinkConfig with default values.
    55  func DefaultLogSinkConfig() LogSinkConfig {
    56  	return LogSinkConfig{
    57  		DBLoggerBufferSize:    defaultLoggerBufferSize,
    58  		DBLoggerFlushInterval: defaultLoggerFlushInterval,
    59  		RateLimitBurst:        defaultLogSinkRateLimitBurst,
    60  		RateLimitRefill:       defaultLogSinkRateLimitRefill,
    61  	}
    62  }