1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
package main
import (
"crypto/tls"
"sync"
)
type CertStorage struct {
certs sync.Map
}
func (tcs *CertStorage) Fetch(hostname string, gen func() (*tls.Certificate, error)) (*tls.Certificate, error) {
var cert tls.Certificate
icert, ok := tcs.certs.Load(hostname)
if ok {
cert = icert.(tls.Certificate)
} else {
certp, err := gen()
if err != nil {
return nil, err
}
// store as concrete implementation
cert = *certp
tcs.certs.Store(hostname, cert)
}
return &cert, nil
}
func NewCertStorage() *CertStorage {
tcs := &CertStorage{}
tcs.certs = sync.Map{}
return tcs
}
|