-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* cleanup + config improvements * require newer vapor tag * fix merge issue
- Loading branch information
1 parent
e531234
commit cec35a4
Showing
7 changed files
with
129 additions
and
152 deletions.
There are no files selected for viewing
This file contains 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
This file was deleted.
Oops, something went wrong.
This file contains 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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import AsyncKit | ||
import Logging | ||
|
||
public final class APNSConnectionSource: ConnectionPoolSource { | ||
private let configuration: APNSwiftConfiguration | ||
|
||
public init(configuration: APNSwiftConfiguration) { | ||
self.configuration = configuration | ||
} | ||
public func makeConnection( | ||
logger: Logger, | ||
on eventLoop: EventLoop | ||
) -> EventLoopFuture<APNSwiftConnection> { | ||
APNSwiftConnection.connect(configuration: self.configuration, on: eventLoop) | ||
} | ||
} | ||
|
||
extension APNSwiftConnection: ConnectionPoolItem { | ||
public var eventLoop: EventLoop { | ||
self.channel.eventLoop | ||
} | ||
|
||
public var isClosed: Bool { | ||
self.channel.isActive | ||
} | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import Vapor | ||
|
||
extension Application { | ||
public var apns: APNS { | ||
.init(application: self) | ||
} | ||
|
||
public struct APNS { | ||
struct ConfigurationKey: StorageKey { | ||
typealias Value = APNSwiftConfiguration | ||
} | ||
|
||
public var configuration: APNSwiftConfiguration? { | ||
get { | ||
self.application.storage[ConfigurationKey.self] | ||
} | ||
nonmutating set { | ||
self.application.storage[ConfigurationKey.self] = newValue | ||
} | ||
} | ||
|
||
|
||
struct PoolKey: StorageKey, LockKey { | ||
typealias Value = EventLoopGroupConnectionPool<APNSConnectionSource> | ||
} | ||
|
||
internal var pool: EventLoopGroupConnectionPool<APNSConnectionSource> { | ||
if let existing = self.application.storage[PoolKey.self] { | ||
return existing | ||
} else { | ||
let lock = self.application.locks.lock(for: PoolKey.self) | ||
lock.lock() | ||
defer { lock.unlock() } | ||
guard let configuration = self.configuration else { | ||
fatalError("APNS not configured. Use app.apns.configuration = ...") | ||
} | ||
let new = EventLoopGroupConnectionPool( | ||
source: APNSConnectionSource(configuration: configuration), | ||
maxConnectionsPerEventLoop: 1, | ||
logger: self.application.logger, | ||
on: self.application.eventLoopGroup | ||
) | ||
self.application.storage.set(PoolKey.self, to: new) { | ||
$0.shutdown() | ||
} | ||
return new | ||
} | ||
} | ||
|
||
let application: Application | ||
} | ||
} |
File renamed without changes.
This file contains 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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import Vapor | ||
|
||
extension Request { | ||
public var apns: APNS { | ||
.init(request: self) | ||
} | ||
|
||
public struct APNS { | ||
let request: Request | ||
} | ||
} | ||
|
||
extension Request.APNS: APNSwiftClient { | ||
public var logger: Logger? { | ||
self.request.logger | ||
} | ||
|
||
public var eventLoop: EventLoop { | ||
self.request.eventLoop | ||
} | ||
|
||
public func send( | ||
rawBytes payload: ByteBuffer, | ||
pushType: APNSwiftConnection.PushType, | ||
to deviceToken: String, | ||
expiration: Date?, | ||
priority: Int?, | ||
collapseIdentifier: String?, | ||
topic: String?, | ||
logger: Logger? | ||
) -> EventLoopFuture<Void> { | ||
self.request.application.apns.pool.withConnection( | ||
logger: logger, | ||
on: self.eventLoop | ||
) { | ||
$0.send( | ||
rawBytes: payload, | ||
pushType: pushType, | ||
to: deviceToken, | ||
expiration: expiration, | ||
priority: priority, | ||
collapseIdentifier: collapseIdentifier, | ||
topic: topic, | ||
logger: logger | ||
) | ||
} | ||
} | ||
} |
This file contains 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