github.com/web-platform-tests/wpt.fyi@v0.0.0-20240530210107-70cf978996f1/api/query/web_features_manifest_cache.go (about)

     1  // Copyright 2024 The WPT Dashboard Project. All rights reserved.
     2  // Use of this source code is governed by a BSD-style license that can be
     3  // found in the LICENSE file.
     4  
     5  package query
     6  
     7  import (
     8  	"context"
     9  	"sync"
    10  
    11  	"github.com/web-platform-tests/wpt.fyi/shared"
    12  )
    13  
    14  // SetWebFeaturesDataCache safely swaps the data cache.
    15  // Currently, the separate goroutine in the poll folder will use this.
    16  func SetWebFeaturesDataCache(newData shared.WebFeaturesData) {
    17  	webFeaturesDataCacheLock.Lock()
    18  	defer webFeaturesDataCacheLock.Unlock()
    19  	webFeaturesDataCache = newData
    20  }
    21  
    22  // GetWebFeaturesDataCache safely retrieves the data cache.
    23  func GetWebFeaturesDataCache() shared.WebFeaturesData {
    24  	webFeaturesDataCacheLock.RLock()
    25  	defer webFeaturesDataCacheLock.RUnlock()
    26  
    27  	return webFeaturesDataCache
    28  }
    29  
    30  // webFeaturesDataCache is the local cache of Web Features data in searchcache. Zero value is nil.
    31  var webFeaturesDataCache shared.WebFeaturesData // nolint:gochecknoglobals // TODO: Fix gochecknoglobals lint error
    32  var webFeaturesDataCacheLock sync.RWMutex       // nolint:gochecknoglobals // TODO: Fix gochecknoglobals lint error
    33  
    34  type searchcacheWebFeaturesManifestFetcher struct{}
    35  
    36  func (f searchcacheWebFeaturesManifestFetcher) Fetch() (shared.WebFeaturesData, error) {
    37  	cache := GetWebFeaturesDataCache()
    38  	if cache != nil {
    39  		return cache, nil
    40  	}
    41  
    42  	ctx := context.Background()
    43  	gitHubClient, err := shared.NewAppEngineAPI(ctx).GetGitHubClient()
    44  	if err != nil {
    45  		shared.GetLogger(ctx).Warningf("unable to get github client for searchcache")
    46  
    47  		return nil, err
    48  	}
    49  
    50  	featuresClient := shared.NewGitHubWebFeaturesClient(gitHubClient)
    51  	data, err := featuresClient.Get(ctx)
    52  	if err != nil {
    53  		shared.GetLogger(ctx).Warningf("github client unable to get features for searchcache")
    54  
    55  		return nil, err
    56  	}
    57  
    58  	return data, nil
    59  }