|
| 1 | +import 'dart:async'; |
| 2 | + |
| 3 | +import 'sqlite_connection.dart'; |
| 4 | +import 'sqlite_connection_factory.dart'; |
| 5 | +import 'sqlite_connection_impl.dart'; |
| 6 | +import 'sqlite_queries.dart'; |
| 7 | +import 'update_notification.dart'; |
| 8 | + |
| 9 | +/// A connection pool with a single write connection and multiple read connections. |
| 10 | +class SqliteConnectionPool with SqliteQueries implements SqliteConnection { |
| 11 | + SqliteConnection? _writeConnection; |
| 12 | + |
| 13 | + final List<SqliteConnectionImpl> _readConnections = []; |
| 14 | + |
| 15 | + final SqliteConnectionFactory _factory; |
| 16 | + |
| 17 | + @override |
| 18 | + final Stream<UpdateNotification>? updates; |
| 19 | + |
| 20 | + final int maxReaders; |
| 21 | + |
| 22 | + final String? debugName; |
| 23 | + |
| 24 | + /// Open a new connection pool. |
| 25 | + /// |
| 26 | + /// The provided factory is used to open connections on demand. Connections |
| 27 | + /// are only opened when requested for the first time. |
| 28 | + /// |
| 29 | + /// [maxReaders] specifies the maximum number of read connections. |
| 30 | + /// A maximum of one write connection will be opened. |
| 31 | + /// |
| 32 | + /// Read connections are opened in read-only mode, and will reject any statements |
| 33 | + /// that modify the database. |
| 34 | + SqliteConnectionPool(this._factory, |
| 35 | + {this.updates, |
| 36 | + this.maxReaders = 5, |
| 37 | + SqliteConnection? writeConnection, |
| 38 | + this.debugName}) |
| 39 | + : _writeConnection = writeConnection; |
| 40 | + |
| 41 | + @override |
| 42 | + Future<T> readLock<T>(Future<T> Function(SqliteReadContext tx) callback, |
| 43 | + {Duration? lockTimeout}) async { |
| 44 | + await _expandPool(); |
| 45 | + |
| 46 | + bool haveLock = false; |
| 47 | + var completer = Completer<T>(); |
| 48 | + |
| 49 | + var futures = _readConnections.map((connection) async { |
| 50 | + try { |
| 51 | + return await connection.readLock((ctx) async { |
| 52 | + if (haveLock) { |
| 53 | + // Already have a different lock - release this one. |
| 54 | + return false; |
| 55 | + } |
| 56 | + haveLock = true; |
| 57 | + |
| 58 | + var future = callback(ctx); |
| 59 | + completer.complete(future); |
| 60 | + |
| 61 | + // We have to wait for the future to complete before we can release the |
| 62 | + // lock. |
| 63 | + try { |
| 64 | + await future; |
| 65 | + } catch (_) { |
| 66 | + // Ignore |
| 67 | + } |
| 68 | + |
| 69 | + return true; |
| 70 | + }, lockTimeout: lockTimeout); |
| 71 | + } on TimeoutException { |
| 72 | + return false; |
| 73 | + } |
| 74 | + }); |
| 75 | + |
| 76 | + final stream = Stream<bool>.fromFutures(futures); |
| 77 | + var gotAny = await stream.any((element) => element); |
| 78 | + |
| 79 | + if (!gotAny) { |
| 80 | + // All TimeoutExceptions |
| 81 | + throw TimeoutException('Failed to get a read connection', lockTimeout); |
| 82 | + } |
| 83 | + |
| 84 | + try { |
| 85 | + return await completer.future; |
| 86 | + } catch (e) { |
| 87 | + // throw e; |
| 88 | + rethrow; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + @override |
| 93 | + Future<T> writeLock<T>(Future<T> Function(SqliteWriteContext tx) callback, |
| 94 | + {Duration? lockTimeout}) { |
| 95 | + _writeConnection ??= _factory.openConnection( |
| 96 | + debugName: debugName != null ? '$debugName-writer' : null); |
| 97 | + return _writeConnection!.writeLock(callback, lockTimeout: lockTimeout); |
| 98 | + } |
| 99 | + |
| 100 | + Future<void> _expandPool() async { |
| 101 | + if (_readConnections.length >= maxReaders) { |
| 102 | + return; |
| 103 | + } |
| 104 | + bool hasCapacity = _readConnections.any((connection) => !connection.locked); |
| 105 | + if (!hasCapacity) { |
| 106 | + var name = debugName == null |
| 107 | + ? null |
| 108 | + : '$debugName-${_readConnections.length + 1}'; |
| 109 | + var connection = _factory.openConnection( |
| 110 | + updates: updates, |
| 111 | + debugName: name, |
| 112 | + readOnly: true) as SqliteConnectionImpl; |
| 113 | + _readConnections.add(connection); |
| 114 | + |
| 115 | + // Edge case: |
| 116 | + // If we don't await here, there is a chance that a different connection |
| 117 | + // is used for the transaction, and that it finishes and deletes the database |
| 118 | + // while this one is still opening. This is specifically triggered in tests. |
| 119 | + // To avoid that, we wait for the connection to be ready. |
| 120 | + await connection.ready; |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments