Skip to content

Commit 41589e9

Browse files
committed
[Feature] Add RateLimiter support
1 parent 650c878 commit 41589e9

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

moxt/libmisc.cpp

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "libmisc.hpp"
2+
#include "common.hpp"
23
#include "libc.hpp"
34
#include "moxt/utils/floatutil.hpp"
45
#include "photon/common/callback.h"
@@ -26,10 +27,11 @@
2627
#include <execinfo.h>
2728
#include <iostream>
2829

30+
#include "moxt/utils/c_rate_limiter.hpp"
2931
#include "moxt/utils/cast.hpp"
32+
#include "moxt/utils/snowflake.hpp"
3033
#include <openssl/err.h>
3134
#include <openssl/opensslv.h>
32-
#include "moxt/utils/snowflake.hpp"
3335
#include <parallel_hashmap/phmap.h>
3436
#include <snmalloc/snmalloc.h>
3537

@@ -413,4 +415,19 @@ SEQ_FUNC bool seq_free_string_in_cache(int64_t key) {
413415
return true;
414416
}
415417
return false;
418+
}
419+
420+
// CRateLimiter
421+
SEQ_FUNC void *seq_new_crate_limiter(int maxCount, uint64_t windowSize) {
422+
return new CRateLimiter(maxCount, windowSize);
423+
}
424+
425+
SEQ_FUNC bool seq_crate_limiter_allow_and_record_request(void *ptr) {
426+
CRateLimiter *p = static_cast<CRateLimiter *>(ptr);
427+
return p->allowAndRecordRequest();
428+
}
429+
430+
SEQ_FUNC void seq_delete_crate_limiter(void *ptr) {
431+
CRateLimiter *p = static_cast<CRateLimiter *>(ptr);
432+
delete p;
416433
}

moxt/libmisc.hpp

+6
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,10 @@ SEQ_FUNC const char *seq_get_string_in_cache(int64_t key, size_t *resultLen);
1717

1818
SEQ_FUNC bool seq_free_string_in_cache(int64_t key);
1919

20+
SEQ_FUNC void *seq_new_crate_limiter(int maxCount, uint64_t windowSize);
21+
22+
SEQ_FUNC bool seq_crate_limiter_allow_and_record_request(void *ptr);
23+
24+
SEQ_FUNC void seq_delete_crate_limiter(void *ptr);
25+
2026
#endif

0 commit comments

Comments
 (0)