github.com/mailru/activerecord@v1.12.2/pkg/iproto/util/pool/config/config.go (about)

     1  package config
     2  
     3  import (
     4  	"runtime"
     5  	"time"
     6  
     7  	"github.com/mailru/activerecord/pkg/iproto/util/pool"
     8  )
     9  
    10  // Config describes an object that is capable to create configuration variables
    11  // which could be changed from outside somehow.
    12  type Config interface {
    13  	Int(string, int, string, ...func(int) error) *int
    14  	Duration(string, time.Duration, string, ...func(time.Duration) error) *time.Duration
    15  }
    16  
    17  // Stat describes an object that is the same as Config but with stat
    18  // additional methods.
    19  type Stat interface {
    20  	Config
    21  	Float64Slice(string, []float64, string, ...func([]float64) error) *[]float64
    22  }
    23  
    24  func Export(config Config, prefix string) func() *pool.Config {
    25  	prefix = sanitize(prefix)
    26  
    27  	var (
    28  		unstoppableWorkers = config.Int(
    29  			prefix+"pool.unstoppable_workers", 1,
    30  			"number of always running workers",
    31  		)
    32  		maxWorkers = config.Int(
    33  			prefix+"pool.max_workers", runtime.NumCPU(),
    34  			"total number of workers that could be spawned",
    35  		)
    36  		extraWorkerTTL = config.Duration(
    37  			prefix+"pool.extra_worker_ttl", pool.DefaultExtraWorkerTTL,
    38  			"time to live for extra spawnd workers",
    39  		)
    40  	)
    41  
    42  	workQueueSize := config.Int(
    43  		prefix+"pool.work_queue_size", 0,
    44  		"work queue size",
    45  	)
    46  
    47  	return func() *pool.Config {
    48  		return &pool.Config{
    49  			UnstoppableWorkers: *unstoppableWorkers,
    50  			MaxWorkers:         *maxWorkers,
    51  			ExtraWorkerTTL:     *extraWorkerTTL,
    52  			WorkQueueSize:      *workQueueSize,
    53  		}
    54  	}
    55  }
    56  
    57  func sanitize(p string) string {
    58  	if n := len(p); n != 0 {
    59  		if p[n-1] != '.' {
    60  			return p + "."
    61  		}
    62  	}
    63  
    64  	return p
    65  }