minor improvements

This commit is contained in:
crapStone 2024-04-27 22:13:02 +02:00 committed by crapStone
parent 7071ee9bff
commit acd02709c7
2 changed files with 14 additions and 32 deletions

View file

@ -28,8 +28,6 @@ import (
var ErrUserRateLimitExceeded = errors.New("rate limit exceeded: 10 certificates per user per 24 hours") var ErrUserRateLimitExceeded = errors.New("rate limit exceeded: 10 certificates per user per 24 hours")
var keyCache *lru.Cache[string, tls.Certificate]
// TLSConfig returns the configuration for generating, serving and cleaning up Let's Encrypt certificates. // TLSConfig returns the configuration for generating, serving and cleaning up Let's Encrypt certificates.
func TLSConfig(mainDomainSuffix string, func TLSConfig(mainDomainSuffix string,
giteaClient *gitea.Client, giteaClient *gitea.Client,
@ -40,6 +38,12 @@ func TLSConfig(mainDomainSuffix string,
noDNS01 bool, noDNS01 bool,
rawDomain string, rawDomain string,
) *tls.Config { ) *tls.Config {
keyCache, err := lru.New[string, tls.Certificate](32)
if err != nil {
panic(err) // This should only happen if 32 < 0 at the time of writing, which should be reason enough to panic.
}
return &tls.Config{ return &tls.Config{
// check DNS name & get certificate from Let's Encrypt // check DNS name & get certificate from Let's Encrypt
GetCertificate: func(info *tls.ClientHelloInfo) (*tls.Certificate, error) { GetCertificate: func(info *tls.ClientHelloInfo) (*tls.Certificate, error) {
@ -109,14 +113,6 @@ func TLSConfig(mainDomainSuffix string,
} }
} }
if keyCache == nil {
var err error
keyCache, err = lru.New[string, tls.Certificate](4096)
if err != nil {
panic(err) // This should only happen if 4096 < 0 at the time of writing, which should be reason enough to panic.
}
}
if tlsCertificate, ok := keyCache.Get(domain); ok { if tlsCertificate, ok := keyCache.Get(domain); ok {
// we can use an existing certificate object // we can use an existing certificate object
return &tlsCertificate, nil return &tlsCertificate, nil

View file

@ -5,19 +5,14 @@ import (
"strings" "strings"
"time" "time"
lru "github.com/hashicorp/golang-lru/v2" "github.com/hashicorp/golang-lru/v2/expirable"
) )
type lookupCacheEntry struct { const lookupCacheValidity = 30 * time.Second
cachedName string const defaultPagesRepo = "pages"
timestamp time.Time
}
var lookupCacheValidity = 30 * time.Second // TODO(): refactor to not use global variables
var lookupCache *expirable.LRU[string, string] = expirable.NewLRU[string, string](4096, nil, lookupCacheValidity)
var lookupCache *lru.Cache[string, lookupCacheEntry]
var defaultPagesRepo = "pages"
// GetTargetFromDNS searches for CNAME or TXT entries on the request domain ending with MainDomainSuffix. // GetTargetFromDNS searches for CNAME or TXT entries on the request domain ending with MainDomainSuffix.
// If everything is fine, it returns the target data. // If everything is fine, it returns the target data.
@ -26,14 +21,8 @@ func GetTargetFromDNS(domain, mainDomainSuffix, firstDefaultBranch string) (targ
var cname string var cname string
var err error var err error
if lookupCache == nil { if entry, ok := lookupCache.Get(domain); ok {
lookupCache, err = lru.New[string, lookupCacheEntry](4096) cname = entry
if err != nil {
panic(err) // This should only happen if 4096 < 0 at the time of writing, which should be reason enough to panic.
}
}
if entry, ok := lookupCache.Get(domain); ok && time.Now().Before(entry.timestamp.Add(lookupCacheValidity)) {
cname = entry.cachedName
} else { } else {
cname, err = net.LookupCNAME(domain) cname, err = net.LookupCNAME(domain)
cname = strings.TrimSuffix(cname, ".") cname = strings.TrimSuffix(cname, ".")
@ -51,10 +40,7 @@ func GetTargetFromDNS(domain, mainDomainSuffix, firstDefaultBranch string) (targ
} }
} }
} }
_ = lookupCache.Add(domain, lookupCacheEntry{ _ = lookupCache.Add(domain, cname)
cname,
time.Now(),
})
} }
if cname == "" { if cname == "" {
return return