Skip to content

Commit

Permalink
Some general cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
gwynne committed Feb 1, 2024
1 parent 6115ac9 commit f1bcf9c
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 22 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @gwynne
17 changes: 9 additions & 8 deletions [email protected]
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
// swift-tools-version:5.9
import PackageDescription

let swiftSettings: [SwiftSetting] = [
.enableUpcomingFeature("ForwardTrailingClosures"),
.enableUpcomingFeature("ExistentialAny"),
.enableUpcomingFeature("ConciseMagicFile"),
.enableUpcomingFeature("DisableOutwardActorInference"),
.enableExperimentalFeature("StrictConcurrency=complete"),
]

let package = Package(
name: "QueuesFluentDriver",
platforms: [
Expand Down Expand Up @@ -49,3 +41,12 @@ let package = Package(
),
]
)

var swiftSettings: [SwiftSetting] { [
.enableUpcomingFeature("ForwardTrailingClosures"),
.enableUpcomingFeature("ExistentialAny"),
.enableUpcomingFeature("ConciseMagicFile"),
.enableUpcomingFeature("DisableOutwardActorInference"),
.enableUpcomingFeature("StrictConcurrency"),
.enableExperimentalFeature("StrictConcurrency=complete"),
] }
2 changes: 1 addition & 1 deletion Sources/QueuesFluentDriver/FluentQueue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public struct FluentQueue: Queue, Sendable {
.where(SQLFunction("coalesce", args: JobModel.sqlColumn(\.$data.$delayUntil), SQLFunction("now")), .lessThanOrEqual, SQLFunction("now"))
.orderBy(JobModel.sqlColumn(\.$data.$delayUntil))
.limit(1)
.lockingClause(SQLSkipLocked.forUpdateSkipLocked)
.lockingClause(SQLLockingClauseWithSkipLocked.updateSkippingLocked)

if self.sqlDb.dialect.supportsReturning {
return try await self.sqlDb.update(JobModel.sqlTable)
Expand Down
32 changes: 19 additions & 13 deletions Sources/QueuesFluentDriver/SQLSkipLocked.swift
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
import SQLKit

enum SQLSkipLocked: SQLExpression {
case forUpdateSkipLocked
case forShareSkipLocked
enum SQLLockingClauseWithSkipLocked: SQLExpression {
/// Request an exclusive "writer" lock, skipping rows that are already locked.
case updateSkippingLocked

/// Request a shared "reader" lock, skipping rows that are already locked.
///
/// > Note: This is the "lightest" locking that is supported by both Postgres and MySQL.
case shareSkippingLocked

// See `SQLExpression.serialize(to:)`.
func serialize(to serializer: inout SQLSerializer) {
guard serializer.dialect.name != "sqlite" else {
return
}

switch self {
case .forUpdateSkipLocked:
serializer.write("FOR UPDATE SKIP LOCKED")
case .forShareSkipLocked:
// This is the "lightest" locking that is supported by both Postgres and Mysql
serializer.write("FOR SHARE SKIP LOCKED")
serializer.statement {
switch self {
case .updateSkippingLocked:
guard $0.dialect.exclusiveSelectLockExpression != nil else { return }
$0.append(SQLLockingClause.update)
case .shareSkippingLocked:
guard $0.dialect.sharedSelectLockExpression != nil else { return }
$0.append(SQLLockingClause.share)
}
$0.append("SKIP LOCKED")
}
}
}

0 comments on commit f1bcf9c

Please sign in to comment.