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

Timers (or how to do setInterval and clearInterval in Elixir / Erlang?) #225

Open
nelsonic opened this issue Jan 3, 2025 · 3 comments
Open
Assignees
Labels
discuss Share your constructive thoughts on how to make progress with this issue elixir Pull requests that update Elixir code enhancement New feature or enhancement of existing functionality help wanted If you can help make progress with this issue, please comment! priority-2 Second highest priority, should be worked on as soon as the Priority-1 issues are finished question A question needs to be answered before progress can be made on this issue research Research required; be specific T25m Time Estimate 25 Minutes technical A technical issue that requires understanding of the code, infrastructure or dependencies

Comments

@nelsonic
Copy link
Member

nelsonic commented Jan 3, 2025

I'm making a few million API requests to get some data dwyl/who#17
and need to control the request frequency to avoid hitting rate limits ... ⏳ (5k/hour -> 120k/day -> 3.65m/month )
In JS-land I would just use setInterval to define the timeframe for executing the API requests
and clearInterval once they were complete.
Not had to do this in Elixir before, so doing a bit of googling ... 🔍

Reading:
https://stackoverflow.com/questions/43892784/what-are-the-equivalent-of-settimeout-and-setinterval-in-elixir

image

The recommended Erlang function is: timer.apply_interval/4

:timer.apply_interval(1000, IO, :puts, ["weee"])

https://www.erlang.org/doc/apps/stdlib/timer.html

Need to write myself a mini example ...

Side note: notice how this question on StackOverflow that has clearly been useful to other people got 4 downvotes:
stackoverflow-downvotes-on-questions
yes, it doesn't conform to the narrow SO question guidelines, but it's still a useful question/answer!
SO is just not a very welcoming (to beginners) environment. 😞
This in a nutshell is why ChatGPT which just repackages the web is so popular: it doesn't "judge" your ("stupid") questions!

@nelsonic nelsonic added enhancement New feature or enhancement of existing functionality help wanted If you can help make progress with this issue, please comment! question A question needs to be answered before progress can be made on this issue discuss Share your constructive thoughts on how to make progress with this issue priority-2 Second highest priority, should be worked on as soon as the Priority-1 issues are finished T25m Time Estimate 25 Minutes technical A technical issue that requires understanding of the code, infrastructure or dependencies elixir Pull requests that update Elixir code research Research required; be specific labels Jan 3, 2025
@nelsonic nelsonic self-assigned this Jan 3, 2025
@nelsonic
Copy link
Member Author

nelsonic commented Jan 3, 2025

@ndrean
Copy link
Contributor

ndrean commented Jan 3, 2025

Maybe not what you are after but about rate limiting as controlled server-side. But I am not sure one should do this server-side...

https://elixirforum.com/t/how-to-rate-limit-a-public-live-view/68081

@nelsonic
Copy link
Member Author

nelsonic commented Jan 7, 2025

I'm using a Task.start combined with :timer.sleep and it works fine. 👌 Crude but functional. 😉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discuss Share your constructive thoughts on how to make progress with this issue elixir Pull requests that update Elixir code enhancement New feature or enhancement of existing functionality help wanted If you can help make progress with this issue, please comment! priority-2 Second highest priority, should be worked on as soon as the Priority-1 issues are finished question A question needs to be answered before progress can be made on this issue research Research required; be specific T25m Time Estimate 25 Minutes technical A technical issue that requires understanding of the code, infrastructure or dependencies
Projects
None yet
Development

No branches or pull requests

2 participants