mirror of
https://github.com/bitwarden/mobile
synced 2025-12-17 16:53:26 +00:00
[PM-1646] Add thread safety to migration process (#2453)
* Make migration process thread safe * tweaks
This commit is contained in:
@@ -4,7 +4,6 @@ namespace Bit.Core.Abstractions
|
|||||||
{
|
{
|
||||||
public interface IStateMigrationService
|
public interface IStateMigrationService
|
||||||
{
|
{
|
||||||
Task<bool> NeedsMigration();
|
Task MigrateIfNeededAsync();
|
||||||
Task Migrate();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Bit.Core.Abstractions;
|
using Bit.Core.Abstractions;
|
||||||
using Bit.Core.Enums;
|
using Bit.Core.Enums;
|
||||||
@@ -17,6 +18,7 @@ namespace Bit.Core.Services
|
|||||||
private readonly IStorageService _preferencesStorageService;
|
private readonly IStorageService _preferencesStorageService;
|
||||||
private readonly IStorageService _liteDbStorageService;
|
private readonly IStorageService _liteDbStorageService;
|
||||||
private readonly IStorageService _secureStorageService;
|
private readonly IStorageService _secureStorageService;
|
||||||
|
private readonly SemaphoreSlim _semaphore;
|
||||||
|
|
||||||
private enum Storage
|
private enum Storage
|
||||||
{
|
{
|
||||||
@@ -31,9 +33,27 @@ namespace Bit.Core.Services
|
|||||||
_liteDbStorageService = liteDbStorageService;
|
_liteDbStorageService = liteDbStorageService;
|
||||||
_preferencesStorageService = preferenceStorageService;
|
_preferencesStorageService = preferenceStorageService;
|
||||||
_secureStorageService = secureStorageService;
|
_secureStorageService = secureStorageService;
|
||||||
|
|
||||||
|
_semaphore = new SemaphoreSlim(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<bool> NeedsMigration()
|
public async Task MigrateIfNeededAsync()
|
||||||
|
{
|
||||||
|
await _semaphore.WaitAsync();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (await IsMigrationNeededAsync())
|
||||||
|
{
|
||||||
|
await PerformMigrationAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
_semaphore.Release();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task<bool> IsMigrationNeededAsync()
|
||||||
{
|
{
|
||||||
var lastVersion = await GetLastStateVersionAsync();
|
var lastVersion = await GetLastStateVersionAsync();
|
||||||
if (lastVersion == 0)
|
if (lastVersion == 0)
|
||||||
@@ -45,7 +65,7 @@ namespace Bit.Core.Services
|
|||||||
return lastVersion < StateVersion;
|
return lastVersion < StateVersion;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task Migrate()
|
private async Task PerformMigrationAsync()
|
||||||
{
|
{
|
||||||
var lastVersion = await GetLastStateVersionAsync();
|
var lastVersion = await GetLastStateVersionAsync();
|
||||||
switch (lastVersion)
|
switch (lastVersion)
|
||||||
|
|||||||
@@ -1537,11 +1537,8 @@ namespace Bit.Core.Services
|
|||||||
{
|
{
|
||||||
if (!_migrationChecked)
|
if (!_migrationChecked)
|
||||||
{
|
{
|
||||||
var migrationService = ServiceContainer.Resolve<IStateMigrationService>("stateMigrationService");
|
var migrationService = ServiceContainer.Resolve<IStateMigrationService>();
|
||||||
if (await migrationService.NeedsMigration())
|
await migrationService.MigrateIfNeededAsync();
|
||||||
{
|
|
||||||
await migrationService.Migrate();
|
|
||||||
}
|
|
||||||
_migrationChecked = true;
|
_migrationChecked = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user