Limit update checks to once per configurable time period to avoid HTTP request time delay
Peter Borzov committed
Oct 9, 2014 at 14:03 UTC
b60f0d582fc2025d212383d62cc63f49c27b5dd1
3 files changed
+43
-13
cmd/ipfs/ipfs.go
+4
-1
@@ -120,13 +120,16 @@ func localNode(confdir string, online bool) (*core.IpfsNode, error) {
120
return nil, err
121
}
122
123
- if cfg.Version.Check != config.CheckIgnore {
123
+ if cfg.Version.EligibleForUpdateCheck() {
124
obsolete := updates.CheckForUpdates()
125
if obsolete != nil {
126
if cfg.Version.Check == config.CheckError {
127
return nil, obsolete
128
}
129
log.Warning(fmt.Sprintf("%v", obsolete)) // when "warn" version.check mode we just show warning message
130
+ } else {
131
+ // update most recent check timestamp in config
132
+ cfg.RecordCurrentUpdateCheck(filename)
133
}
134
}
135
config/config.go
-12
@@ -45,18 +45,6 @@ func (bp *BootstrapPeer) String() string {
45
return bp.Address + "/" + bp.PeerID
46
}
47
48
-// Version regulates checking if the most recent version is run
49
-type Version struct {
50
- Check string // "ignore" for do not check, "warn" and "error" for reacting when obsolete
51
- Current string // ipfs version for which config was generated
52
-}
53
-
54
-const (
55
- CheckError = "error" // value for Version.Check to raise error and exit if version is obsolete
56
- CheckWarn = "warn" // value for Version.Check to show warning message if version is obsolete
57
- CheckIgnore = "ignore" // value for Version.Check to not perform update check
58
-)
59
-
48
// Config is used to load IPFS config files.
49
type Config struct {
50
Identity Identity // local node's peer identity
config/version.go
new
+39
@@ -0,0 +1,39 @@
1
+package config
2
+
3
+import "time"
4
+
5
+// Version regulates checking if the most recent version is run
6
+type Version struct {
7
+ Check string // "ignore" for do not check, "warn" and "error" for reacting when obsolete
8
+ Current string // ipfs version for which config was generated
9
+ UpdateCheckedTime time.Time // timestamp for the last time API endpoint was checked for updates
10
+ UpdateCheckPeriod time.Duration // time duration over which the update check will not be performed
11
+}
12
+
13
+// supported Version.Check values
14
+const (
15
+ CheckError = "error" // value for Version.Check to raise error and exit if version is obsolete
16
+ CheckWarn = "warn" // value for Version.Check to show warning message if version is obsolete
17
+ CheckIgnore = "ignore" // value for Version.Check to not perform update check
18
+)
19
+
20
+var defaultUpdateCheckPeriod = time.Hour * 48
21
+
22
+// EligibleForUpdateCheck returns if update check API endpoint is needed for this specific runtime
23
+func (v *Version) EligibleForUpdateCheck() bool {
24
+ if v.Check == CheckIgnore || v.UpdateCheckedTime.Add(v.UpdateCheckPeriod).After(time.Now()) {
25
+ return false
26
+ }
27
+ return true
28
+}
29
+
30
+// RecordCurrentUpdateCheck is called to record that update check was performed and showed that the running version is the most recent one
31
+func (cfg *Config) RecordCurrentUpdateCheck(filename string) {
32
+ cfg.Version.UpdateCheckedTime = time.Now()
33
+ if cfg.Version.UpdateCheckPeriod == time.Duration(0) {
34
+ // UpdateCheckPeriod was not initialized for some reason (e.g. config file used is broken)
35
+ cfg.Version.UpdateCheckPeriod = defaultUpdateCheckPeriod
36
+ }
37
+
38
+ WriteConfigFile(filename, cfg)
39
+}