Files
FireflyGo_Proxy/cache.go
T
2026-09-02 13:14:41 +07:00

41 lines
706 B
Go

package main
import (
"crypto/tls"
"sync"
)
type CertStorage struct {
certs map[string]*tls.Certificate
mtx sync.RWMutex
}
func (cs *CertStorage) Fetch(hostname string, gen func() (*tls.Certificate, error)) (*tls.Certificate, error) {
cs.mtx.RLock()
cert, ok := cs.certs[hostname]
cs.mtx.RUnlock()
if ok {
return cert, nil
}
cert, err := gen()
if err != nil {
return nil, err
}
cs.mtx.Lock()
if existing, ok := cs.certs[hostname]; ok {
cert = existing
} else {
cs.certs[hostname] = cert
}
cs.mtx.Unlock()
return cert, nil
}
func NewCertStorage() *CertStorage {
return &CertStorage{
certs: make(map[string]*tls.Certificate),
}
}