Skip to content

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
merged 22 commits into from
Jul 19, 2016

Conversation

nickmshelley
Copy link
Collaborator

Most of this work was done in #411, which seems to have been abandoned, so I'm trying to get it through. Let me know if there's a better way than opening a new PR.

kdubb and others added 12 commits April 15, 2016 22:23
Uses WAL mode to support multiple reads and a single writer.
Allows customizing new connections once created
ConnectionType is now Connection and Connection is now DBConnection
vfs seems to be a more reliable handling of exclusive mode.
Was using incorrect dispatch_once token.
# Conflicts:
#	SQLiteTests/ConnectionPoolTests.swift
@nickmshelley nickmshelley mentioned this pull request Jun 16, 2016
@@ -151,44 +153,41 @@ public final class ConnectionPool {

var borrowed : BorrowedConnection!

repeat {
dispatch_semaphore_wait(connectionSemaphore, DISPATCH_TIME_FOREVER)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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 repeat loop and add this semaphore wait before the dispatch_sync call.

@nickmshelley
Copy link
Collaborator Author

I guess I'm going to have to do #457 first...

@nickmshelley
Copy link
Collaborator Author

Tests passed! Woot!

public protocol Connection {

/// Whether or not the database was opened in a read-only state.
var readonly : Bool { get }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No space before colons on property declarations.

@@ -138,7 +138,7 @@ extension Tokenizer : CustomStringConvertible {

}

extension Connection {
extension DirectConnection {

public func registerTokenizer(submoduleName: String, next: String -> (String, Range<String.Index>)?) throws {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can I use FTS with a connection pool? Because this extension is on DirectConnection, I'm inclined to think no.

@hiltonc
Copy link
Contributor

hiltonc commented Jun 28, 2016

Also, please update the documentation. It references Connection, which has been renamed, and it needs a section on using a connection pool.

private var unavailableReadConnections = [DirectConnection]()
private let lockQueue : dispatch_queue_t
private var writeConnection : DirectConnection!
private let connectionSemaphore = dispatch_semaphore_create(5)
Copy link
Contributor

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.

Copy link
Collaborator Author

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?

Copy link
Contributor

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.

@nickmshelley
Copy link
Collaborator Author

I'll update the source documentation, but should the user documentation coincide with the latest release or what's in master? I usually expect the former, in which case we wouldn't want to change any documentation yet (unless it's in a branch or something).

@hiltonc
Copy link
Contributor

hiltonc commented Jun 28, 2016

Is https://github.com/stephencelis/SQLite.swift/blob/master/Documentation/Index.md#sqliteswift-documentation what you mean by user documentation? That's part of the repo and should be kept in sync with the code.

@nickmshelley
Copy link
Collaborator Author

Yes that is what I mean. And I agree that it should be kept in sync with the code, but as a user I would want that kept in sync with the latest release of the code so I can understand how to use the library I'm consuming rather than an unreleased version. But I can just as easily do it now, just didn't want to confuse users.

What do you prefer @stephencelis?

@nickmshelley
Copy link
Collaborator Author

I changed the API so it's backwards-compatible (and I think also better than what it was, but I would love some feedback on it).

@nickmshelley
Copy link
Collaborator Author

I looked at the history of the documentation, and it looks like 77a0f8e updated the documentation along with the code, so I'll go ahead and update the docs (but I'd still love some feedback on my changes in the meantime).

@hiltonc
Copy link
Contributor

hiltonc commented Jul 19, 2016

Love the new API. 👍

@hiltonc
Copy link
Contributor

hiltonc commented Jul 19, 2016

👍 This looks great to me.

@nickmshelley nickmshelley merged commit 0997f34 into stephencelis:master Jul 19, 2016
@nickmshelley nickmshelley deleted the connection-pool branch July 19, 2016 20:12
public init(_ name: String) {
queue = dispatch_queue_create(name, DISPATCH_QUEUE_SERIAL)
queueContext = unsafeBitCast(queue, UnsafeMutablePointer<Void>.self)
dispatch_queue_set_specific(queue, ReentrantDispatcher.queueKey, queueContext, nil)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stephencelis: Sorry, looks like this (some part of the refactoring) broke the ability to use a single connection across multiple threads. I have a broken test ready, working on a fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants