Skip to content

Commit

Permalink
Use RWMutex
Browse files Browse the repository at this point in the history
Signed-off-by: Meredith Lancaster <[email protected]>
  • Loading branch information
malancas committed Jun 14, 2024
1 parent 6b8ffcb commit c573163
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions pkg/tuf/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,19 +293,18 @@ func ClientFromRemote(_ context.Context, mirror string, rootJSON []byte, targets
}

var (
mu sync.Mutex
mu sync.RWMutex
singletonRootError error
timestamp time.Time
trustedRoot *root.TrustedRoot
)

// GetTrustedRoot returns the trusted root for the TUF repository.
func GetTrustedRoot() (*root.TrustedRoot, error) {
mu.Lock()
defer mu.Unlock()

now := time.Now().UTC()
if timestamp.IsZero() || timestamp.Before(now.Add(-24*time.Hour)) {
mu.Lock()
defer mu.Unlock()

tufClient, err := tuf.NewFromEnv(context.Background())
if err != nil {
Expand All @@ -328,5 +327,9 @@ func GetTrustedRoot() (*root.TrustedRoot, error) {

return trustedRoot, nil
}

mu.RLock()
defer mu.RUnlock()

return trustedRoot, nil
}

0 comments on commit c573163

Please sign in to comment.