Disable prometheus fot in-memory store if period set to 0

This commit is contained in:
Širhoe Biazhkovič
2021-10-20 17:16:11 +03:00
parent 25879a0f3a
commit 823b92fe83
3 changed files with 27 additions and 20 deletions
@@ -1,3 +1,6 @@
// Package directory implements container which
// checks if hash present in any of *.torrent file
// placed in some directory
package directory
import (
@@ -40,9 +43,8 @@ func build(confBytes []byte) (container.Container, error) {
dir = c.BlacklistPath
}
var w *dirwatch.Instance
w, err = dirwatch.New(dir)
if w, err = dirwatch.New(dir); err != nil {
return nil, fmt.Errorf("unable to initialize directory watch")
return nil, fmt.Errorf("unable to initialize directory watch: %v", err)
}
lst.watcher = w
go func() {
@@ -1,3 +1,5 @@
// Package list implements container with pre-defined
// list of torrent hashes from config file
package list
import (
@@ -35,14 +37,13 @@ func build(confBytes []byte) (container.Container, error) {
hashList := c.Whitelist
if l.Invert {
l.Invert = true
hashList = c.Blacklist
}
for _, hashString := range hashList {
hashinfo, err := hex.DecodeString(hashString)
if err != nil {
return nil, fmt.Errorf("whitelist : invalid hash %s", hashString)
return nil, fmt.Errorf("whitelist : invalid hash %s, %v", hashString, err)
}
if len(hashinfo) != 20 {
return nil, fmt.Errorf("whitelist : hash %s is not 20 byes", hashString)
+20 -16
View File
@@ -147,23 +147,27 @@ func New(provided Config) (storage.PeerStore, error) {
}
}()
// Start a goroutine for reporting statistics to Prometheus.
ps.wg.Add(1)
go func() {
defer ps.wg.Done()
t := time.NewTicker(cfg.PrometheusReportingInterval)
for {
select {
case <-ps.closed:
t.Stop()
return
case <-t.C:
before := time.Now()
ps.populateProm()
log.Debug("storage: populateProm() finished", log.Fields{"timeTaken": time.Since(before)})
if cfg.PrometheusReportingInterval > 0 {
// Start a goroutine for reporting statistics to Prometheus.
ps.wg.Add(1)
go func() {
defer ps.wg.Done()
t := time.NewTicker(cfg.PrometheusReportingInterval)
for {
select {
case <-ps.closed:
t.Stop()
return
case <-t.C:
before := time.Now()
ps.populateProm()
log.Debug("storage: populateProm() finished", log.Fields{"timeTaken": time.Since(before)})
}
}
}
}()
}()
} else {
log.Info("prometheus disabled because of zero reporting interval")
}
return ps, nil
}