add logic to avoid fetching multiple tokens in parallel
This commit is contained in:
parent
4dec2539af
commit
301379574a
2 changed files with 25 additions and 1 deletions
|
@ -11,6 +11,7 @@ import (
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"golang.org/x/sync/semaphore"
|
"golang.org/x/sync/semaphore"
|
||||||
|
@ -26,6 +27,9 @@ type Osuapi struct {
|
||||||
token string
|
token string
|
||||||
expires time.Time
|
expires time.Time
|
||||||
config *config.Config
|
config *config.Config
|
||||||
|
|
||||||
|
tokenLock sync.RWMutex
|
||||||
|
isFetchingToken bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(config *config.Config) *Osuapi {
|
func New(config *config.Config) *Osuapi {
|
||||||
|
@ -35,7 +39,13 @@ func New(config *config.Config) *Osuapi {
|
||||||
|
|
||||||
// want to cap at around 1000 requests a minute, OSU cap is 1200
|
// want to cap at around 1000 requests a minute, OSU cap is 1200
|
||||||
lock := semaphore.NewWeighted(1000)
|
lock := semaphore.NewWeighted(1000)
|
||||||
return &Osuapi{client, lock, "", time.Now(), config}
|
|
||||||
|
return &Osuapi{
|
||||||
|
httpClient: client,
|
||||||
|
lock: lock,
|
||||||
|
expires: time.Now(),
|
||||||
|
config: config,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (api *Osuapi) Token() (token string, err error) {
|
func (api *Osuapi) Token() (token string, err error) {
|
||||||
|
@ -44,6 +54,16 @@ func (api *Osuapi) Token() (token string, err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if api.isFetchingToken {
|
||||||
|
api.tokenLock.RLock()
|
||||||
|
token = api.token
|
||||||
|
api.tokenLock.RUnlock()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
api.tokenLock.Lock()
|
||||||
|
api.isFetchingToken = true
|
||||||
|
|
||||||
data := fmt.Sprintf(
|
data := fmt.Sprintf(
|
||||||
"client_id=%s&client_secret=%s&grant_type=client_credentials&scope=public",
|
"client_id=%s&client_secret=%s&grant_type=client_credentials&scope=public",
|
||||||
api.config.Oauth.ClientId,
|
api.config.Oauth.ClientId,
|
||||||
|
@ -73,7 +93,9 @@ func (api *Osuapi) Token() (token string, err error) {
|
||||||
log.Println("got new access token", osuToken.AccessToken[:12]+"...")
|
log.Println("got new access token", osuToken.AccessToken[:12]+"...")
|
||||||
api.token = osuToken.AccessToken
|
api.token = osuToken.AccessToken
|
||||||
api.expires = time.Now().Add(time.Duration(osuToken.ExpiresIn) * time.Second)
|
api.expires = time.Now().Add(time.Duration(osuToken.ExpiresIn) * time.Second)
|
||||||
|
|
||||||
token = api.token
|
token = api.token
|
||||||
|
api.tokenLock.Unlock()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -14,6 +14,8 @@
|
||||||
<a href="/">subscribe-bot</a>
|
<a href="/">subscribe-bot</a>
|
||||||
</h1>
|
</h1>
|
||||||
|
|
||||||
|
<small>logging in with your osu account does nothing</small>
|
||||||
|
|
||||||
<div class="nav-bar">
|
<div class="nav-bar">
|
||||||
<a href="/">home</a>
|
<a href="/">home</a>
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue