obervinov / users-package

This Python module is designed to simplify user management in Telegram bots, providing the necessary functionality for tasks such as user authentication, authorization and compliance with speed limits, ensuring effective management of user attributes and access rights.
MIT License
1 stars 0 forks source link

Configuration `per_day` in RateLimit not working as expected #40

Open obervinov opened 5 months ago

obervinov commented 5 months ago

Describe the bug Configuration per_day in RateLimit not working as expected. According to my observations, this parameter does not work as expected and when the number of requests is exceeded, the query execution time accounting breaks down.

To Reproduce Steps to reproduce the behavior:

  1. Set configuration {"requests_per_day": 5, "requests_per_hour": 1, "random_shift_minutes": 360}
  2. Generate 11-12 queries to apply daily ratelimit
  3. The ratelimit timer will set the same processing time for all received requests for some time and will not add +1 day to it.

Expected behavior If the daily ratelimit is exceeded, the processing time +1 day (+1 hour from the previous request) should be returned to all requests.

Screenshots

Снимок экрана 2024-05-11 в 18 21 38

Additional context Users data in Vault

{
  "authentication": "{\"time\": \"2024-05-10 10:37:42.206036\", \"status\": \"allowed\"}",
  "authorization": "{\"time\": \"2024-05-10 10:37:54.057882\", \"status\": \"allowed\", \"role\": \"post\"}",
  "requests_counters": "{\"requests_per_hour\": 3, \"requests_per_day\": 7}",
  "requests_history": "[\"2024-04-28 18:03:46.689374\", \"2024-04-28 20:17:29.715123\", \"2024-04-29 18:31:12.010274\", \"2024-04-29 18:34:00.162449\", \"2024-04-29 18:44:46.783133\", \"2024-04-29 18:44:47.614515\", \"2024-04-30 12:56:04.160023\", \"2024-04-30 19:28:25.244414\", \"2024-04-30 20:21:53.361840\", \"2024-05-01 06:14:55.136715\", \"2024-05-01 06:14:56.162464\", \"2024-05-01 06:28:04.770559\", \"2024-05-01 06:28:05.759283\", \"2024-05-01 06:28:06.929947\", \"2024-05-01 06:28:07.893657\", \"2024-05-01 06:49:41.293213\", \"2024-05-01 06:49:42.123203\", \"2024-05-01 09:30:40.433108\", \"2024-05-01 09:30:42.427200\", \"2024-05-01 09:30:43.070135\", \"2024-05-01 09:30:43.892978\", \"2024-05-01 09:30:44.626797\", \"2024-05-01 09:30:45.444172\", \"2024-05-01 09:30:46.327353\", \"2024-05-01 09:30:47.213644\", \"2024-05-01 09:30:48.253656\", \"2024-05-01 09:30:49.121649\", \"2024-05-01 09:30:49.964373\", \"2024-05-01 09:30:50.770601\", \"2024-05-02 10:05:52.731315\", \"2024-05-02 10:05:53.510574\", \"2024-05-02 10:05:54.548760\", \"2024-05-02 10:05:55.440846\", \"2024-05-02 10:05:56.354842\", \"2024-05-02 10:05:57.198197\", \"2024-05-02 10:05:57.998544\", \"2024-05-02 10:05:58.864686\", \"2024-05-04 07:36:58.402686\", \"2024-05-04 07:36:59.409894\", \"2024-05-04 07:37:00.548504\", \"2024-05-04 07:37:01.473791\", \"2024-05-04 07:37:02.530700\", \"2024-05-09 11:49:03.184859\", \"2024-05-09 11:55:44.834973\", \"2024-05-09 11:55:45.902370\", \"2024-05-09 11:55:47.169803\", \"2024-05-10 10:35:40.252446\", \"2024-05-10 10:36:11.728285\", \"2024-05-10 10:37:12.231424\", \"2024-05-10 10:38:47.389254\"]",
  "requests_ratelimits": "{\"end_time\": \"2024-05-11 15:18:04.473465\"}"
}

Application logs

[2024-05-11 14:13:09,436] INFO [logger.logger:authentication:304] [class.Users] Access from user ID 123456789: allowed
[2024-05-11 14:13:09,651] INFO [logger.logger:authorization:363] [class.Users] Check role `role_list` for user `123456789`: allowed
[2024-05-11 14:13:09,862] INFO [logger.logger:authentication:304] [class.Users] Access from user ID 123456789: allowed
[2024-05-11 14:13:10,070] INFO [logger.logger:authorization:363] [class.Users] Check role `role_list` for user `123456789`: allowed
[2024-05-11 14:13:12,631] INFO [logger.logger:authentication:304] [class.Users] Access from user ID 123456789: allowed
[2024-05-11 14:13:12,849] INFO [logger.logger:authorization:363] [class.Users] Check role `role_list` for user `123456789`: allowed
[2024-05-11 14:13:13,048] INFO [logger.logger:authentication:304] [class.Users] Access from user ID 123456789: allowed
[2024-05-11 14:13:13,279] INFO [logger.logger:authorization:363] [class.Users] Check role `role` for user `123456789`: allowed
[2024-05-11 14:13:13,707] INFO [logger.logger:_update_requests_counters:520] [class.RateLimiter] Current request counters: {'requests_per_hour': 0, 'requests_per_day': 0}
[2024-05-11 14:13:13,708] WARNING [logger.logger:_active_rate_limit:378] [class.RateLimiter] A rate limit has been detected for user ID 123456789 that has already been applied and has not expired yet
[2024-05-11 14:13:14,162] INFO [logger.logger:update_status_message:307] [Bot]: Message with type `status_message` for user 123456789 has been updated
[2024-05-11 14:13:14,163] INFO [logger.logger:process_one_post:451] [Bot]: Message for user 123456789 added in queue
[2024-05-11 14:13:14,172] INFO [logger.logger:authentication:304] [class.Users] Access from user ID 123456789: allowed
[2024-05-11 14:13:14,331] INFO [logger.logger:authorization:363] [class.Users] Check role `role` for user `123456789`: allowed
[2024-05-11 14:13:14,646] INFO [logger.logger:_update_requests_counters:520] [class.RateLimiter] Current request counters: {'requests_per_hour': 1, 'requests_per_day': 1}
[2024-05-11 14:13:15,095] INFO [logger.logger:process_one_post:451] [Bot]: Message for user 123456789 added in queue
[2024-05-11 14:13:15,104] INFO [logger.logger:authentication:304] [class.Users] Access from user ID 123456789: allowed
[2024-05-11 14:13:15,250] INFO [logger.logger:authorization:363] [class.Users] Check role `role` for user `123456789`: allowed
[2024-05-11 14:13:15,530] INFO [logger.logger:_update_requests_counters:520] [class.RateLimiter] Current request counters: {'requests_per_hour': 2, 'requests_per_day': 2}
[2024-05-11 14:13:15,530] WARNING [logger.logger:_active_rate_limit:378] [class.RateLimiter] A rate limit has been detected for user ID 123456789 that has already been applied and has not expired yet
[2024-05-11 14:13:15,871] INFO [logger.logger:process_one_post:451] [Bot]: Message for user 123456789 added in queue

Check list

obervinov commented 4 days ago

Still reproducing, the day counter is not working correctly