Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rate limit delay support #177

Merged
merged 4 commits into from
Apr 5, 2021
Merged

Add rate limit delay support #177

merged 4 commits into from
Apr 5, 2021

Conversation

glensc
Copy link
Collaborator

@glensc glensc commented Apr 5, 2021

To rate limit requests without hitting rate limit error, decorate the method like:

    @rate_limit(delay=1.2)
    def me(self):
        try:
            return trakt.users.User('me')

All methods share the same pool for rate limits.

Currently, all methods in TraktApi all GET method based, so for test only .me method is rate limited

refs:

@glensc glensc self-assigned this Apr 5, 2021
@glensc glensc added the tested label Apr 5, 2021
glensc added 3 commits April 5, 2021 20:02
This limits number of requests allowed to be performed in sequence
@glensc glensc force-pushed the rate-limit-delay branch from 757f178 to 173e1a6 Compare April 5, 2021 17:02
@glensc glensc force-pushed the rate-limit-delay branch from 173e1a6 to b31fbe4 Compare April 5, 2021 19:02
@glensc glensc merged commit d98cd0b into Taxel:main Apr 5, 2021
@glensc glensc deleted the rate-limit-delay branch April 5, 2021 19:19
@simonc56
Copy link
Collaborator

simonc56 commented Apr 5, 2021

Tested : too much errors for me with 1.1sec delay.
It depends how reliable and how far connection with trakt servers is at the moment.

@glensc
Copy link
Collaborator Author

glensc commented Apr 5, 2021

@twolaw so better go back to 1.2s?

@glensc
Copy link
Collaborator Author

glensc commented Apr 5, 2021

@twolaw and how do you even test this? as the rate limit affects only POST method. do you clear your library to get a big amount of updates?

can you post a log of how this behaved for you?

@simonc56
Copy link
Collaborator

simonc56 commented Apr 5, 2021

Yes, I cleared a TV show each time I wanted to try another delay value with massive POST requests.

I did tests from France, did you test it with differents delay values ?
Found out that below 1.2 I get much more errors, and above it slows down the script for no advantage.

@glensc
Copy link
Collaborator Author

glensc commented Apr 5, 2021

@twolaw no, I only tested that if trakt throws, the PTS code handles it.

@glensc
Copy link
Collaborator Author

glensc commented Apr 5, 2021

@twolaw slow connection should rather benefit for rate limit, as you be making connections slower...

and can you share what output did you get? the rate limit itself worked, and you got warning messages on the terminal? you can obfuscate the irrelevant details like show name if you wish.

@simonc56
Copy link
Collaborator

simonc56 commented Apr 6, 2021

@twolaw no, I only tested that if trakt throws, the PTS code handles it.

That's not good enough because trakt errors slow down the script, having to resend the requests.
It is important to raise trakt errors as little as possible to keep a good speed.

and can you share what output did you get? the rate limit itself worked, and you got warning messages on the terminal?

Yes the rate limit works, the objective is to be as fast as possible and to get very few trakt rate-limit warnings.

@glensc
Copy link
Collaborator Author

glensc commented Apr 6, 2021

perhaps add 0.2-second delay additionally to the value of retry-after header? or how is my implementation different than yours?

@glensc
Copy link
Collaborator Author

glensc commented Apr 6, 2021

maybe it was always that slow (after rate limit being implemented), just did not notice it as sleeping was not logged?

@simonc56
Copy link
Collaborator

simonc56 commented Apr 6, 2021

You should remove the "Sleeping for x sec" logging because it is a normal behaviour and it will create too much noise in logs.
Or at least move to debug level.

@glensc
Copy link
Collaborator Author

glensc commented Apr 6, 2021

@twolaw so, it's just a logging matter? or you are still getting hit by rate limit sooner than before? as you didn't share your output I have to guess what's happening. you didn't answer to my previous questions.

@simonc56
Copy link
Collaborator

simonc56 commented Apr 6, 2021

The previous remark about logging is something else I just noticed.

Here is logs with 1.2s delay. As you can see, trakt raises very few rate-limit errors.

