2018-05-07 12:33:16 +00:00
|
|
|
package pubsubmon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ipfs/ipfs-cluster/config"
|
2019-02-08 21:57:16 +00:00
|
|
|
"github.com/kelseyhightower/envconfig"
|
2018-05-07 12:33:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const configKey = "pubsubmon"
|
2019-02-08 21:57:16 +00:00
|
|
|
const envConfigKey = "cluster_pubsubmon"
|
2018-05-07 12:33:16 +00:00
|
|
|
|
|
|
|
// Default values for this Config.
|
|
|
|
const (
|
2019-03-12 03:00:51 +00:00
|
|
|
DefaultCheckInterval = 15 * time.Second
|
|
|
|
DefaultFailureThreshold = 3.0
|
2018-05-07 12:33:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Config allows to initialize a Monitor and customize some parameters.
|
|
|
|
type Config struct {
|
|
|
|
config.Saver
|
|
|
|
|
2019-04-18 00:53:27 +00:00
|
|
|
CheckInterval time.Duration
|
|
|
|
// FailureThreshold indicates when a peer should be considered failed.
|
|
|
|
// The greater the threshold value the more leniency is granted.
|
|
|
|
// A value between 2.0 and 4.0 is suggested for the threshold.
|
2019-03-12 03:00:51 +00:00
|
|
|
FailureThreshold float64
|
2018-05-07 12:33:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type jsonConfig struct {
|
2019-03-12 03:00:51 +00:00
|
|
|
CheckInterval string `json:"check_interval"`
|
|
|
|
FailureThreshold float64 `json:"failure_threshold"`
|
2018-05-07 12:33:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ConfigKey provides a human-friendly identifier for this type of Config.
|
|
|
|
func (cfg *Config) ConfigKey() string {
|
|
|
|
return configKey
|
|
|
|
}
|
|
|
|
|
|
|
|
// Default sets the fields of this Config to sensible values.
|
|
|
|
func (cfg *Config) Default() error {
|
|
|
|
cfg.CheckInterval = DefaultCheckInterval
|
2019-03-12 03:00:51 +00:00
|
|
|
cfg.FailureThreshold = DefaultFailureThreshold
|
2018-05-07 12:33:16 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-02-07 18:46:42 +00:00
|
|
|
// ApplyEnvVars fills in any Config fields found
|
|
|
|
// as environment variables.
|
|
|
|
func (cfg *Config) ApplyEnvVars() error {
|
2019-02-15 17:07:20 +00:00
|
|
|
jcfg := cfg.toJSONConfig()
|
2019-02-08 21:57:16 +00:00
|
|
|
|
|
|
|
err := envconfig.Process(envConfigKey, jcfg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return cfg.applyJSONConfig(jcfg)
|
2019-02-07 18:46:42 +00:00
|
|
|
}
|
|
|
|
|
2018-05-07 12:33:16 +00:00
|
|
|
// Validate checks that the fields of this Config have working values,
|
|
|
|
// at least in appearance.
|
|
|
|
func (cfg *Config) Validate() error {
|
|
|
|
if cfg.CheckInterval <= 0 {
|
2019-03-21 17:18:40 +00:00
|
|
|
return errors.New("pubsubmon.check_interval too low")
|
2018-05-07 12:33:16 +00:00
|
|
|
}
|
2019-03-12 03:00:51 +00:00
|
|
|
|
|
|
|
if cfg.FailureThreshold <= 0 {
|
2019-04-01 05:33:58 +00:00
|
|
|
return errors.New("pubsubmon.failure_threshold too low")
|
2019-03-12 03:00:51 +00:00
|
|
|
}
|
|
|
|
|
2018-05-07 12:33:16 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadJSON sets the fields of this Config to the values defined by the JSON
|
|
|
|
// representation of it, as generated by ToJSON.
|
|
|
|
func (cfg *Config) LoadJSON(raw []byte) error {
|
|
|
|
jcfg := &jsonConfig{}
|
|
|
|
err := json.Unmarshal(raw, jcfg)
|
|
|
|
if err != nil {
|
2019-03-21 17:18:40 +00:00
|
|
|
logger.Error("Error unmarshaling pubsubmon monitor config")
|
2018-05-07 12:33:16 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-02-08 21:57:16 +00:00
|
|
|
cfg.Default()
|
|
|
|
|
|
|
|
return cfg.applyJSONConfig(jcfg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cfg *Config) applyJSONConfig(jcfg *jsonConfig) error {
|
2018-05-07 12:33:16 +00:00
|
|
|
interval, _ := time.ParseDuration(jcfg.CheckInterval)
|
|
|
|
cfg.CheckInterval = interval
|
2019-03-12 03:00:51 +00:00
|
|
|
cfg.FailureThreshold = jcfg.FailureThreshold
|
2018-05-07 12:33:16 +00:00
|
|
|
|
|
|
|
return cfg.Validate()
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToJSON generates a human-friendly JSON representation of this Config.
|
|
|
|
func (cfg *Config) ToJSON() ([]byte, error) {
|
2019-02-15 17:07:20 +00:00
|
|
|
jcfg := cfg.toJSONConfig()
|
2018-05-07 12:33:16 +00:00
|
|
|
|
|
|
|
return json.MarshalIndent(jcfg, "", " ")
|
|
|
|
}
|
2019-02-15 17:07:20 +00:00
|
|
|
|
|
|
|
func (cfg *Config) toJSONConfig() *jsonConfig {
|
|
|
|
return &jsonConfig{
|
2019-03-12 03:00:51 +00:00
|
|
|
CheckInterval: cfg.CheckInterval.String(),
|
|
|
|
FailureThreshold: cfg.FailureThreshold,
|
2019-02-15 17:07:20 +00:00
|
|
|
}
|
|
|
|
}
|