-
Notifications
You must be signed in to change notification settings - Fork 282
/
config.go
158 lines (127 loc) · 4.3 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package stateless
import (
"encoding/json"
"errors"
"time"
"github.com/kelseyhightower/envconfig"
"github.com/ipfs/ipfs-cluster/config"
)
const configKey = "stateless"
const envConfigKey = "cluster_stateless"
// Default values for this Config.
const (
DefaultMaxPinQueueSize = 1000000
DefaultConcurrentPins = 10
DefaultPriorityPinMaxAge = 24 * time.Hour
DefaultPriorityPinMaxRetries = 5
)
// 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
// 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
// PriorityPinMaxAge specifies the maximum age that a pin needs to
// can have since it was submitted to the cluster to be pinned
// preferentially (before pins that are older or have too many retries).
PriorityPinMaxAge time.Duration
// PriorityPinMaxRetries specifies the maximum amount of retries that
// a pin can have before it is moved to a non-prioritary queue.
PriorityPinMaxRetries int
}
type jsonConfig struct {
MaxPinQueueSize int `json:"max_pin_queue_size,omitempty"`
ConcurrentPins int `json:"concurrent_pins"`
PriorityPinMaxAge string `json:"priority_pin_max_age"`
PriorityPinMaxRetries int `json:"priority_pin_max_retries"`
}
// 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
cfg.ConcurrentPins = DefaultConcurrentPins
cfg.PriorityPinMaxAge = DefaultPriorityPinMaxAge
cfg.PriorityPinMaxRetries = DefaultPriorityPinMaxRetries
return nil
}
// ApplyEnvVars fills in any Config fields found
// as environment variables.
func (cfg *Config) ApplyEnvVars() error {
jcfg := cfg.toJSONConfig()
err := envconfig.Process(envConfigKey, jcfg)
if err != nil {
return err
}
return cfg.applyJSONConfig(jcfg)
}
// 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("statelesstracker.max_pin_queue_size too low")
}
if cfg.ConcurrentPins <= 0 {
return errors.New("statelesstracker.concurrent_pins is too low")
}
if cfg.PriorityPinMaxAge <= 0 {
return errors.New("statelesstracker.priority_pin_max_age is too low")
}
if cfg.PriorityPinMaxRetries <= 0 {
return errors.New("statelesstracker.priority_pin_max_retries is too low")
}
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 {
logger.Error("Error unmarshaling statelesstracker config")
return err
}
cfg.Default()
return cfg.applyJSONConfig(jcfg)
}
func (cfg *Config) applyJSONConfig(jcfg *jsonConfig) error {
config.SetIfNotDefault(jcfg.MaxPinQueueSize, &cfg.MaxPinQueueSize)
config.SetIfNotDefault(jcfg.ConcurrentPins, &cfg.ConcurrentPins)
err := config.ParseDurations(cfg.ConfigKey(),
&config.DurationOpt{
Duration: jcfg.PriorityPinMaxAge,
Dst: &cfg.PriorityPinMaxAge,
Name: "priority_pin_max_age",
},
)
if err != nil {
return err
}
config.SetIfNotDefault(jcfg.PriorityPinMaxRetries, &cfg.PriorityPinMaxRetries)
return cfg.Validate()
}
// ToJSON generates a human-friendly JSON representation of this Config.
func (cfg *Config) ToJSON() ([]byte, error) {
jcfg := cfg.toJSONConfig()
return config.DefaultJSONMarshal(jcfg)
}
func (cfg *Config) toJSONConfig() *jsonConfig {
jCfg := &jsonConfig{
ConcurrentPins: cfg.ConcurrentPins,
PriorityPinMaxAge: cfg.PriorityPinMaxAge.String(),
PriorityPinMaxRetries: cfg.PriorityPinMaxRetries,
}
if cfg.MaxPinQueueSize != DefaultMaxPinQueueSize {
jCfg.MaxPinQueueSize = cfg.MaxPinQueueSize
}
return jCfg
}
// ToDisplayJSON returns JSON config as a string.
func (cfg *Config) ToDisplayJSON() ([]byte, error) {
return config.DisplayJSON(cfg.toJSONConfig())
}