From 1aa6c86d3fcdaa22a2eb4a3c4aa060871bcc15ca Mon Sep 17 00:00:00 2001 From: Leo Balduf Date: Sun, 25 Jun 2017 15:36:17 +0200 Subject: [PATCH] storage: update config defaults --- example_config.yaml | 6 +++--- storage/memory/peer_store.go | 32 ++++++++++++++++++++-------- storage/memorybysubnet/peer_store.go | 32 ++++++++++++++++++++-------- 3 files changed, 49 insertions(+), 21 deletions(-) diff --git a/example_config.yaml b/example_config.yaml index 1427075..5f62329 100644 --- a/example_config.yaml +++ b/example_config.yaml @@ -1,7 +1,7 @@ chihaya: # The interval communicated with BitTorrent clients informing them how # frequently they should announce in between client events. - announce_interval: 15m + announce_interval: 30m # The network interface that will bind to an HTTP endpoint that can be # scraped by an instance of the Prometheus time series database. @@ -70,11 +70,11 @@ chihaya: name: memory config: # The frequency which stale peers are removed. - gc_interval: 14m + gc_interval: 3m # The amount of time until a peer is considered stale. # To avoid churn, keep this slightly larger than `announce_interval` - peer_lifetime: 16m + peer_lifetime: 31m # The number of partitions data will be divided into in order to provide a # higher degree of parallelism. diff --git a/storage/memory/peer_store.go b/storage/memory/peer_store.go index ecbf95a..9206d21 100644 --- a/storage/memory/peer_store.go +++ b/storage/memory/peer_store.go @@ -6,7 +6,6 @@ import ( "encoding/binary" "net" "runtime" - "strconv" "sync" "sync/atomic" "time" @@ -21,6 +20,14 @@ import ( // Name is the name by which this peer store is registered with Chihaya. const Name = "memory" +// Default config constants. +const ( + defaultShardCount = 1024 + defaultPrometheusReportingInterval = time.Second * 1 + defaultGarbageCollectionInterval = time.Minute * 3 + defaultPeerLifetime = time.Minute * 30 +) + func init() { // Register the storage driver. storage.RegisterDriver(Name, driver{}) @@ -70,19 +77,17 @@ func (cfg Config) LogFields() log.Fields { // This function warns to the logger when a value is changed. func (cfg Config) Validate() Config { validcfg := cfg - if cfg.ShardCount > 0 { - validcfg.ShardCount = cfg.ShardCount - } else { - validcfg.ShardCount = 1024 + if cfg.ShardCount <= 0 { + validcfg.ShardCount = defaultShardCount log.WithFields(log.Fields{ "name": Name + ".ShardCount", - "provided": strconv.Itoa(cfg.ShardCount), - "default": strconv.Itoa(validcfg.ShardCount), + "provided": cfg.ShardCount, + "default": validcfg.ShardCount, }).Warnln("falling back to default configuration") } if cfg.GarbageCollectionInterval <= 0 { - validcfg.GarbageCollectionInterval = time.Minute * 14 + validcfg.GarbageCollectionInterval = defaultGarbageCollectionInterval log.WithFields(log.Fields{ "name": Name + ".GarbageCollectionInterval", "provided": cfg.GarbageCollectionInterval, @@ -91,7 +96,7 @@ func (cfg Config) Validate() Config { } if cfg.PrometheusReportingInterval <= 0 { - validcfg.PrometheusReportingInterval = time.Second * 1 + validcfg.PrometheusReportingInterval = defaultPrometheusReportingInterval log.WithFields(log.Fields{ "name": Name + ".PrometheusReportingInterval", "provided": cfg.PrometheusReportingInterval, @@ -99,6 +104,15 @@ func (cfg Config) Validate() Config { }).Warnln("falling back to default configuration") } + if cfg.PeerLifetime <= 0 { + validcfg.PeerLifetime = defaultPeerLifetime + log.WithFields(log.Fields{ + "name": Name + ".PeerLifetime", + "provided": cfg.PeerLifetime, + "default": validcfg.PeerLifetime, + }).Warnln("falling back to default configuration") + } + return validcfg } diff --git a/storage/memorybysubnet/peer_store.go b/storage/memorybysubnet/peer_store.go index 5234297..33d082e 100644 --- a/storage/memorybysubnet/peer_store.go +++ b/storage/memorybysubnet/peer_store.go @@ -7,7 +7,6 @@ import ( "encoding/binary" "net" "runtime" - "strconv" "sync" "sync/atomic" "time" @@ -22,6 +21,14 @@ import ( // Name is the name by which this peer store is registered with Chihaya. const Name = "memorybysubnet" +// Default config constants. +const ( + defaultShardCount = 1024 + defaultPrometheusReportingInterval = time.Second * 1 + defaultGarbageCollectionInterval = time.Minute * 3 + defaultPeerLifetime = time.Minute * 30 +) + func init() { // Register the storage driver. storage.RegisterDriver(Name, driver{}) @@ -75,19 +82,17 @@ func (cfg Config) LogFields() log.Fields { // This function warns to the logger when a value is changed. func (cfg Config) Validate() Config { validcfg := cfg - if cfg.ShardCount > 0 { - validcfg.ShardCount = cfg.ShardCount - } else { - validcfg.ShardCount = 1024 + if cfg.ShardCount <= 0 { + validcfg.ShardCount = defaultShardCount log.WithFields(log.Fields{ "name": Name + ".ShardCount", - "provided": strconv.Itoa(cfg.ShardCount), - "default": strconv.Itoa(validcfg.ShardCount), + "provided": cfg.ShardCount, + "default": validcfg.ShardCount, }).Warnln("falling back to default configuration") } if cfg.GarbageCollectionInterval <= 0 { - validcfg.GarbageCollectionInterval = time.Minute * 14 + validcfg.GarbageCollectionInterval = defaultGarbageCollectionInterval log.WithFields(log.Fields{ "name": Name + ".GarbageCollectionInterval", "provided": cfg.GarbageCollectionInterval, @@ -96,7 +101,7 @@ func (cfg Config) Validate() Config { } if cfg.PrometheusReportingInterval <= 0 { - validcfg.PrometheusReportingInterval = time.Second * 1 + validcfg.PrometheusReportingInterval = defaultPrometheusReportingInterval log.WithFields(log.Fields{ "name": Name + ".PrometheusReportingInterval", "provided": cfg.PrometheusReportingInterval, @@ -104,6 +109,15 @@ func (cfg Config) Validate() Config { }).Warnln("falling back to default configuration") } + if cfg.PeerLifetime <= 0 { + validcfg.PeerLifetime = defaultPeerLifetime + log.WithFields(log.Fields{ + "name": Name + ".PeerLifetime", + "provided": cfg.PeerLifetime, + "default": validcfg.PeerLifetime, + }).Warnln("falling back to default configuration") + } + return validcfg }