acme: Fix race condition in LocalStore during saving.
This commit is contained in:
parent
ddc663eac0
commit
ab13019bde
2 changed files with 104 additions and 2 deletions
|
@ -34,7 +34,10 @@ func (s *LocalStore) save(resolverName string, storedData *StoredData) {
|
|||
defer s.lock.Unlock()
|
||||
|
||||
s.storedData[resolverName] = storedData
|
||||
s.saveDataChan <- s.storedData
|
||||
|
||||
// we cannot pass s.storedData directly, map is reference type and as result
|
||||
// we can face with race condition, so we need to work with objects copy
|
||||
s.saveDataChan <- s.unSafeCopyOfStoredData()
|
||||
}
|
||||
|
||||
func (s *LocalStore) get(resolverName string) (*StoredData, error) {
|
||||
|
@ -81,7 +84,10 @@ func (s *LocalStore) get(resolverName string) (*StoredData, error) {
|
|||
}
|
||||
if len(certificates) < len(storedData.Certificates) {
|
||||
storedData.Certificates = certificates
|
||||
s.saveDataChan <- s.storedData
|
||||
|
||||
// we cannot pass s.storedData directly, map is reference type and as result
|
||||
// we can face with race condition, so we need to work with objects copy
|
||||
s.saveDataChan <- s.unSafeCopyOfStoredData()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -111,6 +117,15 @@ func (s *LocalStore) listenSaveAction() {
|
|||
})
|
||||
}
|
||||
|
||||
// unSafeCopyOfStoredData creates maps copy of storedData. Is not thread safe, you should use `s.lock`.
|
||||
func (s *LocalStore) unSafeCopyOfStoredData() map[string]*StoredData {
|
||||
result := map[string]*StoredData{}
|
||||
for k, v := range s.storedData {
|
||||
result[k] = v
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// GetAccount returns ACME Account.
|
||||
func (s *LocalStore) GetAccount(resolverName string) (*Account, error) {
|
||||
storedData, err := s.get(resolverName)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue