zero: merge pomerium/zero-sdk (#4848)

This commit is contained in:
Denis Mishin 2023-12-11 17:31:39 -05:00 committed by GitHub
parent c4dd965f2d
commit ea64902a73
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
54 changed files with 4800 additions and 170 deletions

View file

@ -0,0 +1,55 @@
package cluster
import (
"net/url"
"sync"
"time"
)
// URLCache is a cache of URLs to download bundles from.
type URLCache struct {
mx sync.RWMutex
cache map[string]DownloadCacheEntry
}
// DownloadCacheEntry is a cache entry for a URL to download a bundle from.
type DownloadCacheEntry struct {
// URL is the URL to download the bundle from.
URL url.URL
// ExpiresAt is the time at which the URL expires.
ExpiresAt time.Time
// CaptureHeaders is a list of headers to capture from the response.
CaptureHeaders []string
}
// NewURLCache creates a new URL cache.
func NewURLCache() *URLCache {
return &URLCache{
cache: make(map[string]DownloadCacheEntry),
}
}
// Get gets the cache entry for the given key, if it exists and has not expired.
func (c *URLCache) Get(key string, minTTL time.Duration) (*DownloadCacheEntry, bool) {
c.mx.RLock()
defer c.mx.RUnlock()
entry, ok := c.cache[key]
if !ok {
return nil, false
}
if time.Until(entry.ExpiresAt) < minTTL {
return nil, false
}
return &entry, true
}
// Set sets the cache entry for the given key.
func (c *URLCache) Set(key string, entry DownloadCacheEntry) {
c.mx.Lock()
defer c.mx.Unlock()
c.cache[key] = entry
}