This commit is contained in:
37
cache.go
Normal file
37
cache.go
Normal file
@@ -0,0 +1,37 @@
|
||||
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()
|
||||
cs.certs[hostname] = cert
|
||||
cs.mtx.Unlock()
|
||||
|
||||
return cert, nil
|
||||
}
|
||||
|
||||
func NewCertStorage() *CertStorage {
|
||||
return &CertStorage{
|
||||
certs: make(map[string]*tls.Certificate),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user