|
1 |
| -import HTTP |
2 |
| -import Cache |
3 | 1 | import Vapor
|
4 |
| -import Foundation |
5 | 2 |
|
6 |
| -public struct Rate { |
7 |
| - public enum Interval { |
8 |
| - case second |
9 |
| - case minute |
10 |
| - case hour |
11 |
| - case day |
12 |
| - } |
13 |
| - |
14 |
| - public let limit: Int |
15 |
| - public let interval: Interval |
16 |
| - |
17 |
| - public init(_ limit: Int, per interval: Interval) { |
18 |
| - self.limit = limit |
19 |
| - self.interval = interval |
20 |
| - } |
21 |
| - |
22 |
| - internal var refreshInterval: Double { |
23 |
| - switch interval { |
24 |
| - case .second: |
25 |
| - return 1 |
26 |
| - case .minute: |
27 |
| - return 60 |
28 |
| - case .hour: |
29 |
| - return 3_600 |
30 |
| - case .day: |
31 |
| - return 86_400 |
32 |
| - } |
33 |
| - } |
34 |
| -} |
| 3 | +public struct Gatekeeper: Service { |
| 4 | + |
| 5 | + internal let config: GatekeeperConfig |
| 6 | + internal let cacheFactory: ((Container) throws -> KeyedCache) |
35 | 7 |
|
36 |
| -public struct Gatekeeper: Middleware { |
37 |
| - internal var cache: CacheProtocol |
38 |
| - |
39 |
| - internal let limit: Int |
40 |
| - internal let refreshInterval: Double |
41 |
| - |
42 |
| - public init(rate: Rate, cache: CacheProtocol = MemoryCache()) { |
43 |
| - self.cache = cache |
44 |
| - self.limit = rate.limit |
45 |
| - self.refreshInterval = rate.refreshInterval |
| 8 | + public init( |
| 9 | + config: GatekeeperConfig, |
| 10 | + cacheFactory: @escaping ((Container) throws -> KeyedCache) = { container in try container.make() } |
| 11 | + ) { |
| 12 | + self.config = config |
| 13 | + self.cacheFactory = cacheFactory |
46 | 14 | }
|
47 |
| - |
48 |
| - public func respond(to request: Request, chainingTo next: Responder) throws -> Response { |
49 |
| - guard let peer = request.peerHostname else { |
| 15 | + |
| 16 | + public func accessEndpoint( |
| 17 | + on request: Request |
| 18 | + ) throws -> Future<Gatekeeper.Entry> { |
| 19 | + |
| 20 | + guard let peerHostName = request.http.remotePeer.hostname else { |
50 | 21 | throw Abort(
|
51 | 22 | .forbidden,
|
52 |
| - metadata: nil, |
53 |
| - reason: "Unable to verify peer." |
| 23 | + reason: "Unable to verify peer" |
54 | 24 | )
|
55 | 25 | }
|
56 |
| - |
57 |
| - var entry = try cache.get(peer) |
58 |
| - var createdAt = entry?["createdAt"]?.double ?? Date().timeIntervalSince1970 |
59 |
| - var requestsLeft = entry?["requestsLeft"]?.int ?? limit |
60 |
| - |
61 |
| - let now = Date().timeIntervalSince1970 |
62 |
| - if now - createdAt >= refreshInterval { |
63 |
| - createdAt = now |
64 |
| - requestsLeft = limit |
65 |
| - } |
66 |
| - |
67 |
| - defer { |
68 |
| - do { |
69 |
| - try cache.set(peer, Node(node: [ |
70 |
| - "createdAt": createdAt, |
71 |
| - "requestsLeft": requestsLeft |
72 |
| - ])) |
73 |
| - } catch { |
74 |
| - print("WARNING: cache failed: \(error)") |
| 26 | + |
| 27 | + let peerCacheKey = cacheKey(for: peerHostName) |
| 28 | + let cache = try cacheFactory(request) |
| 29 | + |
| 30 | + return cache.get(peerCacheKey, as: Entry.self) |
| 31 | + .map(to: Entry.self) { entry in |
| 32 | + if let entry = entry { |
| 33 | + return entry |
| 34 | + } else { |
| 35 | + return Entry( |
| 36 | + peerHostname: peerHostName, |
| 37 | + createdAt: Date(), |
| 38 | + requestsLeft: self.config.limit |
| 39 | + ) |
| 40 | + } |
75 | 41 | }
|
76 |
| - } |
77 |
| - |
78 |
| - requestsLeft -= 1 |
79 |
| - guard requestsLeft >= 0 else { |
80 |
| - throw Abort( |
81 |
| - .tooManyRequests, |
82 |
| - metadata: nil, |
83 |
| - reason: "Slow down." |
84 |
| - ) |
85 |
| - } |
86 |
| - |
87 |
| - let response = try next.respond(to: request) |
88 |
| - return response |
| 42 | + .map(to: Entry.self) { entry in |
| 43 | + |
| 44 | + let now = Date() |
| 45 | + var mutableEntry = entry |
| 46 | + if now.timeIntervalSince1970 - entry.createdAt.timeIntervalSince1970 >= self.config.refreshInterval { |
| 47 | + mutableEntry.createdAt = now |
| 48 | + mutableEntry.requestsLeft = self.config.limit |
| 49 | + } |
| 50 | + mutableEntry.requestsLeft -= 1 |
| 51 | + return mutableEntry |
| 52 | + }.then { entry in |
| 53 | + return cache.set(peerCacheKey, to: entry).transform(to: entry) |
| 54 | + }.map(to: Entry.self) { entry in |
| 55 | + |
| 56 | + if entry.requestsLeft <= 0 { |
| 57 | + throw Abort( |
| 58 | + .tooManyRequests, |
| 59 | + reason: "Patience you must have, my young Padawan." |
| 60 | + ) |
| 61 | + } |
| 62 | + return entry |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + private func cacheKey(for hostname: String) -> String { |
| 67 | + return "gatekeeper_\(hostname)" |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +extension Gatekeeper { |
| 72 | + public struct Entry: Codable { |
| 73 | + let peerHostname: String |
| 74 | + var createdAt: Date |
| 75 | + var requestsLeft: Int |
89 | 76 | }
|
90 | 77 | }
|
0 commit comments