2017-11-29 13:32:26 +00:00
|
|
|
package maptracker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
|
2019-02-08 21:57:16 +00:00
|
|
|
"github.com/kelseyhightower/envconfig"
|
|
|
|
|
2017-11-29 13:32:26 +00:00
|
|
|
"github.com/ipfs/ipfs-cluster/config"
|
|
|
|
)
|
|
|
|
|
|
|
|
const configKey = "maptracker"
|
2019-02-08 21:57:16 +00:00
|
|
|
const envConfigKey = "cluster_maptracker"
|
2017-11-29 13:32:26 +00:00
|
|
|
|
|
|
|
// Default values for this Config.
|
|
|
|
const (
|
2018-08-14 17:57:14 +00:00
|
|
|
DefaultMaxPinQueueSize = 50000
|
2018-05-14 15:00:33 +00:00
|
|
|
DefaultConcurrentPins = 10
|
2017-11-29 13:32:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Config allows to initialize a Monitor and customize some parameters.
|
|
|
|
type Config struct {
|
|
|
|
config.Saver
|
|
|
|
|
|
|
|
// If higher, they will automatically marked with an error.
|
|
|
|
MaxPinQueueSize int
|
2018-03-07 13:51:37 +00:00
|
|
|
// ConcurrentPins specifies how many pin requests can be sent to the ipfs
|
|
|
|
// daemon in parallel. If the pinning method is "refs", it might increase
|
|
|
|
// speed. Unpin requests are always processed one by one.
|
|
|
|
ConcurrentPins int
|
2017-11-29 13:32:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type jsonConfig struct {
|
2018-05-02 11:23:16 +00:00
|
|
|
MaxPinQueueSize int `json:"max_pin_queue_size"`
|
|
|
|
ConcurrentPins int `json:"concurrent_pins"`
|
2017-11-29 13:32:26 +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.MaxPinQueueSize = DefaultMaxPinQueueSize
|
2018-03-07 13:51:37 +00:00
|
|
|
cfg.ConcurrentPins = DefaultConcurrentPins
|
2017-11-29 13:32:26 +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-08 21:57:16 +00:00
|
|
|
jcfg := &jsonConfig{}
|
|
|
|
|
|
|
|
err := envconfig.Process(envConfigKey, jcfg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return cfg.applyJSONConfig(jcfg)
|
2019-02-07 18:46:42 +00:00
|
|
|
}
|
|
|
|
|
2017-11-29 13:32:26 +00:00
|
|
|
// Validate checks that the fields of this Config have working values,
|
|
|
|
// at least in appearance.
|
|
|
|
func (cfg *Config) Validate() error {
|
|
|
|
if cfg.MaxPinQueueSize <= 0 {
|
|
|
|
return errors.New("maptracker.max_pin_queue_size too low")
|
|
|
|
}
|
2018-03-07 13:51:37 +00:00
|
|
|
|
|
|
|
if cfg.ConcurrentPins <= 0 {
|
|
|
|
return errors.New("maptracker.concurrent_pins is too low")
|
|
|
|
}
|
2017-11-29 13:32:26 +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 {
|
2018-03-09 14:02:00 +00:00
|
|
|
logger.Error("Error unmarshaling maptracker config")
|
2017-11-29 13:32:26 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg.Default()
|
|
|
|
|
2019-02-08 21:57:16 +00:00
|
|
|
return cfg.applyJSONConfig(jcfg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cfg *Config) applyJSONConfig(jcfg *jsonConfig) error {
|
2017-11-29 13:32:26 +00:00
|
|
|
config.SetIfNotDefault(jcfg.MaxPinQueueSize, &cfg.MaxPinQueueSize)
|
2018-03-07 13:51:37 +00:00
|
|
|
config.SetIfNotDefault(jcfg.ConcurrentPins, &cfg.ConcurrentPins)
|
2017-11-29 13:32:26 +00:00
|
|
|
|
|
|
|
return cfg.Validate()
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToJSON generates a human-friendly JSON representation of this Config.
|
|
|
|
func (cfg *Config) ToJSON() ([]byte, error) {
|
|
|
|
jcfg := &jsonConfig{}
|
|
|
|
|
|
|
|
jcfg.MaxPinQueueSize = cfg.MaxPinQueueSize
|
2018-03-07 13:51:37 +00:00
|
|
|
jcfg.ConcurrentPins = cfg.ConcurrentPins
|
2017-11-29 13:32:26 +00:00
|
|
|
|
|
|
|
return config.DefaultJSONMarshal(jcfg)
|
|
|
|
}
|