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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
 * Copyright (C) 2021  Aravinth Manivannan <realaravinth@batsense.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
//! Leaky bucket algorithm is implemantation for mcatpcha using batch processing Everytime count
//! is increased for an mcaptcha object, a decrement job is added to a batch that is scheduled to
//! be executed at that mcaptcha object's expiry rate(MCaptcha.get_duration())
use std::collections::HashMap;
use std::time::Duration;

use redis_module::key::RedisKeyWritable;
use redis_module::native_types::RedisType;
use redis_module::raw::KeyType;
use redis_module::{raw, Context};
use redis_module::{NotifyEvent, RedisString};
use serde::{Deserialize, Serialize};

use crate::errors::*;
use crate::mcaptcha::MCaptcha;
use crate::utils::*;
use crate::*;

/// Bucket type version, aka encoding version
const REDIS_MCAPTCHA_BUCKET_TYPE_VERSION: i32 = 0;

#[derive(Debug, PartialEq)]
/// encoding formats for persistence
pub enum Format {
    Json,
}

impl Format {
    #[inline]
    pub fn parse_str<'a, T: Deserialize<'a>>(&self, data: &'a str) -> CacheResult<T> {
        match self {
            Format::Json => Ok(serde_json::from_str(data)?),
        }
    }

    #[inline]
    pub fn from_str<'a, T: Deserialize<'a>>(&self, data: &'a str) -> CacheResult<T> {
        let res = self.parse_str(data)?;
        Ok(res)
    }
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Bucket {
    /// timer ID
    timer: u64,
    /// instant(seconds from UNIX_EPOCH) at which time bucket begins decrement process
    bucket_instant: u64,
    /// a list of captcha keys that should be decremented during clean up
    decrement: HashMap<String, u32>,
}

impl Bucket {
    /// Run when bucket timer expired at BUCKET_EXPIRY_OFFSET. Runs scheduled jobs in corresponding
    /// if they haven't already executed
    pub fn on_delete(ctx: &Context, _event_type: NotifyEvent, _event: &str, key_name: &str) {
        // TODO: this callback is executed after the bucket is deleted. So all jobs scheduled within
        // the bucket are lost. This means, we could end up with stagnent increments in mcaptcha objects
        // Rather than setting a timer, use a safety, upon who's expiry, the bucket's callback(job
        // runner) will be executed
        if !is_bucket_timer(key_name) {
            return;
        }

        let bucket_name = get_bucket_name_from_timer_name(key_name);
        if bucket_name.is_none() {
            return;
        }

        let bucket_name = bucket_name.unwrap();

        let bucket = ctx.open_key_writable(&RedisString::create(ctx.ctx, bucket_name));
        if bucket.key_type() == KeyType::Empty {
            ctx.log_debug(&format!("Bucket doesn't exist: {}", key_name));
        } else {
            Bucket::decrement_runner(ctx, &bucket);
        }
    }

    /// creates new bucket and sets off timer to go off at `duration`
    #[inline]
    fn new(ctx: &Context, duration: u64) -> CacheResult<Self> {
        let decrement = HashMap::with_capacity(HIT_PER_SECOND);

        let bucket_instant = get_bucket_instant(duration)?;
        let timer = ctx.create_timer(
            Duration::from_secs(duration),
            Self::decrement,
            bucket_instant,
        );

        let bucket = Bucket {
            timer,
            bucket_instant,
            decrement,
        };
        Ok(bucket)
    }

    /// decrement runner that decrements all registered counts _without_ cleaning after itself
    /// use [decrement] when you require auto cleanup. Internally, it calls this method.
    #[inline]
    fn decrement_runner(ctx: &Context, key: &RedisKeyWritable) {
        match key.get_value::<Bucket>(&MCAPTCHA_BUCKET_TYPE) {
            Ok(Some(bucket)) => {
                ctx.log_debug("entering loop hashmap");
                for (captcha, count) in bucket.decrement.drain() {
                    ctx.log_debug(&format!(
                        "reading captcha: {} with decr count {}",
                        &captcha, count
                    ));
                    let stored_captcha =
                        ctx.open_key_writable(&RedisString::create(ctx.ctx, &captcha));
                    if stored_captcha.key_type() == KeyType::Empty {
                        continue;
                    }
                    if let Ok(Some(captcha)) = MCaptcha::get_mut_mcaptcha(&stored_captcha) {
                        captcha.decrement_visitor_by(count);
                    }
                }
            }
            _ => {
                ctx.log_debug("bucket not found, can't decrement");
            }
        }
    }

    /// executes when timer goes off. Decrements all registered counts and cleans itself up
    fn decrement(ctx: &Context, bucket_instant: u64) {
        // get  bucket
        let bucket_name = get_bucket_name(bucket_instant);

        let timer = ctx.open_key_writable(&RedisString::create(
            ctx.ctx,
            &get_timer_name_from_bucket_name(&bucket_name),
        ));
        let _ = timer.delete();

        ctx.log_debug(&format!("Bucket instant: {}", &bucket_instant));

        let bucket = ctx.open_key_writable(&RedisString::create(ctx.ctx, &bucket_name));
        Bucket::decrement_runner(ctx, &bucket);

        if let Err(e) = bucket.delete() {
            ctx.log_warning(&format!("enountered error while deleting hashmap: {:?}", e));
        }

        let timer = ctx.open_key_writable(&RedisString::create(
            ctx.ctx,
            &get_timer_name_from_bucket_name(&bucket_name),
        ));
        if let Err(e) = timer.delete() {
            ctx.log_warning(&format!(
                "enountered error while deleting bucket tiemr: {:?}",
                e
            ));
        }
    }

