Skip to content
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

Fix nio crash #5

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,26 @@
"repositoryURL": "https://github.com/swift-aws/aws-sdk-swift.git",
"state": {
"branch": null,
"revision": "b66a8ad7f949ef80a503069603ad1abd240e7f7d",
"version": "2.0.2"
"revision": "bc557114b6c16f63780d35ac2c5c21bf5c9b8f12",
"version": "3.0.0"
}
},
{
"package": "AWSSDKSwiftCore",
"repositoryURL": "https://github.com/swift-aws/aws-sdk-swift-core.git",
"state": {
"branch": null,
"revision": "e1d6f9515f95dacd21758e033c27b15a715aeb21",
"version": "2.0.0-rc.3"
"revision": "254c1e3f85414bf0f529f47cc081be2bd0c12c1c",
"version": "3.0.1"
}
},
{
"package": "HexavilleFramework",
"repositoryURL": "https://github.com/noppoMan/HexavilleFramework.git",
"state": {
"branch": null,
"revision": "6edcc3a4e0f4a37e21846b817e27a4f26513d840",
"version": "1.0.0-rc.1"
"revision": "9430c81699849c065b41b1f6b74ef704fa64247b",
"version": "1.0.0-rc.3"
}
},
{
Expand All @@ -51,17 +51,17 @@
"repositoryURL": "https://github.com/apple/swift-nio.git",
"state": {
"branch": null,
"revision": "a20e129c22ad00a51c902dca54a5456f90664780",
"version": "1.12.0"
"revision": "ba7970fe396e8198b84c6c1b44b38a1d4e2eb6bd",
"version": "1.14.1"
}
},
{
"package": "swift-nio-ssl",
"repositoryURL": "https://github.com/apple/swift-nio-ssl.git",
"state": {
"branch": null,
"revision": "db16c3a90b101bb53b26a58867a344ad428072e0",
"version": "1.3.2"
"revision": "0f3999f3e3c359cc74480c292644c3419e44a12f",
"version": "1.4.0"
}
},
{
Expand All @@ -87,17 +87,17 @@
"repositoryURL": "https://github.com/jakeheis/SwiftCLI.git",
"state": {
"branch": null,
"revision": "fb076cba39c679da4e27813518d8860d8815a25b",
"version": "5.2.1"
"revision": "2f9325de7dcaa368ce5a3710b04d9df572725b14",
"version": "5.3.0"
}
},
{
"package": "SwiftyJSON",
"repositoryURL": "https://github.com/IBM-Swift/SwiftyJSON.git",
"state": {
"branch": null,
"revision": "693d2d28fe2f91aedf02c3754baade625fd97c46",
"version": "17.0.2"
"revision": "f2612ea3ac29996ae9601bdcb00cc1c29e26f104",
"version": "17.0.4"
}
}
]
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ let package = Package(
.executable(name: "dynamodb-session-store-example", targets: ["DynamodbSessionStoreExample"]),
],
dependencies: [
.package(url: "https://github.com/swift-aws/aws-sdk-swift.git", .upToNextMajor(from: "2.0.2")),
.package(url: "https://github.com/swift-aws/aws-sdk-swift.git", .upToNextMajor(from: "3.0.0")),
.package(url: "https://github.com/noppoMan/HexavilleFramework.git", .upToNextMajor(from: "1.0.0-rc.1"))
],
targets: [
Expand Down
86 changes: 82 additions & 4 deletions Sources/DynamodbSessionStore/DynamodbSessionStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,21 @@ public struct DynamodbSessionStore: SessionStoreProvider {

public func read(forKey: String) throws -> [String : Any]? {
let input = DynamoDB.GetItemInput(
key: ["session_id": DynamoDB.AttributeValue(s: forKey)],
consistentRead: true,
key: ["session_id": DynamoDB.AttributeValue(s: forKey)],
tableName: tableName
)
let result = try dynamodb.getItem(input)

let result: DynamoDB.GetItemOutput = try executeSync { done in
do {
try self.dynamodb.getItem(input).whenSuccess { response in
done(nil, response)
}
} catch {
done(error, nil)
}
}

guard let item = result.item?["value"], let jsonStr = item.s else {
throw DynamodbSessionStoreError.couldNotFindItem
}
Expand Down Expand Up @@ -57,15 +67,83 @@ public struct DynamodbSessionStore: SessionStoreProvider {
tableName: tableName
)

_ = try dynamodb.putItem(input)

try executeSyncWithoutReturnValue { done in
do {
try self.dynamodb.putItem(input).whenSuccess { response in
done(nil)
}
} catch {
done(error)
}
}
}

public func delete(forKey: String) throws {
let input = DynamoDB.DeleteItemInput(
key: ["session_id" : DynamoDB.AttributeValue(s: forKey)],
tableName: tableName
)
_ = try dynamodb.deleteItem(input)

try executeSyncWithoutReturnValue { done in
do {
try self.dynamodb.deleteItem(input).whenSuccess { response in
done(nil)
}
} catch {
done(error)
}
}
}
}


func executeSync<T>(_ fn: (@escaping (Error?, T?) -> Void) -> Void) throws -> T {
let group = DispatchGroup()
group.enter()

var _error: Error?
var _result: T?

fn { error, result in
if error != nil {
_error = error
return
}

_result = result

group.leave()
}

group.wait()

if let error = _error {
throw error
}

return _result!
}


func executeSyncWithoutReturnValue(_ fn: (@escaping (Error?) -> Void) -> Void) throws {
let group = DispatchGroup()
group.enter()

var _error: Error?

fn { error in
if error != nil {
_error = error
return
}

group.leave()
}

group.wait()

if let error = _error {
throw error
}
}
6 changes: 3 additions & 3 deletions Sources/DynamodbSessionStoreExample/main.swift
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import Foundation
import HexavilleFramework
import DynamodbSessionStore
import SwiftAWSDynamodb
import DynamoDB

let app = HexavilleFramework()

let session = SessionMiddleware(
cookieAttribute: CookieAttribute(expiration: 3600, httpOnly: true, secure: false),
store: DynamodbSessionStore(
tableName: ProcessInfo.processInfo.environment["DYNAMODB_SESSION_TABLE_NAME"] ?? "test-table",
dynamodb: Dynamodb()
dynamodb: DynamoDB()
)
)

Expand All @@ -22,7 +22,7 @@ app.use { req, context in

var router = Router()

router.use(.get, "/") { req, context in
router.use(.GET, "/") { req, context in
if let now = context.session?["now"] {
return Response(body: "current time is: \(now)")
} else {
Expand Down
18 changes: 9 additions & 9 deletions Sources/DynamodbSessionStoreTableManager/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ class CreateCommand: Command {
func execute() throws {
let dynamodb = DynamoDB(endpoint: endpoint.value)
let input = DynamoDB.CreateTableInput(
provisionedThroughput: DynamoDB.ProvisionedThroughput(
readCapacityUnits: Int64(readCapacityUnits.value ?? 10),
writeCapacityUnits: Int64(writeCapacityUnits.value ?? 10)
),
tableName: tableName.value,
attributeDefinitions: [
DynamoDB.AttributeDefinition(attributeName: "session_id", attributeType: .s),
],
keySchema: [
DynamoDB.KeySchemaElement(keyType: .hash, attributeName: "session_id")
]
DynamoDB.KeySchemaElement(attributeName: "session_id", keyType: .hash)
],
provisionedThroughput: DynamoDB.ProvisionedThroughput(
readCapacityUnits: Int64(readCapacityUnits.value ?? 10),
writeCapacityUnits: Int64(writeCapacityUnits.value ?? 10)
),
tableName: tableName.value
)

do {
Expand Down Expand Up @@ -76,8 +76,8 @@ class CreateCommand: Command {
print("Applying updateTimeToLive configuration to \(tableName.value)....")

let updateTimeToLiveInput = DynamoDB.UpdateTimeToLiveInput(
timeToLiveSpecification: timeToLiveSpecificationInput,
tableName: tableName.value
tableName: tableName.value,
timeToLiveSpecification: timeToLiveSpecificationInput
)

_ = try dynamodb.updateTimeToLive(updateTimeToLiveInput)
Expand Down