1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
use crate::prelude::*;
use core::cmp::min;
use core::fmt::{self, Debug, Formatter};
#[derive(Clone)]
pub struct VelocityControl {
pub start_sec: u64,
pub bucket_interval: u32,
pub buckets: Vec<u64>,
pub limit: u64,
}
impl Debug for VelocityControl {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
f.debug_struct("VelocityControl")
.field("start_sec", &self.start_sec)
.field("bucket_interval", &self.bucket_interval)
.field("buckets", &format_args!("{:?}", self.buckets))
.field("limit", &self.limit)
.finish()
}
}
#[derive(Clone, Copy, Debug)]
pub enum VelocityControlIntervalType {
Hourly,
Daily,
Unlimited,
}
#[derive(Clone, Copy, Debug)]
pub struct VelocityControlSpec {
pub limit: u64,
pub interval_type: VelocityControlIntervalType,
}
impl VelocityControlSpec {
pub const UNLIMITED: VelocityControlSpec =
VelocityControlSpec { limit: 0, interval_type: VelocityControlIntervalType::Unlimited };
}
impl VelocityControl {
pub fn new_with_intervals(limit: u64, bucket_interval: u32, num_buckets: usize) -> Self {
assert!(bucket_interval > 0 && num_buckets > 0);
let mut buckets = Vec::new();
buckets.resize(num_buckets, 0);
VelocityControl { start_sec: 0, bucket_interval, buckets, limit }
}
pub fn new_unlimited(bucket_interval: u32, num_buckets: usize) -> Self {
assert!(bucket_interval > 0 && num_buckets > 0);
let mut buckets = Vec::new();
buckets.resize(num_buckets, 0);
VelocityControl { start_sec: 0, bucket_interval, buckets, limit: u64::MAX }
}
pub fn new(spec: VelocityControlSpec) -> Self {
match spec.interval_type {
VelocityControlIntervalType::Hourly => Self::new_with_intervals(spec.limit, 300, 12),
VelocityControlIntervalType::Daily => Self::new_with_intervals(spec.limit, 3600, 24),
VelocityControlIntervalType::Unlimited => Self::new_unlimited(300, 12),
}
}
pub fn load_from_state(spec: VelocityControlSpec, state: (u64, Vec<u64>)) -> Self {
let control = Self::new(spec);
control.with_state(state)
}
fn with_state(mut self, state: (u64, Vec<u64>)) -> VelocityControl {
self.start_sec = state.0;
self.buckets = state.1;
self
}
pub fn get_state(&self) -> (u64, Vec<u64>) {
(self.start_sec, self.buckets.clone())
}
pub fn is_unlimited(&self) -> bool {
self.limit == u64::MAX
}
pub fn insert(&mut self, current_sec: u64, velocity: u64) -> bool {
let nshift = (current_sec - self.start_sec) / self.bucket_interval as u64;
let len = self.buckets.len();
let nshift = min(len, nshift as usize);
self.buckets.resize(len - nshift, 0);
for _ in 0..nshift {
self.buckets.insert(0, 0);
}
self.start_sec = current_sec - (current_sec % self.bucket_interval as u64);
let current_velocity = self.velocity();
if current_velocity.saturating_add(velocity) > self.limit {
false
} else {
self.buckets[0] = self.buckets[0].saturating_add(velocity);
true
}
}
pub fn clear(&mut self) {
for bucket in self.buckets.iter_mut() {
*bucket = 0;
}
}
pub fn velocity(&self) -> u64 {
let mut sum = 0u64;
for bucket in self.buckets.iter() {
sum = sum.saturating_add(*bucket)
}
sum
}
}
#[cfg(test)]
mod tests {
use crate::util::velocity::VelocityControl;
#[test]
fn test_velocity() {
let mut c = VelocityControl::new_with_intervals(100, 10, 4);
assert_eq!(c.velocity(), 0);
assert!(c.insert(1100, 90));
assert_eq!(c.velocity(), 90);
assert!(!c.insert(1101, 11));
assert_eq!(c.velocity(), 90);
assert!(c.insert(1101, 10));
assert_eq!(c.velocity(), 100);
assert!(!c.insert(1139, 90));
assert_eq!(c.velocity(), 100);
assert!(c.insert(1140, 90));
assert_eq!(c.velocity(), 90);
assert!(c.insert(1150, 5));
assert_eq!(c.velocity(), 95);
assert!(c.insert(1180, 80));
assert_eq!(c.velocity(), 85);
assert!(c.insert(1190, 1));
assert_eq!(c.velocity(), 81);
}
#[test]
fn test_unlimited() {
let mut c = VelocityControl::new_unlimited(10, 4);
assert!(c.insert(0, u64::MAX - 1));
assert_eq!(c.velocity(), u64::MAX - 1);
assert!(c.insert(0, 1));
assert_eq!(c.velocity(), u64::MAX);
assert!(c.insert(0, 1));
assert_eq!(c.velocity(), u64::MAX);
}
}