-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Implement connection pool. #451
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
nickmshelley
merged 22 commits into
stephencelis:master
from
nickmshelley:connection-pool
Jul 19, 2016
Merged
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
6881560
Add connection pool for concurrent access
kdubb 42ebd18
Add delegate to connection pool
kdubb 68f46d5
Better names for protocols and classes
kdubb 66b82b0
Use vfs for exclusivity
kdubb ccc20e5
Added alternate concurrency test
kdubb 18f612f
Fix imports in ConnectionPool.swift
kdubb 2a5908b
Rename `DBConnection` to `DirectConnection`
kdubb 9548d17
Replace pool delegate with setup closures
kdubb ffb9274
Fix writable connection initialization
kdubb b7a0eb1
Cleanup connection pool tests
kdubb 6df0272
Merge branch 'connection_pool' of https://github.com/reTXT/SQLite.swi…
nickmshelley 960309e
Implement connection limit.
nickmshelley 6052c31
Fix import problem for standalone.
nickmshelley e32db09
Shorten testConcurrentAccess2 time as it is making the travis tests t…
nickmshelley 2fd69b8
Tests are still taking too long.
nickmshelley 6d556b6
Merge branch 'master' into connection-pool
nickmshelley a688d18
Clean up some spacing.
nickmshelley a1121c4
Change ConnectionPool API and get rid of Connection protocol.
nickmshelley d2721ed
Merge branch 'master' into connection-pool
nickmshelley 476cdc5
Make sure every thread has at least one read.
nickmshelley c7db919
Add connection pool documentation.
nickmshelley 66b61dd
Use the right variable names.
nickmshelley File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,6 +39,7 @@ public final class ConnectionPool { | |
private var unavailableReadConnections = [DirectConnection]() | ||
private let lockQueue : dispatch_queue_t | ||
private var writeConnection : DirectConnection! | ||
private let connectionSemaphore = dispatch_semaphore_create(5) | ||
|
||
public var foreignKeys : Bool { | ||
get { | ||
|
@@ -91,6 +92,7 @@ public final class ConnectionPool { | |
self.pool.unavailableReadConnections.removeAtIndex(index) | ||
} | ||
self.pool.availableReadConnections.append(self.connection) | ||
dispatch_semaphore_signal(self.pool.connectionSemaphore) | ||
} | ||
} | ||
|
||
|
@@ -151,44 +153,41 @@ public final class ConnectionPool { | |
|
||
var borrowed : BorrowedConnection! | ||
|
||
repeat { | ||
dispatch_semaphore_wait(connectionSemaphore, DISPATCH_TIME_FOREVER) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The diff didn't come through very well on the changes below. All I did was remove the |
||
dispatch_sync(lockQueue) { | ||
|
||
dispatch_sync(lockQueue) { | ||
|
||
// Ensure database is open | ||
self.writable | ||
// Ensure database is open | ||
self.writable | ||
|
||
let connection : DirectConnection | ||
|
||
if let availableConnection = self.availableReadConnections.popLast() { | ||
connection = availableConnection | ||
} | ||
else { | ||
|
||
let connection : DirectConnection | ||
let flags = SQLITE_OPEN_READONLY | SQLITE_OPEN_WAL | SQLITE_OPEN_NOMUTEX | ||
|
||
if let availableConnection = self.availableReadConnections.popLast() { | ||
connection = availableConnection | ||
} | ||
else { | ||
|
||
let flags = SQLITE_OPEN_READONLY | SQLITE_OPEN_WAL | SQLITE_OPEN_NOMUTEX | ||
|
||
connection = try! DirectConnection(self.location, flags: flags, dispatcher: ImmediateDispatcher(), vfsName: vfsName) | ||
connection.busyTimeout = 2 | ||
connection = try! DirectConnection(self.location, flags: flags, dispatcher: ImmediateDispatcher(), vfsName: vfsName) | ||
connection.busyTimeout = 2 | ||
|
||
for (type, setupProcessor) in self.internalSetup { | ||
if type == .WriteAheadLogging { | ||
continue | ||
} | ||
try! setupProcessor(connection) | ||
} | ||
|
||
for setupProcessor in self.setup { | ||
try! setupProcessor(connection) | ||
for (type, setupProcessor) in self.internalSetup { | ||
if type == .WriteAheadLogging { | ||
continue | ||
} | ||
|
||
try! setupProcessor(connection) | ||
} | ||
|
||
self.unavailableReadConnections.append(connection) | ||
for setupProcessor in self.setup { | ||
try! setupProcessor(connection) | ||
} | ||
|
||
borrowed = BorrowedConnection(pool: self, connection: connection) | ||
} | ||
|
||
} while borrowed == nil | ||
self.unavailableReadConnections.append(connection) | ||
|
||
borrowed = BorrowedConnection(pool: self, connection: connection) | ||
} | ||
|
||
return borrowed | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a fine default, but should be configurable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we want this configurable?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because we do not know that 5 is the right connection pool size for every app.