1. <?php
  2. /**
  3. * Aggregates values from text files with rate limiting.
  4. *
  5. * @param array $filePaths An array of file paths to aggregate.
  6. * @param int $requestsPerSecond The maximum number of requests allowed per second.
  7. * @return array An associative array where keys are file paths and values are the aggregated data.
  8. * Returns an empty array on error.
  9. */
  10. function aggregateTextFilesWithRateLimit(array $filePaths, int $requestsPerSecond): array
  11. {
  12. $results = [];
  13. $rateLimit = new RateLimit(1, $requestsPerSecond); // Initialize rate limiter
  14. foreach ($filePaths as $filePath) {
  15. try {
  16. $data = readAndAggregateFile($filePath);
  17. $results[$filePath] = $data;
  18. } catch (Exception $e) {
  19. error_log("Error processing file {$filePath}: " . $e->getMessage());
  20. // Handle the error appropriately (e.g., log, skip, or retry)
  21. continue; // Skip to the next file
  22. }
  23. $rateLimit->wait(); // Wait if rate limit is exceeded
  24. }
  25. return $results;
  26. }
  27. /**
  28. * Reads a text file and aggregates its content.
  29. *
  30. * @param string $filePath The path to the text file.
  31. * @return string The aggregated content of the file.
  32. * @throws Exception If the file cannot be read.
  33. */
  34. function readAndAggregateFile(string $filePath): string
  35. {
  36. $content = file_get_contents($filePath);
  37. if ($content === false) {
  38. throw new Exception("Failed to read file: {$filePath}");
  39. }
  40. // Perform aggregation logic here. This is a placeholder.
  41. // Replace with your specific aggregation requirements.
  42. return strtoupper($content); // Example: Convert to uppercase
  43. }
  44. /**
  45. * Simple Rate Limiter class
  46. */
  47. class RateLimit {
  48. private $capacity;
  49. private $rate;
  50. private $tokens;
  51. private $lastReset;
  52. public function __construct(int $capacity, int $rate) {
  53. $this->capacity = $capacity;
  54. $this->rate = $rate;
  55. $this->tokens = $capacity;
  56. $this->lastReset = time();
  57. }
  58. public function wait(): void {
  59. $now = time();
  60. $timePassed = $now - $this->lastReset;
  61. $tokensToAdd = $timePassed * $this->rate;
  62. $this->tokens = min($this->capacity, $this->tokens + $tokensToAdd);
  63. $this->lastReset = $now;
  64. if ($this->tokens < 1) {
  65. $sleep = 1000 / $this->rate; // Calculate sleep time
  66. usleep($sleep * 1000000); // Sleep for calculated time
  67. }
  68. }
  69. }
  70. // Example Usage (replace with your actual file paths and rate limit)
  71. $filePaths = [
  72. 'file1.txt',
  73. 'file2.txt',
  74. 'file3.txt',
  75. ];
  76. // Create dummy files for testing
  77. file_put_contents('file1.txt', 'This is file 1.');
  78. file_put_contents('file2.txt', 'This is file 2.');
  79. file_put_contents('file3.txt', 'This is file 3.');
  80. $requestsPerSecond = 2;
  81. $aggregatedData = aggregateTextFilesWithRateLimit($filePaths, $requestsPerSecond);
  82. print_r($aggregatedData);
  83. ?>

Add your comment