]> git.saurik.com Git - apple/security.git/blob - keychain/ckks/RateLimiter.h
Security-58286.220.15.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 #import <Foundation/Foundation.h>
25
26 NS_ASSUME_NONNULL_BEGIN
27
28 @interface RateLimiter : NSObject <NSSecureCoding>
29
30 @property (readonly, nonatomic) NSDictionary* config;
31 @property (readonly, nonatomic) NSUInteger stateSize;
32 @property (readonly, nonatomic, nullable) NSString* assetType;
33
34 typedef NS_ENUM(NSInteger, RateLimiterBadness) {
35 RateLimiterBadnessClear = 0, // everything is fine, process right now
36 RateLimiterBadnessCongested,
37 RateLimiterBadnessSeverelyCongested,
38 RateLimiterBadnessGridlocked,
39 RateLimiterBadnessOverloaded, // everything is on fire, go away
40 };
41
42 - (instancetype _Nullable)initWithConfig:(NSDictionary*)config;
43 - (instancetype _Nullable)initWithPlistFromURL:(NSURL*)url;
44 - (instancetype _Nullable)initWithAssetType:(NSString*)type; // Not implemented yet
45 - (instancetype _Nullable)initWithCoder:(NSCoder*)coder;
46 - (instancetype _Nullable)init NS_UNAVAILABLE;
47
48 /*!
49 * @brief Find out whether objects may be processed or must wait.
50 * @param obj The object being judged.
51 * @param time Current time.
52 * @param limitTime Assigned okay-to-process time. Nil when object may be processed immediately.
53 * @return RateLimiterBadness enum value indicating current congestion situation, or to signal
54 *
55 * 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.
56 * 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
57 * this overloaded state will end.
58 */
59 - (NSInteger)judge:(id)obj at:(NSDate*)time limitTime:(NSDate* _Nonnull __autoreleasing* _Nonnull)limitTime;
60
61 - (void)reset;
62 - (NSString*)diagnostics;
63 + (BOOL)supportsSecureCoding;
64
65 // TODO:
66 // implement config loading from MobileAsset
67
68 @end
69
70 NS_ASSUME_NONNULL_END
71
72 /* Annotated example plist
73
74 <?xml version="1.0" encoding="UTF-8"?>
75 <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
76 <plist version="1.0">
77 <dict>
78 <key>general</key>
79 <dict>
80 <!-- Total item limit -->
81 <key>maxStateSize</key>
82 <integer>250</integer>
83 <!-- Throw away items after this many seconds -->
84 <key>maxItemAge</key>
85 <integer>3600</integer>
86 <!-- Ignore everybody for this many seconds -->
87 <key>overloadDuration</key>
88 <integer>1800</integer>
89 <!-- Printable string for logs -->
90 <key>name</key>
91 <string>CKKS</string>
92 <!-- Load config stored in this MobileAsset (ignored if inited with config or plist directly) -->
93 <key>MAType</key>
94 <string></string>
95 </dict>
96 <!-- Each property you want to ratelimit on must have its own group dictionary -->
97 <key>groups</key>
98 <array>
99 <dict>
100 <!-- The first group must be for the global bucket. It behaves identically otherwise -->
101 <key>property</key>
102 <string>global</string>
103 <key>capacity</key>
104 <integer>20</integer>
105 <key>rate</key>
106 <integer>30</integer>
107 <key>badness</key>
108 <integer>1</integer>
109 </dict>
110 <dict>
111 <!-- Your object must respond to this selector that takes no arguments by returning an NSString * -->
112 <key>property</key>
113 <string>UUID</string>
114 <!-- Buckets of this type hold at most this many tokens -->
115 <key>capacity</key>
116 <integer>3</integer>
117 <!-- Tokens replenish at 1 every this many seconds -->
118 <key>rate</key>
119 <integer>600</integer>
120 <!-- Max of all empty bucket badnesses is returned to caller. See RateLimiterBadness enum -->
121 <key>badness</key>
122 <integer>3</integer>
123 </dict>
124 </array>
125 </dict>
126 </plist>
127
128 */