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@
24 #import <Foundation/Foundation.h>
26 NS_ASSUME_NONNULL_BEGIN
28 @interface RateLimiter
: NSObject
<NSSecureCoding
>
30 @
property (readonly
, nonatomic
) NSDictionary
* config
;
31 @
property (readonly
, nonatomic
) NSUInteger stateSize
;
32 @
property (readonly
, nonatomic
, nullable
) NSString
* assetType
;
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
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
;
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
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.
59 - (NSInteger
)judge
:(id
)obj at
:(NSDate
*)time limitTime
:(NSDate
* _Nonnull __autoreleasing
* _Nonnull
)limitTime
;
62 - (NSString
*)diagnostics
;
63 + (BOOL
)supportsSecureCoding
;
66 // implement config loading from MobileAsset
72 /* Annotated example plist
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">
80 <!-- Total item limit -->
81 <key>maxStateSize</key>
82 <integer>250</integer>
83 <!-- Throw away items after this many seconds -->
85 <integer>3600</integer>
86 <!-- Ignore everybody for this many seconds -->
87 <key>overloadDuration</key>
88 <integer>1800</integer>
89 <!-- Printable string for logs -->
92 <!-- Load config stored in this MobileAsset (ignored if inited with config or plist directly) -->
95 <!-- Use this property for AWD's topWriters metric -->
96 <key>topOffendersPropertyIndex</key>
99 <!-- Each property you want to ratelimit on must have its own group dictionary -->
103 <!-- The first group must be for the global bucket. It behaves identically otherwise -->
105 <string>global</string>
107 <integer>20</integer>
109 <integer>30</integer>
114 <!-- Your object must respond to this selector that takes no arguments by returning an NSString * -->
116 <string>UUID</string>
117 <!-- Buckets of this type hold at most this many tokens -->
120 <!-- Tokens replenish at 1 every this many seconds -->
122 <integer>600</integer>
123 <!-- Max of all empty bucket badnesses is returned to caller. See RateLimiterBadness enum -->