mirror of
https://github.com/ipfs/kubo.git
synced 2025-07-01 10:49:24 +08:00
Limit update checks to once per configurable time period to avoid HTTP request time delay
This commit is contained in:

committed by
Juan Batiz-Benet

parent
3e40ada0ee
commit
b60f0d582f
@ -120,13 +120,16 @@ func localNode(confdir string, online bool) (*core.IpfsNode, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if cfg.Version.Check != config.CheckIgnore {
|
if cfg.Version.EligibleForUpdateCheck() {
|
||||||
obsolete := updates.CheckForUpdates()
|
obsolete := updates.CheckForUpdates()
|
||||||
if obsolete != nil {
|
if obsolete != nil {
|
||||||
if cfg.Version.Check == config.CheckError {
|
if cfg.Version.Check == config.CheckError {
|
||||||
return nil, obsolete
|
return nil, obsolete
|
||||||
}
|
}
|
||||||
log.Warning(fmt.Sprintf("%v", obsolete)) // when "warn" version.check mode we just show warning message
|
log.Warning(fmt.Sprintf("%v", obsolete)) // when "warn" version.check mode we just show warning message
|
||||||
|
} else {
|
||||||
|
// update most recent check timestamp in config
|
||||||
|
cfg.RecordCurrentUpdateCheck(filename)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,18 +45,6 @@ func (bp *BootstrapPeer) String() string {
|
|||||||
return bp.Address + "/" + bp.PeerID
|
return bp.Address + "/" + bp.PeerID
|
||||||
}
|
}
|
||||||
|
|
||||||
// Version regulates checking if the most recent version is run
|
|
||||||
type Version struct {
|
|
||||||
Check string // "ignore" for do not check, "warn" and "error" for reacting when obsolete
|
|
||||||
Current string // ipfs version for which config was generated
|
|
||||||
}
|
|
||||||
|
|
||||||
const (
|
|
||||||
CheckError = "error" // value for Version.Check to raise error and exit if version is obsolete
|
|
||||||
CheckWarn = "warn" // value for Version.Check to show warning message if version is obsolete
|
|
||||||
CheckIgnore = "ignore" // value for Version.Check to not perform update check
|
|
||||||
)
|
|
||||||
|
|
||||||
// Config is used to load IPFS config files.
|
// Config is used to load IPFS config files.
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Identity Identity // local node's peer identity
|
Identity Identity // local node's peer identity
|
||||||
|
39
config/version.go
Normal file
39
config/version.go
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
|
// Version regulates checking if the most recent version is run
|
||||||
|
type Version struct {
|
||||||
|
Check string // "ignore" for do not check, "warn" and "error" for reacting when obsolete
|
||||||
|
Current string // ipfs version for which config was generated
|
||||||
|
UpdateCheckedTime time.Time // timestamp for the last time API endpoint was checked for updates
|
||||||
|
UpdateCheckPeriod time.Duration // time duration over which the update check will not be performed
|
||||||
|
}
|
||||||
|
|
||||||
|
// supported Version.Check values
|
||||||
|
const (
|
||||||
|
CheckError = "error" // value for Version.Check to raise error and exit if version is obsolete
|
||||||
|
CheckWarn = "warn" // value for Version.Check to show warning message if version is obsolete
|
||||||
|
CheckIgnore = "ignore" // value for Version.Check to not perform update check
|
||||||
|
)
|
||||||
|
|
||||||
|
var defaultUpdateCheckPeriod = time.Hour * 48
|
||||||
|
|
||||||
|
// EligibleForUpdateCheck returns if update check API endpoint is needed for this specific runtime
|
||||||
|
func (v *Version) EligibleForUpdateCheck() bool {
|
||||||
|
if v.Check == CheckIgnore || v.UpdateCheckedTime.Add(v.UpdateCheckPeriod).After(time.Now()) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// RecordCurrentUpdateCheck is called to record that update check was performed and showed that the running version is the most recent one
|
||||||
|
func (cfg *Config) RecordCurrentUpdateCheck(filename string) {
|
||||||
|
cfg.Version.UpdateCheckedTime = time.Now()
|
||||||
|
if cfg.Version.UpdateCheckPeriod == time.Duration(0) {
|
||||||
|
// UpdateCheckPeriod was not initialized for some reason (e.g. config file used is broken)
|
||||||
|
cfg.Version.UpdateCheckPeriod = defaultUpdateCheckPeriod
|
||||||
|
}
|
||||||
|
|
||||||
|
WriteConfigFile(filename, cfg)
|
||||||
|
}
|
Reference in New Issue
Block a user