From f5f820c9fdf8a775530b4070e2826df90131e430 Mon Sep 17 00:00:00 2001 From: noppoman Date: Thu, 9 May 2019 01:02:06 +0900 Subject: [PATCH 1/4] bump aws-sdk swift version --- Package.resolved | 28 +++++++++---------- .../DynamodbSessionStore.swift | 2 +- .../DynamodbSessionStoreExample/main.swift | 6 ++-- .../main.swift | 18 ++++++------ 4 files changed, 27 insertions(+), 27 deletions(-) diff --git a/Package.resolved b/Package.resolved index 351b90a..fcf113b 100644 --- a/Package.resolved +++ b/Package.resolved @@ -6,8 +6,8 @@ "repositoryURL": "https://github.com/swift-aws/aws-sdk-swift.git", "state": { "branch": null, - "revision": "b66a8ad7f949ef80a503069603ad1abd240e7f7d", - "version": "2.0.2" + "revision": "0cffd66a61eff373aed70868a9ff6f720bfd69c2", + "version": "2.0.5" } }, { @@ -15,8 +15,8 @@ "repositoryURL": "https://github.com/swift-aws/aws-sdk-swift-core.git", "state": { "branch": null, - "revision": "e1d6f9515f95dacd21758e033c27b15a715aeb21", - "version": "2.0.0-rc.3" + "revision": "c62ee0c49909345e437d71028dc63dca8e3b1f9b", + "version": "2.0.1" } }, { @@ -24,8 +24,8 @@ "repositoryURL": "https://github.com/noppoMan/HexavilleFramework.git", "state": { "branch": null, - "revision": "6edcc3a4e0f4a37e21846b817e27a4f26513d840", - "version": "1.0.0-rc.1" + "revision": "6c836fb1173a2ee70b0b7fbb4995758b685743af", + "version": "1.0.0-rc.2" } }, { @@ -51,8 +51,8 @@ "repositoryURL": "https://github.com/apple/swift-nio.git", "state": { "branch": null, - "revision": "a20e129c22ad00a51c902dca54a5456f90664780", - "version": "1.12.0" + "revision": "ba7970fe396e8198b84c6c1b44b38a1d4e2eb6bd", + "version": "1.14.1" } }, { @@ -60,8 +60,8 @@ "repositoryURL": "https://github.com/apple/swift-nio-ssl.git", "state": { "branch": null, - "revision": "db16c3a90b101bb53b26a58867a344ad428072e0", - "version": "1.3.2" + "revision": "0f3999f3e3c359cc74480c292644c3419e44a12f", + "version": "1.4.0" } }, { @@ -87,8 +87,8 @@ "repositoryURL": "https://github.com/jakeheis/SwiftCLI.git", "state": { "branch": null, - "revision": "fb076cba39c679da4e27813518d8860d8815a25b", - "version": "5.2.1" + "revision": "5318c37d3cacc8780f50b87a8840a6774320ebdf", + "version": "5.2.2" } }, { @@ -96,8 +96,8 @@ "repositoryURL": "https://github.com/IBM-Swift/SwiftyJSON.git", "state": { "branch": null, - "revision": "693d2d28fe2f91aedf02c3754baade625fd97c46", - "version": "17.0.2" + "revision": "f2612ea3ac29996ae9601bdcb00cc1c29e26f104", + "version": "17.0.4" } } ] diff --git a/Sources/DynamodbSessionStore/DynamodbSessionStore.swift b/Sources/DynamodbSessionStore/DynamodbSessionStore.swift index aaef821..4e736d2 100644 --- a/Sources/DynamodbSessionStore/DynamodbSessionStore.swift +++ b/Sources/DynamodbSessionStore/DynamodbSessionStore.swift @@ -24,8 +24,8 @@ 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) diff --git a/Sources/DynamodbSessionStoreExample/main.swift b/Sources/DynamodbSessionStoreExample/main.swift index 840c3bc..90766f5 100644 --- a/Sources/DynamodbSessionStoreExample/main.swift +++ b/Sources/DynamodbSessionStoreExample/main.swift @@ -1,7 +1,7 @@ import Foundation import HexavilleFramework import DynamodbSessionStore -import SwiftAWSDynamodb +import DynamoDB let app = HexavilleFramework() @@ -9,7 +9,7 @@ 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() ) ) @@ -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 { diff --git a/Sources/DynamodbSessionStoreTableManager/main.swift b/Sources/DynamodbSessionStoreTableManager/main.swift index a3690bd..28ec974 100644 --- a/Sources/DynamodbSessionStoreTableManager/main.swift +++ b/Sources/DynamodbSessionStoreTableManager/main.swift @@ -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 { @@ -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) From 1377a0a8c9d953671b5c5837e2758c0c66912fc7 Mon Sep 17 00:00:00 2001 From: noppoman Date: Thu, 9 May 2019 01:04:27 +0900 Subject: [PATCH 2/4] temporary avoid crash with nio --- .../DynamodbSessionStore.swift | 67 +++++++++++++++++-- 1 file changed, 63 insertions(+), 4 deletions(-) diff --git a/Sources/DynamodbSessionStore/DynamodbSessionStore.swift b/Sources/DynamodbSessionStore/DynamodbSessionStore.swift index 4e736d2..68be3bc 100644 --- a/Sources/DynamodbSessionStore/DynamodbSessionStore.swift +++ b/Sources/DynamodbSessionStore/DynamodbSessionStore.swift @@ -28,8 +28,29 @@ public struct DynamodbSessionStore: SessionStoreProvider { key: ["session_id": DynamoDB.AttributeValue(s: forKey)], tableName: tableName ) - let result = try dynamodb.getItem(input) - guard let item = result.item?["value"], let jsonStr = item.s else { + + let group = DispatchGroup() + group.enter() + + var _result: DynamoDB.GetItemOutput? + var _error: Error? + + DispatchQueue.global().async { + do { + _result = try self.dynamodb.getItem(input) + } catch { + _error = error + } + group.leave() + } + + group.wait() + + if let error = _error { + throw error + } + + guard let item = _result?.item?["value"], let jsonStr = item.s else { throw DynamodbSessionStoreError.couldNotFindItem } @@ -57,7 +78,25 @@ public struct DynamodbSessionStore: SessionStoreProvider { tableName: tableName ) - _ = try dynamodb.putItem(input) + let group = DispatchGroup() + group.enter() + + var _error: Error? + + DispatchQueue.global().async { + do { + _ = try self.dynamodb.putItem(input) + } catch { + _error = error + } + group.leave() + } + + group.wait() + + if let error = _error { + throw error + } } public func delete(forKey: String) throws { @@ -65,7 +104,27 @@ public struct DynamodbSessionStore: SessionStoreProvider { key: ["session_id" : DynamoDB.AttributeValue(s: forKey)], tableName: tableName ) - _ = try dynamodb.deleteItem(input) + + let group = DispatchGroup() + group.enter() + + var _error: Error? + + DispatchQueue.global().async { + do { + _ = try self.dynamodb.deleteItem(input) + } catch { + _error = error + } + group.leave() + } + + group.wait() + + if let error = _error { + throw error + } } } + From 7090c57c7ffba1a09b4d17a7f53ab98e7b564f47 Mon Sep 17 00:00:00 2001 From: noppoman Date: Thu, 6 Jun 2019 20:47:32 +0900 Subject: [PATCH 3/4] debug --- Sources/DynamodbSessionStore/DynamodbSessionStore.swift | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Sources/DynamodbSessionStore/DynamodbSessionStore.swift b/Sources/DynamodbSessionStore/DynamodbSessionStore.swift index 68be3bc..a05ccd0 100644 --- a/Sources/DynamodbSessionStore/DynamodbSessionStore.swift +++ b/Sources/DynamodbSessionStore/DynamodbSessionStore.swift @@ -67,6 +67,9 @@ public struct DynamodbSessionStore: SessionStoreProvider { "value": DynamoDB.AttributeValue(s: stringValue) ] + print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!") + print(item) + if let ttl = ttl { var date = Date() date.addTimeInterval(TimeInterval(ttl)) From 9aed9bd33b339f2ca5be3632d2e0f89c4a6d7efc Mon Sep 17 00:00:00 2001 From: noppoman Date: Thu, 13 Jun 2019 18:50:47 +0900 Subject: [PATCH 4/4] Bump aws-sdk-swift version to 3.0.0 --- Package.resolved | 16 +-- Package.swift | 2 +- .../DynamodbSessionStore.swift | 110 ++++++++++-------- 3 files changed, 72 insertions(+), 56 deletions(-) diff --git a/Package.resolved b/Package.resolved index fcf113b..6c425ce 100644 --- a/Package.resolved +++ b/Package.resolved @@ -6,8 +6,8 @@ "repositoryURL": "https://github.com/swift-aws/aws-sdk-swift.git", "state": { "branch": null, - "revision": "0cffd66a61eff373aed70868a9ff6f720bfd69c2", - "version": "2.0.5" + "revision": "bc557114b6c16f63780d35ac2c5c21bf5c9b8f12", + "version": "3.0.0" } }, { @@ -15,8 +15,8 @@ "repositoryURL": "https://github.com/swift-aws/aws-sdk-swift-core.git", "state": { "branch": null, - "revision": "c62ee0c49909345e437d71028dc63dca8e3b1f9b", - "version": "2.0.1" + "revision": "254c1e3f85414bf0f529f47cc081be2bd0c12c1c", + "version": "3.0.1" } }, { @@ -24,8 +24,8 @@ "repositoryURL": "https://github.com/noppoMan/HexavilleFramework.git", "state": { "branch": null, - "revision": "6c836fb1173a2ee70b0b7fbb4995758b685743af", - "version": "1.0.0-rc.2" + "revision": "9430c81699849c065b41b1f6b74ef704fa64247b", + "version": "1.0.0-rc.3" } }, { @@ -87,8 +87,8 @@ "repositoryURL": "https://github.com/jakeheis/SwiftCLI.git", "state": { "branch": null, - "revision": "5318c37d3cacc8780f50b87a8840a6774320ebdf", - "version": "5.2.2" + "revision": "2f9325de7dcaa368ce5a3710b04d9df572725b14", + "version": "5.3.0" } }, { diff --git a/Package.swift b/Package.swift index 5a14e0a..6e926b7 100644 --- a/Package.swift +++ b/Package.swift @@ -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: [ diff --git a/Sources/DynamodbSessionStore/DynamodbSessionStore.swift b/Sources/DynamodbSessionStore/DynamodbSessionStore.swift index a05ccd0..fbf6fe2 100644 --- a/Sources/DynamodbSessionStore/DynamodbSessionStore.swift +++ b/Sources/DynamodbSessionStore/DynamodbSessionStore.swift @@ -29,28 +29,17 @@ public struct DynamodbSessionStore: SessionStoreProvider { tableName: tableName ) - let group = DispatchGroup() - group.enter() - - var _result: DynamoDB.GetItemOutput? - var _error: Error? - - DispatchQueue.global().async { + let result: DynamoDB.GetItemOutput = try executeSync { done in do { - _result = try self.dynamodb.getItem(input) + try self.dynamodb.getItem(input).whenSuccess { response in + done(nil, response) + } } catch { - _error = error + done(error, nil) } - group.leave() - } - - group.wait() - - if let error = _error { - throw error } - guard let item = _result?.item?["value"], let jsonStr = item.s else { + guard let item = result.item?["value"], let jsonStr = item.s else { throw DynamodbSessionStoreError.couldNotFindItem } @@ -67,9 +56,6 @@ public struct DynamodbSessionStore: SessionStoreProvider { "value": DynamoDB.AttributeValue(s: stringValue) ] - print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!") - print(item) - if let ttl = ttl { var date = Date() date.addTimeInterval(TimeInterval(ttl)) @@ -81,24 +67,15 @@ public struct DynamodbSessionStore: SessionStoreProvider { tableName: tableName ) - let group = DispatchGroup() - group.enter() - - var _error: Error? - DispatchQueue.global().async { + try executeSyncWithoutReturnValue { done in do { - _ = try self.dynamodb.putItem(input) + try self.dynamodb.putItem(input).whenSuccess { response in + done(nil) + } } catch { - _error = error + done(error) } - group.leave() - } - - group.wait() - - if let error = _error { - throw error } } @@ -108,26 +85,65 @@ public struct DynamodbSessionStore: SessionStoreProvider { tableName: tableName ) - let group = DispatchGroup() - group.enter() - - var _error: Error? - - DispatchQueue.global().async { + try executeSyncWithoutReturnValue { done in do { - _ = try self.dynamodb.deleteItem(input) + try self.dynamodb.deleteItem(input).whenSuccess { response in + done(nil) + } } catch { - _error = error + done(error) } - group.leave() + } + } +} + + +func executeSync(_ 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 } - group.wait() + _result = result - if let error = _error { - throw error - } + 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 + } +}