@@ -34,6 +34,7 @@ type ClientManager struct {
34
34
cache map [[sha1 .Size ]byte ]* list.Element
35
35
ll * list.List
36
36
mu sync.Mutex
37
+ once sync.Once
37
38
}
38
39
39
40
// NewClientManager returns a new ClientManager for prolonged, concurrent usage
@@ -60,11 +61,10 @@ func NewClientManager() *ClientManager {
60
61
// Add adds a Client to the manager. You can use this to individually configure
61
62
// Clients in the manager.
62
63
func (m * ClientManager ) Add (client * Client ) {
63
- if m .cache == nil {
64
- m .initInternals ()
65
- }
64
+ m .initInternals ()
66
65
m .mu .Lock ()
67
66
defer m .mu .Unlock ()
67
+
68
68
key := cacheKey (client .Certificate )
69
69
now := time .Now ()
70
70
if ele , hit := m .cache [key ]; hit {
@@ -88,11 +88,10 @@ func (m *ClientManager) Add(client *Client) {
88
88
// the ClientManager's Factory function, store the result in the manager if
89
89
// non-nil, and return it.
90
90
func (m * ClientManager ) Get (certificate tls.Certificate ) * Client {
91
- if m .cache == nil {
92
- m .initInternals ()
93
- }
91
+ m .initInternals ()
94
92
m .mu .Lock ()
95
93
defer m .mu .Unlock ()
94
+
96
95
key := cacheKey (certificate )
97
96
now := time .Now ()
98
97
if ele , hit := m .cache [key ]; hit {
@@ -130,9 +129,10 @@ func (m *ClientManager) Len() int {
130
129
}
131
130
132
131
func (m * ClientManager ) initInternals () {
133
- m .cache = map [[sha1 .Size ]byte ]* list.Element {}
134
- m .ll = list .New ()
135
- m .mu = sync.Mutex {}
132
+ m .once .Do (func () {
133
+ m .cache = map [[sha1 .Size ]byte ]* list.Element {}
134
+ m .ll = list .New ()
135
+ })
136
136
}
137
137
138
138
func (m * ClientManager ) removeOldest () {
0 commit comments