2021-03-17 17:21:54,996 INFO:Now working on show section Séries TV containing 16 elements
2021-03-17 17:21:55,835 INFO:Show [Attack on Titan (2013)]: Finished sync
2021-03-17 17:21:56,599 INFO:Show [Better Call Saul (2015)]: Finished sync
2021-03-17 17:21:57,539 INFO:Show [Black Mirror (2011)]: Finished sync
2021-03-17 17:21:58,641 INFO:Show [The Boys (2019)]: Finished sync
2021-03-17 17:21:59,642 INFO:Show [The Bureau (2015)]: s04e08 added to list test
2021-03-17 17:21:59,642 INFO:Show [The Bureau (2015)]: Finished sync
2021-03-17 17:22:00,390 INFO:Show [Chernobyl (2019)]: Finished sync
2021-03-17 17:22:01,429 INFO:Show [Dark (2017)]: s02e01 added to list test
2021-03-17 17:22:01,530 INFO:Show [Dark (2017)]: s02e08 added to list test
2021-03-17 17:22:01,530 INFO:Show [Dark (2017)]: Finished sync
2021-03-17 17:22:01,946 INFO:Show [The Handmaid's Tale (2017)]: Finished sync
2021-03-17 17:22:03,131 INFO:Show [Homeland (2011)]: Finished sync
2021-03-17 17:22:05,393 INFO:Show [How I Met Your Mother (2005)]: Marked as watched on trakt: episode S04E01
2021-03-17 17:22:06,978 INFO:Show [How I Met Your Mother (2005)]: Marked as watched on trakt: episode S04E02
2021-03-17 17:22:07,962 INFO:Show [How I Met Your Mother (2005)]: Marked as watched on trakt: episode S04E03
2021-03-17 17:22:08,879 INFO:Show [How I Met Your Mother (2005)]: Marked as watched on trakt: episode S04E04
2021-03-17 17:22:10,280 INFO:Show [How I Met Your Mother (2005)]: Marked as watched on trakt: episode S04E05
2021-03-17 17:22:11,412 INFO:Show [How I Met Your Mother (2005)]: Marked as watched on trakt: episode S04E06
2021-03-17 17:22:12,798 INFO:Show [How I Met Your Mother (2005)]: Marked as watched on trakt: episode S04E07
2021-03-17 17:22:13,837 INFO:Show [How I Met Your Mother (2005)]: Marked as watched on trakt: episode S04E08
2021-03-17 17:22:14,956 INFO:Show [How I Met Your Mother (2005)]: Marked as watched on trakt: episode S04E09
2021-03-17 17:22:16,438 INFO:Show [How I Met Your Mother (2005)]: Marked as watched on trakt: episode S04E10
2021-03-17 17:22:17,640 INFO:Show [How I Met Your Mother (2005)]: Marked as watched on trakt: episode S04E11
2021-03-17 17:22:18,894 INFO:Show [How I Met Your Mother (2005)]: Marked as watched on trakt: episode S04E12
2021-03-17 17:22:19,879 INFO:Show [How I Met Your Mother (2005)]: Marked as watched on trakt: episode S04E13
2021-03-17 17:22:21,078 INFO:Show [How I Met Your Mother (2005)]: Marked as watched on trakt: episode S04E14
2021-03-17 17:22:22,279 INFO:Show [How I Met Your Mother (2005)]: Marked as watched on trakt: episode S04E15
2021-03-17 17:22:23,763 INFO:Show [How I Met Your Mother (2005)]: Marked as watched on trakt: episode S04E16
2021-03-17 17:22:24,463 INFO:Show [How I Met Your Mother (2005)]: Marked as watched on trakt: episode S04E17
2021-03-17 17:22:25,994 INFO:Show [How I Met Your Mother (2005)]: Marked as watched on trakt: episode S04E18
2021-03-17 17:22:27,263 INFO:Show [How I Met Your Mother (2005)]: Marked as watched on trakt: episode S04E19
2021-03-17 17:22:28,363 INFO:Show [How I Met Your Mother (2005)]: Marked as watched on trakt: episode S04E20
2021-03-17 17:22:29,547 INFO:Show [How I Met Your Mother (2005)]: Marked as watched on trakt: episode S04E21
2021-03-17 17:22:30,963 INFO:Show [How I Met Your Mother (2005)]: Marked as watched on trakt: episode S04E22
2021-03-17 17:22:32,148 INFO:Show [How I Met Your Mother (2005)]: Marked as watched on trakt: episode S04E23
2021-03-17 17:22:33,410 INFO:Show [How I Met Your Mother (2005)]: Marked as watched on trakt: episode S04E24
2021-03-17 17:22:34,363 INFO:Show [How I Met Your Mother (2005)]: Marked as watched on trakt: episode S05E01
2021-03-17 17:22:35,664 INFO:Show [How I Met Your Mother (2005)]: Marked as watched on trakt: episode S05E02
2021-03-17 17:22:37,063 INFO:Show [How I Met Your Mother (2005)]: Marked as watched on trakt: episode S05E03
2021-03-17 17:22:38,079 INFO:Show [How I Met Your Mother (2005)]: Marked as watched on trakt: episode S05E04
2021-03-17 17:22:39,310 INFO:Show [How I Met Your Mother (2005)]: Marked as watched on trakt: episode S05E05
2021-03-17 17:22:40,526 INFO:Show [How I Met Your Mother (2005)]: Marked as watched on trakt: episode S05E06
2021-03-17 17:22:41,479 INFO:Show [How I Met Your Mother (2005)]: Marked as watched on trakt: episode S05E07
2021-03-17 17:22:43,079 INFO:Show [How I Met Your Mother (2005)]: Marked as watched on trakt: episode S05E08
2021-03-17 17:22:44,363 INFO:Show [How I Met Your Mother (2005)]: Marked as watched on trakt: episode S05E09
2021-03-17 17:22:45,041 WARNING:Show [How I Met Your Mother (2005)]: Rate limit on watched episode S05E10. Sleep 1 sec from trakt
2021-03-17 17:22:46,510 INFO:Show [How I Met Your Mother (2005)]: Marked as watched on trakt: episode S05E10
2021-03-17 17:22:47,580 INFO:Show [How I Met Your Mother (2005)]: Marked as watched on trakt: episode S05E11
2021-03-17 17:22:48,744 INFO:Show [How I Met Your Mother (2005)]: Marked as watched on trakt: episode S05E12
2021-03-17 17:22:50,285 INFO:Show [How I Met Your Mother (2005)]: Marked as watched on trakt: episode S05E13
2021-03-17 17:22:51,256 INFO:Show [How I Met Your Mother (2005)]: Marked as watched on trakt: episode S05E14
2021-03-17 17:22:52,457 INFO:Show [How I Met Your Mother (2005)]: Marked as watched on trakt: episode S05E15
2021-03-17 17:22:53,806 INFO:Show [How I Met Your Mother (2005)]: Marked as watched on trakt: episode S05E16
2021-03-17 17:22:55,006 INFO:Show [How I Met Your Mother (2005)]: Marked as watched on trakt: episode S05E17
2021-03-17 17:22:55,923 INFO:Show [How I Met Your Mother (2005)]: Marked as watched on trakt: episode S05E18
2021-03-17 17:22:57,394 INFO:Show [How I Met Your Mother (2005)]: Marked as watched on trakt: episode S05E19
2021-03-17 17:22:58,896 INFO:Show [How I Met Your Mother (2005)]: Marked as watched on trakt: episode S05E20
2021-03-17 17:22:59,664 INFO:Show [How I Met Your Mother (2005)]: Marked as watched on trakt: episode S05E21
2021-03-17 17:23:01,343 INFO:Show [How I Met Your Mother (2005)]: Marked as watched on trakt: episode S05E22
2021-03-17 17:23:02,180 INFO:Show [How I Met Your Mother (2005)]: Marked as watched on trakt: episode S05E23
2021-03-17 17:23:03,464 INFO:Show [How I Met Your Mother (2005)]: Marked as watched on trakt: episode S05E24
2021-03-17 17:23:04,327 WARNING:Show [How I Met Your Mother (2005)]: Rate limit on watched episode S06E01. Sleep 1 sec from trakt
2021-03-17 17:23:05,864 INFO:Show [How I Met Your Mother (2005)]: Marked as watched on trakt: episode S06E01
2021-03-17 17:23:07,080 INFO:Show [How I Met Your Mother (2005)]: Marked as watched on trakt: episode S06E02
2021-03-17 17:23:08,527 INFO:Show [How I Met Your Mother (2005)]: Marked as watched on trakt: episode S06E03
2021-03-17 17:23:09,343 INFO:Show [How I Met Your Mother (2005)]: Marked as watched on trakt: episode S06E04
2021-03-17 17:23:10,758 INFO:Show [How I Met Your Mother (2005)]: Marked as watched on trakt: episode S06E05
2021-03-17 17:23:12,113 INFO:Show [How I Met Your Mother (2005)]: Marked as watched on trakt: episode S06E06
2021-03-17 17:23:12,914 INFO:Show [How I Met Your Mother (2005)]: Marked as watched on trakt: episode S06E07
2021-03-17 17:23:14,139 INFO:Show [How I Met Your Mother (2005)]: Marked as watched on trakt: episode S06E08
2021-03-17 17:23:15,380 INFO:Show [How I Met Your Mother (2005)]: Marked as watched on trakt: episode S06E09
2021-03-17 17:23:16,981 INFO:Show [How I Met Your Mother (2005)]: Marked as watched on trakt: episode S06E10
2021-03-17 17:23:17,984 INFO:Show [How I Met Your Mother (2005)]: Marked as watched on trakt: episode S06E11
2021-03-17 17:23:19,570 INFO:Show [How I Met Your Mother (2005)]: Marked as watched on trakt: episode S06E12
2021-03-17 17:23:20,519 INFO:Show [How I Met Your Mother (2005)]: Marked as watched on trakt: episode S06E13
2021-03-17 17:23:21,335 INFO:Show [How I Met Your Mother (2005)]: Marked as watched on trakt: episode S06E14
2021-03-17 17:23:22,821 INFO:Show [How I Met Your Mother (2005)]: Marked as watched on trakt: episode S06E15
2021-03-17 17:23:23,722 INFO:Show [How I Met Your Mother (2005)]: Marked as watched on trakt: episode S06E16
2021-03-17 17:23:25,139 INFO:Show [How I Met Your Mother (2005)]: Marked as watched on trakt: episode S06E17
2021-03-17 17:23:26,441 INFO:Show [How I Met Your Mother (2005)]: Marked as watched on trakt: episode S06E18
2021-03-17 17:23:27,596 INFO:Show [How I Met Your Mother (2005)]: Marked as watched on trakt: episode S06E19
2021-03-17 17:23:28,712 INFO:Show [How I Met Your Mother (2005)]: Marked as watched on trakt: episode S06E20
2021-03-17 17:23:29,981 INFO:Show [How I Met Your Mother (2005)]: Marked as watched on trakt: episode S06E21
2021-03-17 17:23:30,981 INFO:Show [How I Met Your Mother (2005)]: Marked as watched on trakt: episode S06E22
2021-03-17 17:23:32,597 INFO:Show [How I Met Your Mother (2005)]: Marked as watched on trakt: episode S06E23
2021-03-17 17:23:33,412 INFO:Show [How I Met Your Mother (2005)]: Marked as watched on trakt: episode S06E24
2021-03-17 17:23:33,412 INFO:Show [How I Met Your Mother (2005)]: Finished sync
2021-03-17 17:23:34,681 INFO:Show [The Mandalorian (2019)]: Finished sync
2021-03-17 17:23:35,614 INFO:Show [Peaky Blinders (2013)]: s04e06 added to list test
2021-03-17 17:23:35,715 INFO:Show [Peaky Blinders (2013)]: s05e01 added to list test
2021-03-17 17:23:35,831 INFO:Show [Peaky Blinders (2013)]: s05e03 added to list test
2021-03-17 17:23:35,831 INFO:Show [Peaky Blinders (2013)]: Finished sync

@glensc
Copy link
Collaborator Author

glensc commented Apr 6, 2021

@twolaw the log looks okay. and it's from old code, I have not touched shows sync.

I don't see "Sleeping for x sec" at all, so, no logs no problem.

however, the mark_as_watched is POST request, how can you make more than 1 request per second? their docs are outdated?

@simonc56
Copy link
Collaborator

simonc56 commented Apr 6, 2021

however, the mark_as_watched is POST request, how can you make more than 1 request per second? their docs are outdated?

Communication between trakt server and user computer is not realtime. Requests may take more or less time to reach each other.

@glensc
Copy link
Collaborator Author

glensc commented Apr 6, 2021

I found one bug, maybe related to problems you were having:

@glensc glensc mentioned this pull request Apr 10, 2021
@simonc56 simonc56 mentioned this pull request Apr 12, 2021
@simonc56
Copy link
Collaborator

simonc56 commented Apr 12, 2021

I found one bug, maybe related to problems you were having:

* #198

Not a bug in master version last_time is updated each time:
last_time = respect_trakt_rate(last_time)

As I said 1.1s is too short, do you have logs where 1.1s delay doesn't raise too much errors ?

@glensc
Copy link
Collaborator Author

glensc commented Apr 12, 2021

I found one bug, maybe related to problems you were having:

* #198

Not a bug in master version last_time is updated each time:
last_time = respect_trakt_rate(last_time)

Since you did not indicate the version you were using, I assumed you used code from this Pull Request.

As I said 1.1s is too short, do you have logs where 1.1s delay doesn't raise too much errors ?

I'm using 1.1s and have not noticed excess delays that become annoying to me. But I don't clear my libraries either, so based on normal usage.

I'm planning to bulk update the watched status, so this becomes rather irrelevant in the new code. With the bulk update, can actually implement --dry-run in a safe manner, which as a developer I'm very looking forward.

@glensc
Copy link
Collaborator Author

glensc commented Apr 12, 2021

I'm planning to bulk update the watched status, so this becomes rather irrelevant in the new code. With the bulk update, can actually implement --dry-run in a safe manner, which as a developer I'm very looking forward.

Implemented batching add_to_collection trakt api calls:

Repository owner locked as resolved and limited conversation to collaborators Jan 11, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants