Skip to content

Commit

Permalink
fix: qlight token refreshment (#1488)
Browse files Browse the repository at this point in the history
  • Loading branch information
baptiste-b-pegasys authored Aug 25, 2022
1 parent 7b46a8b commit d8a4588
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions qlight/token_holder.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"fmt"
"strings"
"sync"
"time"

"github.com/ethereum/go-ethereum/log"
Expand All @@ -22,6 +23,7 @@ type TokenHolder struct {
peerUpdater RunningPeerAuthUpdater
timer *time.Timer
eta time.Time
lock sync.Mutex
}

func NewTokenHolder(psi string, pluginManager *plugin.PluginManager) (*TokenHolder, error) {
Expand Down Expand Up @@ -92,16 +94,7 @@ func (h *TokenHolder) refreshPlugin(pluginManager plugin.PluginManagerInterface,
}

func (h *TokenHolder) HttpCredentialsProvider(ctx context.Context) (string, error) {
if h.plugin != nil {
log.Debug("HttpCredentialsProvider using plugin")
err := h.updateTimer()
if err != nil {
log.Warn("update token timer", "err", err)
}
return h.plugin.TokenRefresh(ctx, h.token, h.psi)
}
log.Debug("HttpCredentialsProvider using token")
return h.token, nil
return h.CurrentToken(), nil
}

func (h *TokenHolder) ReloadPlugin() error {
Expand Down Expand Up @@ -134,6 +127,8 @@ func (h *TokenHolder) CurrentToken() string {
if !expired {
return h.token
}
h.lock.Lock()
defer h.lock.Unlock()
returnedToken, err := h.plugin.TokenRefresh(context.Background(), h.token, h.psi)
if err != nil {
log.Error("get token from plugin", "err", err)
Expand Down Expand Up @@ -221,7 +216,7 @@ func (h *TokenHolder) tokenExpirationDelay() (time.Duration, error) {
func (h *TokenHolder) tokenExpired() (bool, error) {
expireIn, err := h.tokenExpirationDelay()
if err != nil {
return false, err
return true, err
}
return expireIn < time.Duration(h.refreshAnticipation)*time.Millisecond, nil
}
Expand Down

0 comments on commit d8a4588

Please sign in to comment.