-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(token): cache Fetch tokens by default (#30)
Opts to cache tokens returned by `Fetch` by default. Adds a new `Options` type that can be passed to `Fetch` to configure (disable) this behaviour. As part of this, the `allowUnauthenticated` function option has been moved to that struct. While the original bool is kept to prevent breaking changes, it will be removed before v1 so callers should update their calls ASAP.
- Loading branch information
1 parent
8316650
commit 884a856
Showing
4 changed files
with
161 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright (C) 2024 vcs contributors | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Lesser General Public License as | ||
// published by the Free Software Foundation, either version 3 of the | ||
// License, or (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Lesser General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Lesser General Public | ||
// License along with this program. If not, see | ||
// <https://www.gnu.org/licenses/>. | ||
// | ||
// SPDX-License-Identifier: LGPL-3.0 | ||
|
||
package token | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/jaredallard/vcs" | ||
"github.com/jaredallard/vcs/token/internal/shared" | ||
) | ||
|
||
// tokenCache is a cache of tokens that have been fetched from the | ||
// user's machine. | ||
type tokenCache struct { | ||
// tokensMu is a mutex to protect the tokens map. | ||
tokensMu sync.RWMutex | ||
|
||
// tokens is a map of VCS provider to their respective token. | ||
tokens map[vcs.Provider]*shared.Token | ||
} | ||
|
||
// Get returns a token from the cache if it exists. | ||
func (c *tokenCache) Get(provider vcs.Provider) (*shared.Token, bool) { | ||
c.tokensMu.RLock() | ||
defer c.tokensMu.RUnlock() | ||
|
||
t, ok := c.tokens[provider] | ||
return t, ok | ||
} | ||
|
||
// Set sets a token in the cache. | ||
func (c *tokenCache) Set(provider vcs.Provider, token *shared.Token) { | ||
c.tokensMu.Lock() | ||
defer c.tokensMu.Unlock() | ||
|
||
c.tokens[provider] = token | ||
} | ||
|
||
// cache is the global token cache. | ||
var cache = &tokenCache{tokens: make(map[vcs.Provider]*shared.Token)} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters