cache.mx raw
1 package tls
2
3 import (
4 "crypto/x509"
5 "sync"
6 )
7
8 // weakCertCache caches parsed x509.Certificates so connections can share them.
9 type weakCertCache struct{ sync.Map }
10
11 func (wcc *weakCertCache) newCert(der []byte) (*x509.Certificate, error) {
12 if entry, ok := wcc.Load([]byte(der)); ok {
13 return entry.(*x509.Certificate), nil
14 }
15 cert, err := x509.ParseCertificate(der)
16 if err != nil {
17 return nil, err
18 }
19 if actual, loaded := wcc.LoadOrStore([]byte(der), cert); loaded {
20 return actual.(*x509.Certificate), nil
21 }
22 return cert, nil
23 }
24
25 var globalCertCache = &weakCertCache{}
26