diff options
| author | JP Dillingham <jp@dillingham.me> | 2025-11-16 18:34:28 -0600 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2025-11-16 18:34:48 -0600 |
| commit | 70f16315cf407bdaa5487e9b818acd98704714cf (patch) | |
| tree | 66fd158fd75639b317ec48561f00102f0064088b /src | |
| parent | 4e826f74bfb40a8741c78b2a41d4afdf9a34699c (diff) | |
| download | slskd-70f16315cf407bdaa5487e9b818acd98704714cf.tar.gz | |
use an interceptor to set synchronous pragma for each connection
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Core/Data/SqlitePragmaInterceptor.cs | 55 | ||||
| -rw-r--r-- | src/slskd/Program.cs | 5 |
2 files changed, 57 insertions, 3 deletions
diff --git a/src/slskd/Core/Data/SqlitePragmaInterceptor.cs b/src/slskd/Core/Data/SqlitePragmaInterceptor.cs new file mode 100644 index 00000000..b32982b1 --- /dev/null +++ b/src/slskd/Core/Data/SqlitePragmaInterceptor.cs @@ -0,0 +1,55 @@ +// <copyright file="SqlitePragmaInterceptor.cs" company="slskd Team"> +// Copyright (c) slskd Team. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see https://www.gnu.org/licenses/. +// </copyright> + +namespace slskd +{ + using System.Data.Common; + using System.Threading; + using System.Threading.Tasks; + using Microsoft.Data.Sqlite; + using Microsoft.EntityFrameworkCore.Diagnostics; + + /// <summary> + /// Intercepts SQLite database connections to set PRAGMAs immediately after opening. + /// </summary> + public class SqliteConnectionOpenedInterceptor : DbConnectionInterceptor + { + public override void ConnectionOpened(DbConnection connection, ConnectionEndEventData eventData) + { + if (connection is SqliteConnection) + { + using var cmd = connection.CreateCommand(); + cmd.CommandText = "PRAGMA synchronous=1;"; + cmd.ExecuteNonQuery(); + } + + base.ConnectionOpened(connection, eventData); + } + + public override async Task ConnectionOpenedAsync(DbConnection connection, ConnectionEndEventData eventData, CancellationToken cancellationToken = default) + { + if (connection is SqliteConnection) + { + using var cmd = connection.CreateCommand(); + cmd.CommandText = "PRAGMA synchronous=1;"; + await cmd.ExecuteNonQueryAsync(cancellationToken); + } + + await base.ConnectionOpenedAsync(connection, eventData, cancellationToken); + } + } +} diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index 639d3a88..ee9e2d0c 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -1209,6 +1209,7 @@ namespace slskd services.AddDbContextFactory<T>(options => { options.UseSqlite(connectionString); + options.AddInterceptors(new SqliteConnectionOpenedInterceptor()); if (OptionsAtStartup.Debug && OptionsAtStartup.Flags.LogSQL) { @@ -1230,9 +1231,7 @@ namespace slskd /* set (and validate) our desired PRAGMAs - note: synchronous mode wants to default to 2, and setting it here may not actually do anything - an alternative would be to set it at the start of each new connection, but i'm unsure if that - would have the intended effect. revisit later. + synchronous mode is also set upon every connection via SqliteConnectionOpenedInterceptor. */ ctx.Database.OpenConnection(); var conn = ctx.Database.GetDbConnection(); |
