-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.go
More file actions
39 lines (32 loc) · 670 Bytes
/
cache.go
File metadata and controls
39 lines (32 loc) · 670 Bytes
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
35
36
37
38
39
package main
import (
"crypto/tls"
"sync"
)
// CertStorage is a simple certificate cache that keeps
// everything in memory.
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),
}
}