    /// increments count of key = captcha and registers for auto decrement
    #[inline]
    fn increment(ctx: &Context, captcha: &str) -> CacheResult<String> {
        let captcha_name = get_captcha_key(&captcha);
        //        ctx.log_debug(&captcha_name);
        // increment
        let captcha = ctx.open_key_writable(&RedisString::create(ctx.ctx, &captcha_name));
        ctx.log_debug("loading mcaptcha");
        let captcha = MCaptcha::get_mut_mcaptcha(&captcha)?;

        ctx.log_debug("loaded mcaptcha");
        if captcha.is_none() {
            return Err(CacheError::new("Captcha not found".into()));
        }
        let captcha = captcha.unwrap();
        ctx.log_debug(&format!(
            "current visitor count: {}",
            captcha.get_visitors()
        ));
        captcha.add_visitor();
        let res = captcha.get_add_visitor_result();
        let res = serde_json::to_string(&res)?;

        ctx.log_debug("visitor added");
        let duration = captcha.get_duration();

        Self::increment_by(ctx, (captcha_name, duration), 1)?;

        Ok(res)
    }

    /// open bucket, set decrement by specified number
    pub fn increment_by(
        ctx: &Context,
        (captcha_name, duration): (String, u64),
        increment_by: u32,
    ) -> CacheResult<()> {
        let bucket_instant = get_bucket_instant(duration)?;
        let bucket_name = get_bucket_name(bucket_instant);

        //        ctx.log_debug(&format!("Bucket name: {}", &bucket_name));

        // get  bucket
        let bucket = ctx.open_key_writable(&RedisString::create(ctx.ctx, &bucket_name));

        match bucket.get_value::<Bucket>(&MCAPTCHA_BUCKET_TYPE)? {
            Some(bucket) => match bucket.decrement.get_mut(&captcha_name) {
                Some(count) => *count += increment_by,
                None => {
                    bucket.decrement.insert(captcha_name, 1);
                }
            },

            None => {
                let mut counter = Bucket::new(ctx, duration)?;
                counter.decrement.insert(captcha_name, 1);
                bucket.set_value(&MCAPTCHA_BUCKET_TYPE, counter)?;
                let timer = ctx.open_key_writable(&RedisString::create(
                    ctx.ctx,
                    &get_timer_name_from_bucket_name(&bucket_name),
                ));
                timer.write("1")?;
                timer.set_expire(Duration::from_secs(duration + BUCKET_EXPIRY_OFFSET))?;
            }
        };

        Ok(())
    }

    /// Create new counter
    pub fn counter_create(ctx: &Context, args: Vec<RedisString>) -> RedisResult {
        let mut args = args.into_iter().skip(1);
        // mcaptcha captcha key name
        let key_name = args.next_string()?;
        // expiry
        let res = Self::increment(ctx, &key_name)?;
        Ok(res.into())
    }
}

pub static MCAPTCHA_BUCKET_TYPE: RedisType = RedisType::new(
    "mcaptbuck",
    REDIS_MCAPTCHA_BUCKET_TYPE_VERSION,
    raw::RedisModuleTypeMethods {
        version: raw::REDISMODULE_TYPE_METHOD_VERSION as u64,
        rdb_load: Some(type_methods::rdb_load),
        rdb_save: Some(type_methods::rdb_save),
        aof_rewrite: None,
        free: Some(type_methods::free),

        // Currently unused by Redis
        mem_usage: None,
        digest: None,

        // Aux data
        aux_load: None,
        aux_save: None,
        aux_save_triggers: 0,

        free_effort: None,
        unlink: None,
        copy: None,
        defrag: None,
    },
);

pub mod type_methods {
    use std::os::raw::c_void;

    use libc::c_int;

    use super::*;

    #[allow(non_snake_case, unused)]
    pub extern "C" fn rdb_load(rdb: *mut raw::RedisModuleIO, encver: c_int) -> *mut c_void {
        let bucket = match encver {
            0 => {
                let data = raw::load_string(rdb).unwrap().to_string();
                let bucket: Bucket = Format::Json.from_str(&data).unwrap();
                bucket
            }
            _ => panic!("Can't load bucket from old redis RDB, encver: {}", encver,),
        };

        //        if bucket.
        Box::into_raw(Box::new(bucket)) as *mut c_void
    }

    pub unsafe extern "C" fn free(value: *mut c_void) {
        let val = value as *mut Bucket;
        Box::from_raw(val);
    }

    #[allow(non_snake_case, unused)]
    pub unsafe extern "C" fn rdb_save(rdb: *mut raw::RedisModuleIO, value: *mut c_void) {
        let bucket = &*(value as *mut Bucket);
        match &serde_json::to_string(bucket) {
            Ok(string) => raw::save_string(rdb, string),
            Err(e) => eprintln!("error while rdb_save: {}", e),
        }
    }
}