Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Add Postgres explicit keepalive settings (#2253)
Leendert de Borst8954e9ec4d07
diff --git a/apps/server/AliasVault.Api/Controllers/Security/TwoFactorAuthController.cs b/apps/server/AliasVault.Api/Controllers/Security/TwoFactorAuthController.csindex 2ccf38509..4d36a97bc 100644--- a/apps/server/AliasVault.Api/Controllers/Security/TwoFactorAuthController.cs+++ b/apps/server/AliasVault.Api/Controllers/Security/TwoFactorAuthController.cs@@ -137,28 +137,33 @@ public class TwoFactorAuthController(IDbContextFactory<AliasServerDbContext> dbC}await using var context = await dbContextFactory.CreateDbContextAsync();- await using var transaction = await context.Database.BeginTransactionAsync();- try+ var strategy = context.Database.CreateExecutionStrategy();+ await strategy.ExecuteAsync(async () =>{- // Disable 2FA and remove any existing authenticator key(s) and recovery codes.- await GetUserManager().SetTwoFactorEnabledAsync(user, false);+ await using var transaction = await context.Database.BeginTransactionAsync();- context.UserTokens.RemoveRange(- await context.UserTokens.Where(- x => x.UserId == user.Id &&- (x.Name == "AuthenticatorKey" || x.Name == "RecoveryCodes")).ToListAsync());+ try+ {+ // Disable 2FA and remove any existing authenticator key(s) and recovery codes.+ await GetUserManager().SetTwoFactorEnabledAsync(user, false);- await context.SaveChangesAsync();- await transaction.CommitAsync();+ context.UserTokens.RemoveRange(+ await context.UserTokens.Where(+ x => x.UserId == user.Id &&+ (x.Name == "AuthenticatorKey" || x.Name == "RecoveryCodes")).ToListAsync());- await authLoggingService.LogAuthEventSuccessAsync(user.UserName!, AuthEventType.TwoFactorAuthDisable);- return Ok();- }- catch- {- await transaction.RollbackAsync();- throw;- }+ await context.SaveChangesAsync();+ await transaction.CommitAsync();+ }+ catch+ {+ await transaction.RollbackAsync();+ throw;+ }+ });++ await authLoggingService.LogAuthEventSuccessAsync(user.UserName!, AuthEventType.TwoFactorAuthDisable);+ return Ok();}}diff --git a/apps/server/Databases/AliasServerDb/PostgresqlDbContextFactory.cs b/apps/server/Databases/AliasServerDb/PostgresqlDbContextFactory.csindex 208d31f6b..bd78458a9 100644--- a/apps/server/Databases/AliasServerDb/PostgresqlDbContextFactory.cs+++ b/apps/server/Databases/AliasServerDb/PostgresqlDbContextFactory.cs@@ -84,8 +84,20 @@ public class PostgresqlDbContextFactory : IAliasServerDbContextFactory}}+ // Proxies/loadbalancers in front of PostgreSQL often silently drop idle TCP+ // sessions, which makes pooled connections go stale. Keepalives prevent connections from+ // looking idle, and pruning idle connections early limits the window in which outdated+ // connections could be reused. This prevents potential issues with stale connections in certain environments.+ var keepaliveBuilder = new NpgsqlConnectionStringBuilder(connectionString)+ {+ KeepAlive = 30,+ ConnectionIdleLifetime = 60,+ };+optionsBuilder- .UseNpgsql(connectionString, options => options.CommandTimeout(60))+ .UseNpgsql(keepaliveBuilder.ConnectionString, options => options+ .CommandTimeout(60)+ .EnableRetryOnFailure(maxRetryCount: 3)).UseLazyLoadingProxies();}