2 * Copyright (c) 2017 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
27 #import <Foundation/Foundation.h>
29 @interface RateLimiter
: NSObject
<NSSecureCoding
>
31 @
property (readonly
, nonatomic
, nonnull
) NSDictionary
*config
;
32 @
property (readonly
, nonatomic
) NSUInteger stateSize
;
33 @
property (readonly
, nonatomic
, nullable
) NSString
*assetType
;
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
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
;
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
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.
60 - (NSInteger
)judge
:(id _Nonnull
)obj at
:(NSDate
* _Nonnull
)time limitTime
:(NSDate
* _Nullable __autoreleasing
* _Nonnull
)limitTime
;
63 - (NSString
* _Nonnull
)diagnostics
;
64 + (BOOL
)supportsSecureCoding
;
67 // implement config loading from MobileAsset
71 #endif /* RateLimiter_h */
73 /* Annotated example plist
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">
81 <!-- Total item limit -->
82 <key>maxStateSize</key>
83 <integer>250</integer>
84 <!-- Throw away items after this many seconds -->
86 <integer>3600</integer>
87 <!-- Ignore everybody for this many seconds -->
88 <key>overloadDuration</key>
89 <integer>1800</integer>
90 <!-- Printable string for logs -->
93 <!-- Load config stored in this MobileAsset (ignored if inited with config or plist directly) -->
96 <!-- Use this property for AWD's topWriters metric -->
97 <key>topOffendersPropertyIndex</key>
100 <!-- Each property you want to ratelimit on must have its own group dictionary -->
104 <!-- The first group must be for the global bucket. It behaves identically otherwise -->
106 <string>global</string>
108 <integer>20</integer>
110 <integer>30</integer>
115 <!-- Your object must respond to this selector that takes no arguments by returning an NSString * -->
117 <string>UUID</string>
118 <!-- Buckets of this type hold at most this many tokens -->
121 <!-- Tokens replenish at 1 every this many seconds -->
123 <integer>600</integer>
124 <!-- Max of all empty bucket badnesses is returned to caller. See RateLimiterBadness enum -->