From 78855be916f47877c403c32a06561d8a6cceedb5 Mon Sep 17 00:00:00 2001 From: zyxkad Date: Sat, 30 Sep 2023 17:54:02 -0600 Subject: [PATCH] add timeout for version check --- cmds/mcla/errdb.go | 8 ++++++++ cmds/mcla_wasm/errdb.go | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/cmds/mcla/errdb.go b/cmds/mcla/errdb.go index b4e8cda..d77d474 100644 --- a/cmds/mcla/errdb.go +++ b/cmds/mcla/errdb.go @@ -7,6 +7,7 @@ import ( "net/http" "net/url" "sync" + "time" "github.com/GlobeMC/mcla" ) @@ -41,6 +42,7 @@ type versionDataT struct { type ghErrDB struct { Prefix string cachedVersion versionDataT + lastCheck time.Time } var _ mcla.ErrorDB = (*ghErrDB)(nil) @@ -98,6 +100,10 @@ func (db *ghErrDB)getErrorDesc(id int)(desc *mcla.ErrorDesc, err error){ } func (db *ghErrDB)checkUpdate()(err error){ + if !db.lastCheck.IsZero() && time.Since(db.lastCheck) <= time.Minute { + return nil + } + newVersion, err := db.fetchGhDBVersion() if err != nil { return @@ -151,6 +157,8 @@ func (db *ghErrDB)checkUpdate()(err error){ db.cachedVersion.Patch = newVersion.Patch } // setStorageValue("gh.db.version", db.cachedVersion) + + db.lastCheck = time.Now() return } diff --git a/cmds/mcla_wasm/errdb.go b/cmds/mcla_wasm/errdb.go index 0e7f691..fb04ed4 100644 --- a/cmds/mcla_wasm/errdb.go +++ b/cmds/mcla_wasm/errdb.go @@ -6,6 +6,7 @@ import ( "fmt" "net/url" "sync" + "time" . "github.com/GlobeMC/mcla" ) @@ -40,6 +41,7 @@ type versionDataT struct { type ghErrDB struct { Prefix string cachedVersion versionDataT + lastCheck time.Time } var _ ErrorDB = (*ghErrDB)(nil) @@ -94,6 +96,10 @@ func (db *ghErrDB)getErrorDesc(id int)(desc *ErrorDesc, err error){ } func (db *ghErrDB)checkUpdate()(err error){ + if !db.lastCheck.IsZero() && time.Since(db.lastCheck) <= time.Minute { + return nil + } + newVersion, err := db.fetchGhDBVersion() if err != nil { return @@ -146,6 +152,8 @@ func (db *ghErrDB)checkUpdate()(err error){ db.cachedVersion.Patch = newVersion.Patch } setStorageValue("gh.db.version", db.cachedVersion) + + db.lastCheck = time.Now() return }