@@ -223,12 +223,8 @@ func (s *Store) RecreateAll(_ context.Context) error {
223
223
return err
224
224
}
225
225
226
- // We repeatedly lock and unlock the mutex in this function to give other threads a chance to talk to the credential store.
227
- // It can take several minutes to recreate the credentials if there are hundreds of them, and we don't want to
228
- // block all other threads while we do that.
229
226
// New credentials might be created after our GetAll, but they will be created with the current encryption configuration,
230
227
// so it's okay that they are skipped by this function.
231
-
232
228
s .recreateAllLock .Lock ()
233
229
all , err := store .GetAll ()
234
230
s .recreateAllLock .Unlock ()
@@ -238,28 +234,33 @@ func (s *Store) RecreateAll(_ context.Context) error {
238
234
239
235
// Loop through and recreate each individual credential.
240
236
for serverAddress := range all {
241
- s .recreateAllLock .Lock ()
242
- authConfig , err := store .Get (serverAddress )
243
- if err != nil {
244
- s .recreateAllLock .Unlock ()
245
-
246
- if IsCredentialsNotFoundError (err ) {
247
- // This can happen if the credential was deleted between the GetAll and the Get by another thread.
248
- continue
249
- }
237
+ if err := s .recreateCredential (store , serverAddress ); err != nil {
250
238
return err
251
239
}
240
+ }
252
241
253
- if err := store .Erase (serverAddress ); err != nil {
254
- s .recreateAllLock .Unlock ()
255
- return err
256
- }
242
+ return nil
243
+ }
257
244
258
- if err := store .Store (authConfig ); err != nil {
259
- s .recreateAllLock .Unlock ()
260
- return err
245
+ func (s * Store ) recreateCredential (store credentials.Store , serverAddress string ) error {
246
+ s .recreateAllLock .Lock ()
247
+ defer s .recreateAllLock .Unlock ()
248
+
249
+ authConfig , err := store .Get (serverAddress )
250
+ if err != nil {
251
+ if IsCredentialsNotFoundError (err ) {
252
+ // This can happen if the credential was deleted between the GetAll and the Get by another thread.
253
+ return nil
261
254
}
262
- s .recreateAllLock .Unlock ()
255
+ return err
256
+ }
257
+
258
+ if err := store .Erase (serverAddress ); err != nil {
259
+ return err
260
+ }
261
+
262
+ if err := store .Store (authConfig ); err != nil {
263
+ return err
263
264
}
264
265
265
266
return nil
0 commit comments