Skip to content

Hold a credential that expires

Give credentials/renew a function that mints the credential, then call Get before each use and Reject when the server says the credential is no longer valid.

import "gitlab.com/phpboyscout/go/credentials/renew"

src := renew.New(func(ctx context.Context) (renew.Lease[string], error) {
    tok, err := fetchToken(ctx)
    if err != nil {
        return renew.Lease[string]{}, err
    }

    return renew.Lease[string]{Value: tok.Value, Expires: tok.ExpiresAt}, nil
})

token, err := src.Get(ctx)

fetchToken is yours. The package never learns how a token is obtained, only when to ask for another. However many requests are waiting, at most one mint runs at a time.

When it renews

Renewal starts on the first Get within 60 seconds of expiry. That Get still returns the held credential at once, and the renewal runs behind it. Nothing renews on a timer, so an idle process mints nothing.

A credential that lives less than two minutes gets a shorter window, half its lifetime: a 30-second token is renewed after 15 seconds. To change the 60 seconds:

src := renew.New(mint, renew.WithRenewBefore(5*time.Minute))

If a renewal fails while the held credential is still valid, callers keep getting that credential and the next Get tries again. Once it has expired, Get waits for a mint and returns its error.

Pass it to something that takes a token function

src.Get on a Source[string] is a func(context.Context) (string, error):

cfg.Token = src.Get

That gives early renewal only. Renewing on a rejection needs a call to Reject as well.

Renew when the server rejects it

token, err := src.Get(ctx)
if err != nil {
    return err
}

err = call(ctx, token)
if tokenExpired(err) {
    src.Reject(token)
    // call Get again and retry once
}

Pass the credential you sent. If another request has already replaced it, Reject does nothing, so fifty requests rejected together cause one renewal.

Only reject what the server says is invalid

tokenExpired must recognise a response that says this credential is no longer valid. A bare 401 often does not, and a permission refusal or a rate limit never does. Rejecting on those mints a new credential for every refused request.

Reject compares by value, so a mint that returns the same value again after a rejection cannot be told apart from the one rejected.

Hold something other than a string

Source takes any comparable type, so a struct of keys works. Values are compared with ==, which for a time.Time field includes its location and monotonic reading. Leave the expiry out of the value and put it in Lease.Expires.

Use a credential that does not expire

renew.Fixed("token") returns a Source that never renews, for a caller that has a static token where the code takes a Source.

Stop when your service stops

src := renew.New(mint, renew.WithLifetimeContext(serviceCtx))

When serviceCtx ends, no more mints start. One in flight is told through its context, and is abandoned only if your mint honours it.