1. <?php
  2. class TaskMetadata {
  3. private $user_id;
  4. private $request_id;
  5. private $timestamp;
  6. public function __construct($user_id, $request_id) {
  7. $this->user_id = $user_id;
  8. $this->request_id = $request_id;
  9. $this->timestamp = time();
  10. }
  11. public function getUserID() {
  12. return $this->user_id;
  13. }
  14. public function getRequestID() {
  15. return $this->request_id;
  16. }
  17. public function getTimestamp() {
  18. return $this->timestamp;
  19. }
  20. }
  21. class RateLimiter {
  22. private $limits = []; // Array to store rate limits for each user/request_id
  23. public function __construct() {}
  24. public function initialize() {
  25. // Initialize rate limits for each user/request_id
  26. }
  27. public function isAllowed($user_id, $request_id) {
  28. $key = $this->getKey($user_id, $request_id);
  29. if (!isset($this->limits[$key])) {
  30. $this->limits[$key] = []; // Initialize limit array if not exists
  31. }
  32. if (count($this->limits[$key]) >= 10) { // Example limit: 10 requests per minute
  33. return false; // Rate limit exceeded
  34. }
  35. $this->limits[$key][] = time(); // Add timestamp of the request
  36. return true; // Request allowed
  37. }
  38. private function getKey($user_id, $request_id) {
  39. return $user_id . '_' . $request_id;
  40. }
  41. public function resetLimits($user_id, $request_id) {
  42. $key = $this->getKey($user_id, $request_id);
  43. if(isset($this->limits[$key])){
  44. unset($this->limits[$key]);
  45. }
  46. }
  47. }
  48. function processTask($user_id, $request_id, $task_data, $rate_limiter) {
  49. // Attach metadata
  50. $metadata = new TaskMetadata($user_id, $request_id);
  51. // Check rate limit
  52. if (!$rate_limiter->isAllowed($user_id, $request_id)) {
  53. return ['success' => false, 'message' => 'Rate limit exceeded'];
  54. }
  55. // Process the task
  56. try {
  57. // Your task processing logic here
  58. echo "Processing task for user " . $user_id . " with request ID " . $request_id . "\n";
  59. // Simulate some work
  60. sleep(1);
  61. // Example: Do something with $task_data
  62. // echo "Task data: " . json_encode($task_data) . "\n";
  63. return ['success' => true, 'metadata' => $metadata]; // Return success with metadata
  64. } catch (Exception $e) {
  65. return ['success' => false, 'message' => $e->getMessage()];
  66. }
  67. }
  68. // Example Usage (for testing)
  69. $rate_limiter = new RateLimiter();
  70. $rate_limiter->initialize();
  71. $user_id = 123;
  72. $request_id = 456;
  73. $task_data = ['key' => 'value'];
  74. for ($i = 0; $i < 12; $i++) {
  75. $result = processTask($user_id, $request_id, $task_data, $rate_limiter);
  76. if ($result['success']) {
  77. echo "Task completed successfully.\n";
  78. } else {
  79. echo "Task failed: " . $result['message'] . "\n";
  80. }
  81. sleep(0.1);
  82. }
  83. //Reset limits after testing
  84. $rate_limiter->resetLimits($user_id, $request_id);
  85. ?>

Add your comment