diff options
| author | JP Dillingham <jp@dillingham.me> | 2022-10-23 10:07:26 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-10-23 10:07:26 -0500 |
| commit | a85c5cf8d414b71ac66455e9d97cac62b29c95ad (patch) | |
| tree | d441756f46fc8101e852494a79a3166a375b5739 /src | |
| parent | fbc2738b419b395abe166b74dc2d63aacfe0bbc8 (diff) | |
| parent | 4714f978bf6bfbd489134adb18bd89be655d407f (diff) | |
| download | slskd-a85c5cf8d414b71ac66455e9d97cac62b29c95ad.tar.gz | |
Merge pull request #707 from slskd/memory-keepalive
Add keepalive for share databases
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Shares/IShareRepository.cs | 8 | ||||
| -rw-r--r-- | src/slskd/Shares/ShareService.cs | 2 | ||||
| -rw-r--r-- | src/slskd/Shares/SqliteShareRepository.cs | 65 |
3 files changed, 74 insertions, 1 deletions
diff --git a/src/slskd/Shares/IShareRepository.cs b/src/slskd/Shares/IShareRepository.cs index 0cf5cd80..d8a57d65 100644 --- a/src/slskd/Shares/IShareRepository.cs +++ b/src/slskd/Shares/IShareRepository.cs @@ -24,7 +24,7 @@ namespace slskd.Shares /// <summary> /// Persistent storage of shared files and metadata. /// </summary> - public interface IShareRepository + public interface IShareRepository : IDisposable { /// <summary> /// Gets the connection string for this repository. @@ -68,6 +68,12 @@ namespace slskd.Shares void DumpTo(string filename); /// <summary> + /// Enable connection keepalive. + /// </summary> + /// <param name="enable">A value indicating whether the keepalive logic should be executed.</param> + void EnableKeepalive(bool enable); + + /// <summary> /// Finds the filename of the file matching the specified <paramref name="maskedFilename"/>. /// </summary> /// <param name="maskedFilename">The fully qualified remote path of the file.</param> diff --git a/src/slskd/Shares/ShareService.cs b/src/slskd/Shares/ShareService.cs index 88105a8f..2313cee4 100644 --- a/src/slskd/Shares/ShareService.cs +++ b/src/slskd/Shares/ShareService.cs @@ -400,6 +400,8 @@ namespace slskd.Shares } } + Local.Repository.EnableKeepalive(true); + // one of several thigns happened above before we got here: // this method was called with forceRescan = true // the storage mode is memory, and we loaded the in-memory db from a valid backup diff --git a/src/slskd/Shares/SqliteShareRepository.cs b/src/slskd/Shares/SqliteShareRepository.cs index 0a71280d..af402388 100644 --- a/src/slskd/Shares/SqliteShareRepository.cs +++ b/src/slskd/Shares/SqliteShareRepository.cs @@ -21,6 +21,7 @@ namespace slskd.Shares using System.Collections.Generic; using System.IO; using System.Linq; + using System.Timers; using Microsoft.Data.Sqlite; using Serilog; using Soulseek; @@ -37,6 +38,20 @@ namespace slskd.Shares public SqliteShareRepository(string connectionString) { ConnectionString = connectionString; + + // in-memory databases will be destroyed if at any point the number of connections reaches zero to prevent this, + // create a connection and hold it open for the duration of the application. SQLite will destroy this database when + // the connection that created it closes. since this is the first connection, it *should* keep the db alive. + // it's possible that this connection will be recycled somehow due to pooling; the app will crash if this happens + // so i'll know to keep digging. + KeepaliveConnection = GetConnection(ConnectionString); + KeepaliveTimer = new Timer(1000) + { + AutoReset = true, + Enabled = false, // enabled later + }; + + KeepaliveTimer.Elapsed += (_, _) => Keepalive(); } /// <summary> @@ -44,6 +59,9 @@ namespace slskd.Shares /// </summary> public string ConnectionString { get; } + private bool Disposed { get; set; } + private SqliteConnection KeepaliveConnection { get; } + private Timer KeepaliveTimer { get; } private ILogger Log { get; } = Serilog.Log.ForContext<SqliteShareRepository>(); /// <summary> @@ -154,6 +172,15 @@ namespace slskd.Shares } /// <summary> + /// Diposes this object. + /// </summary> + public void Dispose() + { + Dispose(disposing: true); + GC.SuppressFinalize(this); + } + + /// <summary> /// Dumps the contents of the database to a file. /// </summary> /// <param name="filename">The destination file.</param> @@ -167,6 +194,12 @@ namespace slskd.Shares } /// <summary> + /// Enable connection keepalive. + /// </summary> + /// <param name="enable">A value indicating whether the keepalive logic should be executed.</param> + public void EnableKeepalive(bool enable) => KeepaliveTimer.Enabled = enable; + + /// <summary> /// Finds the filename of the file matching the specified <paramref name="maskedFilename"/>. /// </summary> /// <param name="maskedFilename">The fully qualified remote path of the file.</param> @@ -543,6 +576,23 @@ namespace slskd.Shares }); } + /// <summary> + /// Disposes this object. + /// </summary> + /// <param name="disposing"></param> + protected virtual void Dispose(bool disposing) + { + if (!Disposed) + { + if (disposing) + { + KeepaliveConnection.Dispose(); + } + + Disposed = true; + } + } + private SqliteConnection GetConnection(string connectionString = null) { connectionString ??= ConnectionString; @@ -551,5 +601,20 @@ namespace slskd.Shares conn.Open(); return conn; } + + private void Keepalive() + { + using var cmd = new SqliteCommand("SELECT COUNT(*) FROM pragma_table_info(\"filenames\");", KeepaliveConnection); + + var reader = cmd.ExecuteReader(); + + if (!reader.Read() || reader.GetInt32(0) != 1) + { + var msg = "The internal share database has been corrupted or lost, and the application cannot continue to run. Please report this in a GitHub issue here: https://github.com/slskd/slskd/issues"; + Log.Fatal(msg); + Environment.Exit(1); + throw new ApplicationException(msg); + } + } } }
\ No newline at end of file |
