{"id":31103591,"url":"https://github.com/madhur-25/token-gate","last_synced_at":"2025-10-05T06:34:42.061Z","repository":{"id":311393486,"uuid":"1043566788","full_name":"madhur-25/Token-gate","owner":"madhur-25","description":"A modern C++17 implementation of a thread-safe rate limiter with an LRU Cache for efficient memory management. Fully unit-tested with Google Test and built with CMake.","archived":false,"fork":false,"pushed_at":"2025-08-24T09:36:31.000Z","size":2897,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-08-24T13:44:40.628Z","etag":null,"topics":["cmake","concurrency","cpp","cpp17","googletest","lru-cache","multithreading","oop-principles","rate-limiter","system-design","token-bucket","unit-testing"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/madhur-25.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-08-24T06:16:00.000Z","updated_at":"2025-08-24T09:51:09.000Z","dependencies_parsed_at":"2025-08-24T13:44:46.554Z","dependency_job_id":"65970567-640b-4edd-bf19-373b90c2a29c","html_url":"https://github.com/madhur-25/Token-gate","commit_stats":null,"previous_names":["madhur-25/token-gate"],"tags_count":null,"template":false,"template_full_name":null,"purl":"pkg:github/madhur-25/Token-gate","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/madhur-25%2FToken-gate","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/madhur-25%2FToken-gate/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/madhur-25%2FToken-gate/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/madhur-25%2FToken-gate/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/madhur-25","download_url":"https://codeload.github.com/madhur-25/Token-gate/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/madhur-25%2FToken-gate/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":275521497,"owners_count":25479612,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-17T02:00:09.119Z","response_time":84,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cmake","concurrency","cpp","cpp17","googletest","lru-cache","multithreading","oop-principles","rate-limiter","system-design","token-bucket","unit-testing"],"created_at":"2025-09-17T02:07:18.081Z","updated_at":"2025-09-17T02:07:19.977Z","avatar_url":"https://github.com/madhur-25.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Token-Gate:C++ Rate Limiter\n\nToken-Gate is a thread-safe rate limiter built in modern C++17. It uses the classic **Token Bucket** algorithm to control how often an action can be performed on a per-user basis. To prevent memory leaks in long-running services, it features a built-in **LRU Cache** that automatically evicts the least recently used user data.\n\nThe project is built using a clean, modular design and is fully verified with a suite of unit tests using Google Test.\n\n---\n## Key Features\n\n* **Token Bucket Algorithm**: Implements a flexible, time-based rate limiting algorithm with configurable capacity and token refill rates.\n* **Efficient Memory Management**: Uses a built-in LRU (Least Recently Used) Cache to manage a fixed number of user buckets, preventing memory leaks over time.\n* **Thread-Safe by Design**: Built from the ground up to handle concurrent requests safely using C++ primitives like `std::mutex`, `std::lock_guard`, and `std::atomic`.\n* **Clean \u0026 Modular Design**: Follows modern C++ best practices by separating concerns into logical components (Logger, Metrics, Cache, etc.) and organizing the project with a clean header file structure.\n* **Unit Tested**: Includes a comprehensive test suite using the Google Test framework to ensure the logic is correct and reliable.\n* **Cross-Platform Build System**: Uses CMake to manage the build process, making it easy to compile and run on any system.\n\n---\n\n## Technical Deep Dive\n\nThis project explores several key computer science and software engineering concepts:\n\n* **System Design**: Explores fundamental backend concepts like rate limiting and caching (specifically LRU eviction strategy).\n* **Data Structures**: A practical application of Hash Maps and Doubly Linked Lists to build an optimal O(1) LRU Cache.\n* **Object-Oriented Programming**: Leverages polymorphism for different rate-limiting algorithms and RAII principles for resource management with smart pointers.\n* **Concurrency**: A focus on multithreaded programming and ensuring safe access to shared data.\n* **Modern C++ (17)**: Makes use of modern language features including smart pointers, the `\u003cchrono\u003e` library for precise timekeeping, and templates.\n\n---\n\n## Technology Stack\n\n* **Language**: C++17\n* **Build System**: CMake\n* **Testing Framework**: Google Test\n\n---\n\n## Setup and Build Instructions\n\n### Prerequisites\n* Git\n* CMake (v3.14+)\n* A C++17 compliant compiler (GCC, Clang, or MSVC)\n\n### Build Steps\n\n1.  **Clone the repository:**\n    ```bash\n    git clone [https://github.com/madhur-25/Token-gate.git](https://github.com/madhur-25/Token-gate.git)\n    cd Token-gate\n    ```\n\n2.  **Create a build directory:**\n    ```bash\n    mkdir build\n    cd build\n    ```\n\n3.  **Configure and build the project:**\n    ```bash\n    cmake ..\n    cmake --build .\n    ```\n\n4.  **Run the demo or the unit tests:**\n    ```bash\n    # Run the main demo showing the cache eviction\n    ./main\n\n    # Run the unit tests\n    ./run_tests\n    ```\n\n---\n\n## Demo Output\n\nRunning the `main` executable demonstrates the LRU cache evicting users when its capacity is reached.\n\n```\n--- Accessing 3 users to fill the cache ---\n[INFO] 14:55:10 | user=user1 | Cache Miss. Creating new bucket.\n[INFO] 14:55:10 | user=user1 | Allowed\n[INFO] 14:55:10 | user=user2 | Cache Miss. Creating new bucket.\n[INFO] 14:55:10 | user=user2 | Allowed\n[INFO] 14:55:10 | user=user3 | Cache Miss. Creating new bucket.\n[INFO] 14:55:10 | user=user3 | Allowed\n\n--- Cache is now full. Order of recency: user3, user2, user1 ---\n--- Accessing user1 again to make it the most recently used ---\n[INFO] 14:55:10 | user=user1 | Cache Hit.\n[INFO] 14:55:10 | user=user1 | Allowed\n\n--- Accessing a new user (user4). This should evict the LRU user (user2) ---\n[INFO] 14:55:10 | user=user4 | Cache Miss. Creating new bucket.\n[INFO] 14:55:10 | user=user4 | Allowed\n\n--- Accessing user2 again. It was evicted, so this should be a cache miss. ---\n[INFO] 14:55:10 | user=user2 | Cache Miss. Creating new bucket.\n[INFO] 14:55:10 | user=user2 | Allowed\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmadhur-25%2Ftoken-gate","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmadhur-25%2Ftoken-gate","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmadhur-25%2Ftoken-gate/lists"}