💾 Archived View for gmi.noulin.net › gitRepositories › tuyau › file › rateLimiter › rateLimiter.h.gm… captured on 2023-07-10 at 18:14:01. Gemini links have been rewritten to link to archived content
⬅️ Previous capture (2023-01-29)
-=-=-=-=-=-=-
rateLimiter.h (3397B)
1 #pragma once 2 3 /* Add class methods and class data where there are the TODOs (TODO)*/ 4 5 #include "libsheepyObject.h" 6 #include "hashfunctions.h" 7 #include "hashtable.h" 8 9 #define setupG(obj, maxClients, window, maxAccessCount) (obj)->f->setup(obj, maxClients, window, maxAccessCount) 10 #define clientCountG(obj) (obj)->f->count(obj) 11 #define incomingG(obj, ip) (obj)->f->incoming(obj, ip) 12 13 /* TODO add generics: #define amethodG(obj) (obj)->f->amethod(obj) */ 14 15 /* Class rateLimiter */ 16 typ struct rateLimiter rateLimitert; 17 18 /* for object inheriting rateLimiter, cast to rateLimiter to be able to use this class functions and generics*/ 19 #define cRateLimiter(self) ( (rateLimitert*) self ) 20 21 typ struct { 22 u32 ip; 23 u32 count; 24 time_t time; 25 } rateLimterE; 26 27 hashTbDef(, rateL, rateL, u32, u32, u32); 28 29 typ void (*freeRateLimiterFt) (rateLimitert *self); 30 typ void (*terminateRateLimiterFt) (rateLimitert **self); 31 typ char* (*toStringRateLimiterFt) (rateLimitert *self); 32 typ rateLimitert* (*duplicateRateLimiterFt) (rateLimitert *self); 33 typ void (*smashRateLimiterFt) (rateLimitert **self); 34 35 /** 36 * free rateLimiter 37 */ 38 typ void (*finishRateLimiterFt) (rateLimitert **self); 39 40 typ bool (*setupRateLimiterFt) (rateLimitert *self, u32 maxClients, u64 window, u32 maxAccessCount); 41 typ size_t (*countRateLimiterFt) (rateLimitert *self); 42 typ bool (*incomingRateLimiterFt) (rateLimitert *self, u32 ip); 43 typ bool (*hasRateLimiterFt) (rateLimitert *self, u32 ip); 44 /* TODO add function typ with pattern: functionNameClassTempleFt */ 45 46 /** 47 * class functions 48 * allocated once for all objects 49 * 50 * freed with finalizeRateLimiter 51 */ 52 53 /** 54 * use this define in child classes and add the new function after this class functions 55 * 56 * in this define, add the methods after <finishRateLimiterFt finish;> 57 * 58 * Example: 59 * #define RINGFUNCTIONST \n * RATELIMITERFUNCTIONST; \n * setSizeRingFt setSize 60 */ 61 #define RATELIMITERFUNCTIONST \ 62 setupRateLimiterFt setup;\ 63 countRateLimiterFt count;\ 64 incomingRateLimiterFt incoming;\ 65 hasRateLimiterFt has 66 /* TODO ADD METHODS AFTER <finishRateLimiterFt finish;> HERE */ 67 68 typ struct { 69 freeRateLimiterFt free; 70 terminateRateLimiterFt terminate; 71 toStringRateLimiterFt toString; 72 duplicateRateLimiterFt duplicate; 73 smashRateLimiterFt smash; 74 finishRateLimiterFt finish; 75 RATELIMITERFUNCTIONST; 76 } rateLimiterFunctionst; 77 78 /** 79 * class 80 */ 81 struct rateLimiter { 82 const char *type; 83 rateLimiterFunctionst *f; 84 85 rateLimterE *rateLimiterA; 86 indexer rateLimiterIx; 87 u64 window; 88 u32 maxAccessCount; 89 rateLt rateLimiterH; 90 /* TODO add class data */ 91 }; 92 93 /* rateLimiter */ 94 95 #define createRateLimiter(obj) rateLimitert obj; initiateRateLimiter(&obj) 96 #define createAllocateRateLimiter(obj) rateLimitert *obj; initiateAllocateRateLimiter(&obj) 97 98 void initiateRateLimiter(rateLimitert *self); 99 void initiateAllocateRateLimiter(rateLimitert **self); 100 void finalizeRateLimiter(void); 101 102 /* initialize class methods, call registerMethodsRateLimiter from classes inheriting this class */ 103 void registerMethodsRateLimiter(rateLimiterFunctionst *f); 104 105 rateLimitert* allocRateLimiter(void/*TODO INIT DATA */); 106 107 /* end class rateLimiter*/