@@ -88,6 +88,7 @@ type CertCache struct {
88
88
renewedCerts []* x509.Certificate
89
89
ocspUpdateAfterMu sync.RWMutex
90
90
ocspUpdateAfter time.Time
91
+ stop chan struct {}
91
92
// TODO(twifkak): Implement a registry of Updateable instances which can be configured in the toml.
92
93
ocspFile Updateable
93
94
ocspFilePath string
@@ -141,6 +142,7 @@ func New(certs []*x509.Certificate, certFetcher *certfetcher.CertFetcher, domain
141
142
// you'd have one request, in the backend, and updating them all.
142
143
ocspFile : & Chained {first : & InMemory {}, second : & LocalFile {path : ocspCache }},
143
144
ocspFilePath : ocspCache ,
145
+ stop : make (chan struct {}),
144
146
generateOCSPResponse : generateOCSPResponse ,
145
147
client : http.Client {Timeout : 60 * time .Second },
146
148
extractOCSPServer : func (cert * x509.Certificate ) (string , error ) {
@@ -165,7 +167,7 @@ func New(certs []*x509.Certificate, certFetcher *certfetcher.CertFetcher, domain
165
167
}
166
168
}
167
169
168
- func (this * CertCache ) Init (stop chan struct {} ) error {
170
+ func (this * CertCache ) Init () error {
169
171
this .updateCertIfNecessary ()
170
172
171
173
// Prime the OCSP disk and memory cache, so we can start serving immediately.
@@ -181,18 +183,31 @@ func (this *CertCache) Init(stop chan struct{}) error {
181
183
// like the OCSP responder giving you junk, but also sufficient time
182
184
// to raise an alert if something has gone really wrong.
183
185
// 7. The ability to serve old responses while fetching new responses.
184
- go this .maintainOCSP (stop )
186
+ go this .maintainOCSP ()
185
187
186
188
if this .certFetcher != nil {
187
189
// Update Certs in the background.
188
- go this .maintainCerts (stop )
190
+ go this .maintainCerts ()
189
191
}
190
192
191
193
this .isInitialized = true
192
194
193
195
return nil
194
196
}
195
197
198
+ // Stop stops the goroutines spawned in Init, which are automatically updating the certificate and the OCSP response.
199
+ // It returns true if the call actually stops them, false if they have already been stopped.
200
+ func (this * CertCache ) Stop () bool {
201
+ select {
202
+ // this.stop will never be used for sending a value. Thus this case matches only when it has already been closed.
203
+ case <- this .stop :
204
+ return false
205
+ default :
206
+ close (this .stop )
207
+ return true
208
+ }
209
+ }
210
+
196
211
// Gets the latest cert.
197
212
// Returns the current cert if the cache has not been initialized or if the certFetcher is not set (good for testing)
198
213
// If cert is invalid, it will attempt to renew.
@@ -422,7 +437,7 @@ func waitForSpecifiedTime(waitTimeInMinutes int, numRetries int) int {
422
437
423
438
// Checks for OCSP updates every hour. Terminates only when stop receives
424
439
// a message.
425
- func (this * CertCache ) maintainOCSP (stop chan struct {} ) {
440
+ func (this * CertCache ) maintainOCSP () {
426
441
// Only make one request per ocspCheckInterval, to minimize the impact
427
442
// on OCSP servers that are buckling under load, per sleevi requirement:
428
443
// 5. As with any system doing background requests on a remote server,
@@ -440,7 +455,7 @@ func (this *CertCache) maintainOCSP(stop chan struct{}) {
440
455
if err != nil {
441
456
log .Println ("Warning: OCSP update failed. Cached response may expire:" , err )
442
457
}
443
- case <- stop :
458
+ case <- this . stop :
444
459
ticker .Stop ()
445
460
return
446
461
}
@@ -635,7 +650,7 @@ func (this *CertCache) fetchOCSP(orig []byte, certs []*x509.Certificate, ocspUpd
635
650
636
651
// Checks for cert updates every certCheckInterval hours. Terminates only when stop
637
652
// receives a message.
638
- func (this * CertCache ) maintainCerts (stop chan struct {} ) {
653
+ func (this * CertCache ) maintainCerts () {
639
654
// Only make one request per certCheckInterval, to minimize the impact
640
655
// on servers that are buckling under load.
641
656
ticker := time .NewTicker (certCheckInterval )
@@ -644,7 +659,7 @@ func (this *CertCache) maintainCerts(stop chan struct{}) {
644
659
select {
645
660
case <- ticker .C :
646
661
this .updateCertIfNecessary ()
647
- case <- stop :
662
+ case <- this . stop :
648
663
ticker .Stop ()
649
664
return
650
665
}
0 commit comments