2025-04-16 16:39:16 +00:00
|
|
|
package cache
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"sync"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
commonParams "github.com/cloudbase/garm-provider-common/params"
|
|
|
|
|
"github.com/cloudbase/garm/params"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var githubToolsCache *GithubToolsCache
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
ghToolsCache := &GithubToolsCache{
|
|
|
|
|
entities: make(map[string]GithubEntityTools),
|
|
|
|
|
}
|
|
|
|
|
githubToolsCache = ghToolsCache
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type GithubEntityTools struct {
|
|
|
|
|
updatedAt time.Time
|
2025-05-14 00:34:54 +00:00
|
|
|
expiresAt time.Time
|
2025-05-12 21:47:13 +00:00
|
|
|
entity params.ForgeEntity
|
2025-04-16 16:39:16 +00:00
|
|
|
tools []commonParams.RunnerApplicationDownload
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type GithubToolsCache struct {
|
|
|
|
|
mux sync.Mutex
|
|
|
|
|
// entity IDs are UUID4s. It is highly unlikely they will collide (🤞).
|
|
|
|
|
entities map[string]GithubEntityTools
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-07 23:01:22 +00:00
|
|
|
func (g *GithubToolsCache) Get(entityID string) ([]commonParams.RunnerApplicationDownload, bool) {
|
2025-04-16 16:39:16 +00:00
|
|
|
g.mux.Lock()
|
|
|
|
|
defer g.mux.Unlock()
|
|
|
|
|
|
2025-05-07 23:01:22 +00:00
|
|
|
if cache, ok := g.entities[entityID]; ok {
|
2025-05-14 00:34:54 +00:00
|
|
|
if cache.entity.Credentials.ForgeType == params.GithubEndpointType {
|
|
|
|
|
if time.Now().UTC().After(cache.expiresAt.Add(-5 * time.Minute)) {
|
|
|
|
|
// Stale cache, remove it.
|
|
|
|
|
delete(g.entities, entityID)
|
|
|
|
|
return nil, false
|
|
|
|
|
}
|
2025-04-16 16:39:16 +00:00
|
|
|
}
|
|
|
|
|
return cache.tools, true
|
|
|
|
|
}
|
|
|
|
|
return nil, false
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-12 21:47:13 +00:00
|
|
|
func (g *GithubToolsCache) Set(entity params.ForgeEntity, tools []commonParams.RunnerApplicationDownload) {
|
2025-04-16 16:39:16 +00:00
|
|
|
g.mux.Lock()
|
|
|
|
|
defer g.mux.Unlock()
|
|
|
|
|
|
2025-05-14 00:34:54 +00:00
|
|
|
forgeTools := GithubEntityTools{
|
2025-04-16 16:39:16 +00:00
|
|
|
updatedAt: time.Now(),
|
|
|
|
|
entity: entity,
|
|
|
|
|
tools: tools,
|
|
|
|
|
}
|
2025-05-14 00:34:54 +00:00
|
|
|
|
|
|
|
|
if entity.Credentials.ForgeType == params.GithubEndpointType {
|
|
|
|
|
forgeTools.expiresAt = time.Now().Add(24 * time.Hour)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
g.entities[entity.ID] = forgeTools
|
2025-04-16 16:39:16 +00:00
|
|
|
}
|
|
|
|
|
|
2025-05-12 21:47:13 +00:00
|
|
|
func SetGithubToolsCache(entity params.ForgeEntity, tools []commonParams.RunnerApplicationDownload) {
|
2025-04-16 16:39:16 +00:00
|
|
|
githubToolsCache.Set(entity, tools)
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-07 23:01:22 +00:00
|
|
|
func GetGithubToolsCache(entityID string) ([]commonParams.RunnerApplicationDownload, bool) {
|
|
|
|
|
return githubToolsCache.Get(entityID)
|
2025-04-16 16:39:16 +00:00
|
|
|
}
|