Skip to content

Commit

Permalink
add error db checkUpdate lock
Browse files Browse the repository at this point in the history
  • Loading branch information
zyxkad committed Oct 1, 2023
1 parent 78855be commit bea57e5
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
9 changes: 8 additions & 1 deletion cmds/mcla/errdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"net/url"
"sync"
"sync/atomic"
"time"

"github.com/GlobeMC/mcla"
Expand Down Expand Up @@ -41,6 +42,7 @@ type versionDataT struct {

type ghErrDB struct {
Prefix string
checking atomic.Bool
cachedVersion versionDataT
lastCheck time.Time
}
Expand Down Expand Up @@ -100,8 +102,13 @@ func (db *ghErrDB)getErrorDesc(id int)(desc *mcla.ErrorDesc, err error){
}

func (db *ghErrDB)checkUpdate()(err error){
if !db.checking.CompareAndSwap(false, true) {
return
}
defer db.checking.Store(false)

if !db.lastCheck.IsZero() && time.Since(db.lastCheck) <= time.Minute {
return nil
return
}

newVersion, err := db.fetchGhDBVersion()
Expand Down
7 changes: 7 additions & 0 deletions cmds/mcla_wasm/errdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"net/url"
"sync"
"sync/atomic"
"time"

. "github.com/GlobeMC/mcla"
Expand Down Expand Up @@ -40,6 +41,7 @@ type versionDataT struct {

type ghErrDB struct {
Prefix string
checking atomic.Bool
cachedVersion versionDataT
lastCheck time.Time
}
Expand Down Expand Up @@ -96,6 +98,11 @@ func (db *ghErrDB)getErrorDesc(id int)(desc *ErrorDesc, err error){
}

func (db *ghErrDB)checkUpdate()(err error){
if !db.checking.CompareAndSwap(false, true) {
return
}
defer db.checking.Store(false)

if !db.lastCheck.IsZero() && time.Since(db.lastCheck) <= time.Minute {
return nil
}
Expand Down
2 changes: 2 additions & 0 deletions cmds/mcla_wasm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ func getAPI()(m Map){
}

func main(){
defaultErrDB.checkUpdate() // refresh caches

api := getAPI()
api["release"] = js.FuncOf(func(_ js.Value, _ []js.Value)(_ any){
global.Delete("MCLA")
Expand Down

0 comments on commit bea57e5

Please sign in to comment.