MCPcopy Create free account
hub / github.com/TheThingsNetwork/lorawan-stack / CachePublicKey

Function CachePublicKey

pkg/packetbroker/token.go:213–229  ·  view source on GitHub ↗

CachePublicKey caches the result from the given PublicKeyProvider with the TTL.

(provider PublicKeyProvider, ttl time.Duration)

Source from the content-addressed store, hash-verified

211
212// CachePublicKey caches the result from the given PublicKeyProvider with the TTL.
213func CachePublicKey(provider PublicKeyProvider, ttl time.Duration) PublicKeyProvider {
214 var (
215 key *jose.JSONWebKeySet
216 err error
217 t time.Time
218 mu sync.Mutex
219 )
220 return PublicKeyProviderFunc(func(ctx context.Context) (*jose.JSONWebKeySet, error) {
221 mu.Lock()
222 defer mu.Unlock()
223 if key == nil && err == nil || time.Since(t) > ttl {
224 key, err = provider.PublicKeys(ctx)
225 t = time.Now()
226 }
227 return key, err
228 })
229}
230
231var errFetchToken = errors.DefineAborted("fetch_token", "fetch token")
232

Callers 2

TestTokenFunction · 0.92

Calls 3

PublicKeyProviderFuncFuncType · 0.85
PublicKeysMethod · 0.65
NowMethod · 0.65

Tested by 1

TestTokenFunction · 0.74