]> git.saurik.com Git - apple/security.git/blob - keychain/ckks/RateLimiter.h
Security-58286.20.16.tar.gz
[apple/security.git] / keychain / ckks / RateLimiter.h
1 /*
2 * Copyright (c) 2017 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 #ifndef RateLimiter_h
25 #define RateLimiter_h
26
27 #import <Foundation/Foundation.h>
28
29 @interface RateLimiter : NSObject <NSSecureCoding>
30
31 @property (readonly, nonatomic, nonnull) NSDictionary *config;
32 @property (readonly, nonatomic) NSUInteger stateSize;
33 @property (readonly, nonatomic, nullable) NSString *assetType;
34
35 typedef NS_ENUM(NSInteger, RateLimiterBadness) {
36 RateLimiterBadnessClear = 0, // everything is fine, process right now
37 RateLimiterBadnessCongested,
38 RateLimiterBadnessSeverelyCongested,
39 RateLimiterBadnessGridlocked,
40 RateLimiterBadnessOverloaded, // everything is on fire, go away
41 };
42
43 - (instancetype _Nullable)initWithConfig:(NSDictionary * _Nonnull)config;
44 - (instancetype _Nullable)initWithPlistFromURL:(NSURL * _Nonnull)url;
45 - (instancetype _Nullable)initWithAssetType:(NSString * _Nonnull)type; // Not implemented yet
46 - (instancetype _Nullable)initWithCoder:(NSCoder * _Nonnull)coder;
47 - (instancetype _Nullable)init NS_UNAVAILABLE;
48
49 /*!
50 * @brief Find out whether objects may be processed or must wait.
51 * @param obj The object being judged.
52 * @param time Current time.
53 * @param limitTime Assigned okay-to-process time. Nil when object may be processed immediately.
54 * @return RateLimiterBadness enum value indicating current congestion situation, or to signal
55 *
56 * judge:at: will set the limitTime object to nil in case of 0 badness. For badnesses 1-4 the time object will indicate when it is okay to send the entry.
57 * At badness 5 judge:at: has determined there is too much activity so the caller should hold off altogether. The limitTime object will indicate when
58 * this overloaded state will end.
59 */
60 - (NSInteger)judge:(id _Nonnull)obj at:(NSDate * _Nonnull)time limitTime:(NSDate * _Nullable __autoreleasing * _Nonnull)limitTime;
61
62 - (void)reset;
63 - (NSString * _Nonnull)diagnostics;
64 + (BOOL)supportsSecureCoding;
65
66 // TODO:
67 // implement config loading from MobileAsset
68
69 @end
70
71 #endif /* RateLimiter_h */
72
73 /* Annotated example plist
74
75 <?xml version="1.0" encoding="UTF-8"?>
76 <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
77 <plist version="1.0">
78 <dict>
79 <key>general</key>
80 <dict>
81 <!-- Total item limit -->
82 <key>maxStateSize</key>
83 <integer>250</integer>
84 <!-- Throw away items after this many seconds -->
85 <key>maxItemAge</key>
86 <integer>3600</integer>
87 <!-- Ignore everybody for this many seconds -->
88 <key>overloadDuration</key>
89 <integer>1800</integer>
90 <!-- Printable string for logs -->
91 <key>name</key>
92 <string>CKKS</string>
93 <!-- Load config stored in this MobileAsset (ignored if inited with config or plist directly) -->
94 <key>MAType</key>
95 <string></string>
96 <!-- Use this property for AWD's topWriters metric -->
97 <key>topOffendersPropertyIndex</key>
98 <integer></integer>
99 </dict>
100 <!-- Each property you want to ratelimit on must have its own group dictionary -->
101 <key>groups</key>
102 <array>
103 <dict>
104 <!-- The first group must be for the global bucket. It behaves identically otherwise -->
105 <key>property</key>
106 <string>global</string>
107 <key>capacity</key>
108 <integer>20</integer>
109 <key>rate</key>
110 <integer>30</integer>
111 <key>badness</key>
112 <integer>1</integer>
113 </dict>
114 <dict>
115 <!-- Your object must respond to this selector that takes no arguments by returning an NSString * -->
116 <key>property</key>
117 <string>UUID</string>
118 <!-- Buckets of this type hold at most this many tokens -->
119 <key>capacity</key>
120 <integer>3</integer>
121 <!-- Tokens replenish at 1 every this many seconds -->
122 <key>rate</key>
123 <integer>600</integer>
124 <!-- Max of all empty bucket badnesses is returned to caller. See RateLimiterBadness enum -->
125 <key>badness</key>
126 <integer>3</integer>
127 </dict>
128 </array>
129 </dict>
130 </plist>
131
132 */