mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-26 04:07:50 +00:00
Configuration.writeTargetQueue
This commit is contained in:
1 parent
dd025a14e4
commit
ed4471c5b9
7 files changed
+143
-13
No files matched your search
@@ -248,19 +248,29 @@ public struct Configuration {
|
||||
/// Default: .default
|
||||
public var qos: DispatchQoS = .default
|
||||
|
||||
/// The target queue for all database accesses.
|
||||
/// A target queue for database accesses.
|
||||
///
|
||||
/// When you use a database pool, make sure the queue is concurrent. If
|
||||
/// it is serial, no concurrent database access can happen, and you may
|
||||
/// experience deadlocks.
|
||||
/// Database connections which are not read-only will prefer
|
||||
/// `writeTargetQueue` instead, if it is not nil.
|
||||
///
|
||||
/// When you use a database pool, make sure this queue is concurrent. This
|
||||
/// is because in a serial dispatch queue, no concurrent database access can
|
||||
/// happen, and you may experience deadlocks.
|
||||
///
|
||||
/// If the queue is nil, all database accesses happen in unspecified
|
||||
/// dispatch queues whose quality of service and label are determined by the
|
||||
/// `qos` and `label` Configuration properties.
|
||||
/// dispatch queues whose quality of service is determined by the
|
||||
/// `qos` property.
|
||||
///
|
||||
/// Default: nil
|
||||
public var targetQueue: DispatchQueue? = nil
|
||||
|
||||
/// The target queue for database connections which are not read-only.
|
||||
///
|
||||
/// If this queue is nil, writer connections are controlled by `targetQueue`.
|
||||
///
|
||||
/// Default: nil
|
||||
public var writeTargetQueue: DispatchQueue? = nil
|
||||
|
||||
// MARK: - Factory Configuration
|
||||
|
||||
/// Creates a factory configuration
|
||||
@@ -287,7 +297,15 @@ public struct Configuration {
|
||||
(self.label ?? defaultLabel) + (purpose.map { "." + $0 } ?? "")
|
||||
}
|
||||
|
||||
func makeDispatchQueue(label: String) -> DispatchQueue {
|
||||
func makeWriterDispatchQueue(label: String) -> DispatchQueue {
|
||||
if let targetQueue = writeTargetQueue ?? targetQueue {
|
||||
return DispatchQueue(label: label, target: targetQueue)
|
||||
} else {
|
||||
return DispatchQueue(label: label, qos: qos)
|
||||
}
|
||||
}
|
||||
|
||||
func makeReaderDispatchQueue(label: String) -> DispatchQueue {
|
||||
if let targetQueue = targetQueue {
|
||||
return DispatchQueue(label: label, target: targetQueue)
|
||||
} else {
|
||||
|
||||
@@ -336,7 +336,7 @@ extension DatabasePool: DatabaseReader {
|
||||
defaultLabel: "GRDB.DatabasePool",
|
||||
purpose: "asyncRead")
|
||||
configuration
|
||||
.makeDispatchQueue(label: label)
|
||||
.makeReaderDispatchQueue(label: label)
|
||||
.async {
|
||||
do {
|
||||
guard let readerPool = self.readerPool else {
|
||||
@@ -374,7 +374,7 @@ extension DatabasePool: DatabaseReader {
|
||||
defaultLabel: "GRDB.DatabasePool",
|
||||
purpose: "asyncRead")
|
||||
configuration
|
||||
.makeDispatchQueue(label: label)
|
||||
.makeReaderDispatchQueue(label: label)
|
||||
.async { [weak self] in
|
||||
guard let self = self else {
|
||||
value(nil)
|
||||
@@ -435,7 +435,7 @@ extension DatabasePool: DatabaseReader {
|
||||
defaultLabel: "GRDB.DatabasePool",
|
||||
purpose: "asyncUnsafeRead")
|
||||
configuration
|
||||
.makeDispatchQueue(label: label)
|
||||
.makeReaderDispatchQueue(label: label)
|
||||
.async {
|
||||
do {
|
||||
guard let readerPool = self.readerPool else {
|
||||
@@ -802,7 +802,7 @@ extension DatabasePool: DatabaseReader {
|
||||
observation: observation,
|
||||
writer: self,
|
||||
scheduler: scheduler,
|
||||
reduceQueue: configuration.makeDispatchQueue(label: reduceQueueLabel),
|
||||
reduceQueue: configuration.makeReaderDispatchQueue(label: reduceQueueLabel),
|
||||
onChange: onChange)
|
||||
|
||||
// Starting a concurrent observation means that we'll fetch the initial
|
||||
|
||||
@@ -382,7 +382,7 @@ extension DatabaseWriter {
|
||||
observation: observation,
|
||||
writer: self,
|
||||
scheduler: scheduler,
|
||||
reduceQueue: configuration.makeDispatchQueue(label: reduceQueueLabel),
|
||||
reduceQueue: configuration.makeWriterDispatchQueue(label: reduceQueueLabel),
|
||||
onChange: onChange)
|
||||
|
||||
if scheduler.immediateInitialValue() {
|
||||
|
||||
@@ -47,7 +47,11 @@ final class SerializedDatabase {
|
||||
path: path,
|
||||
description: identifier,
|
||||
configuration: config)
|
||||
self.queue = configuration.makeDispatchQueue(label: identifier)
|
||||
if config.readonly {
|
||||
self.queue = configuration.makeReaderDispatchQueue(label: identifier)
|
||||
} else {
|
||||
self.queue = configuration.makeWriterDispatchQueue(label: identifier)
|
||||
}
|
||||
SchedulingWatchdog.allowDatabase(db, onQueue: queue)
|
||||
try queue.sync {
|
||||
do {
|
||||
|
||||
@@ -432,6 +432,7 @@ public struct DatabaseMigrator {
|
||||
// just as the migrated database
|
||||
var tmpConfig = db.configuration
|
||||
tmpConfig.targetQueue = nil // Avoid deadlocks
|
||||
tmpConfig.writeTargetQueue = nil // Avoid deadlocks
|
||||
tmpConfig.label = "GRDB.DatabaseMigrator.temporary"
|
||||
|
||||
// Create the temporary database on disk, just in
|
||||
|
||||
@@ -994,6 +994,60 @@ class DatabasePoolConcurrencyTests: GRDBTestCase {
|
||||
waitForExpectations(timeout: 2, handler: nil)
|
||||
}
|
||||
|
||||
func testWriteTargetQueue() throws {
|
||||
guard #available(OSX 10.12, tvOS 10.0, *) else {
|
||||
throw XCTSkip("dispatchPrecondition(condition:) is not available")
|
||||
}
|
||||
|
||||
func test(targetQueue: DispatchQueue, writeTargetQueue: DispatchQueue) throws {
|
||||
dbConfiguration.targetQueue = targetQueue
|
||||
dbConfiguration.writeTargetQueue = writeTargetQueue
|
||||
let dbPool = try makeDatabasePool()
|
||||
try dbPool.write { _ in
|
||||
dispatchPrecondition(condition: .onQueue(writeTargetQueue))
|
||||
}
|
||||
try dbPool.read { _ in
|
||||
dispatchPrecondition(condition: .onQueue(targetQueue))
|
||||
}
|
||||
}
|
||||
|
||||
// background queue
|
||||
try test(targetQueue: .global(qos: .background), writeTargetQueue: DispatchQueue(label: "writer"))
|
||||
|
||||
// main queue
|
||||
let expectation = self.expectation(description: "main")
|
||||
DispatchQueue.global(qos: .default).async {
|
||||
try! test(targetQueue: .main, writeTargetQueue: .main)
|
||||
expectation.fulfill()
|
||||
}
|
||||
waitForExpectations(timeout: 2, handler: nil)
|
||||
}
|
||||
|
||||
func testWriteTargetQueueReadOnly() throws {
|
||||
guard #available(OSX 10.12, tvOS 10.0, *) else {
|
||||
throw XCTSkip("dispatchPrecondition(condition:) is not available")
|
||||
}
|
||||
|
||||
// Create a database before we perform read-only accesses
|
||||
_ = try makeDatabasePool(filename: "test")
|
||||
|
||||
func test(targetQueue: DispatchQueue, writeTargetQueue: DispatchQueue) throws {
|
||||
dbConfiguration.readonly = true
|
||||
dbConfiguration.targetQueue = targetQueue
|
||||
dbConfiguration.writeTargetQueue = writeTargetQueue // unused
|
||||
let dbPool = try makeDatabasePool(filename: "test")
|
||||
try dbPool.write { _ in
|
||||
// Not on writeTargetQueue because read-only
|
||||
dispatchPrecondition(condition: .onQueue(targetQueue))
|
||||
}
|
||||
try dbPool.read { _ in
|
||||
dispatchPrecondition(condition: .onQueue(targetQueue))
|
||||
}
|
||||
}
|
||||
|
||||
try test(targetQueue: .global(qos: .background), writeTargetQueue: DispatchQueue(label: "writer"))
|
||||
}
|
||||
|
||||
func testQoS() throws {
|
||||
guard #available(OSX 10.12, tvOS 10.0, *) else {
|
||||
throw XCTSkip("dispatchPrecondition(condition:) is not available")
|
||||
|
||||
@@ -152,6 +152,59 @@ class DatabaseQueueTests: GRDBTestCase {
|
||||
waitForExpectations(timeout: 1, handler: nil)
|
||||
}
|
||||
|
||||
func testWriteTargetQueue() throws {
|
||||
guard #available(OSX 10.12, tvOS 10.0, *) else {
|
||||
throw XCTSkip("dispatchPrecondition(condition:) is not available")
|
||||
}
|
||||
|
||||
func test(targetQueue: DispatchQueue, writeTargetQueue: DispatchQueue) throws {
|
||||
dbConfiguration.targetQueue = targetQueue // unused
|
||||
dbConfiguration.writeTargetQueue = writeTargetQueue
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.write { _ in
|
||||
dispatchPrecondition(condition: .onQueue(writeTargetQueue))
|
||||
}
|
||||
try dbQueue.read { _ in
|
||||
dispatchPrecondition(condition: .onQueue(writeTargetQueue))
|
||||
}
|
||||
}
|
||||
|
||||
// background queue
|
||||
try test(targetQueue: .global(qos: .background), writeTargetQueue: DispatchQueue(label: "writer"))
|
||||
|
||||
// main queue
|
||||
let expectation = self.expectation(description: "main")
|
||||
DispatchQueue.global(qos: .default).async {
|
||||
try! test(targetQueue: .main, writeTargetQueue: .main)
|
||||
expectation.fulfill()
|
||||
}
|
||||
waitForExpectations(timeout: 1, handler: nil)
|
||||
}
|
||||
|
||||
func testWriteTargetQueueReadOnly() throws {
|
||||
guard #available(OSX 10.12, tvOS 10.0, *) else {
|
||||
throw XCTSkip("dispatchPrecondition(condition:) is not available")
|
||||
}
|
||||
|
||||
// Create a database before we perform read-only accesses
|
||||
_ = try makeDatabaseQueue(filename: "test")
|
||||
|
||||
func test(targetQueue: DispatchQueue, writeTargetQueue: DispatchQueue) throws {
|
||||
dbConfiguration.readonly = true
|
||||
dbConfiguration.targetQueue = targetQueue
|
||||
dbConfiguration.writeTargetQueue = writeTargetQueue // unused
|
||||
let dbQueue = try makeDatabaseQueue(filename: "test")
|
||||
try dbQueue.write { _ in
|
||||
dispatchPrecondition(condition: .onQueue(targetQueue))
|
||||
}
|
||||
try dbQueue.read { _ in
|
||||
dispatchPrecondition(condition: .onQueue(targetQueue))
|
||||
}
|
||||
}
|
||||
|
||||
try test(targetQueue: .global(qos: .background), writeTargetQueue: DispatchQueue(label: "writer"))
|
||||
}
|
||||
|
||||
func testQoS() throws {
|
||||
guard #available(OSX 10.12, tvOS 10.0, *) else {
|
||||
throw XCTSkip("dispatchPrecondition(condition:) is not available")
|
||||
|
||||
Reference in new issue
Block a user