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 timer:send_interval/3 #37

Open
erikareads opened this issue Dec 28, 2023 · 4 comments
Open

Add timer:send_interval/3 #37

erikareads opened this issue Dec 28, 2023 · 4 comments

Comments

@erikareads
Copy link

Can we add timer:send_interval/3 to gleam/erlang/process?

It behaves similarly to send_after, except it spins up a process that repeatedly sends the message every time interval. It's useful for a low-fidelity Tick.

Since Subject(a) is opaque, I can't implement it myself in projects without resorting to FFI workarounds unpacking the Subject tuple by hand.

@erikareads
Copy link
Author

This would be the implementation, similar to send_after:

@external(erlang, "timer", "send_interval")
fn timer_send_interval(a: Int, b: Pid, c: msg) -> Timer

pub fn send_interval(subject: Subject(msg), delay: Int, message: msg) -> Timer {
  timer_send_interval(delay, subject.owner, #(subject.tag, message))
}

@lpil
Copy link
Member

lpil commented Dec 28, 2023

Could be cool! Is there a reason not to use send_after? It can help avoid overloading a process, but I guess most the time that's not a risk.

@erikareads
Copy link
Author

  1. The timer:send_interval function does exactly what I need, without me needing to do additional work. (Outside of needing to sidestep the opaque type currently)
  2. It's not clear to me how you use send_after to achieve the same thing, and after OTP 25, timer is pretty efficient to use: https://www.erlang.org/doc/efficiency_guide/commoncaveats#timer-module

@lpil
Copy link
Member

lpil commented Dec 29, 2023

You'd use send_after again when you receive the previous tick message.

It's not that the timer is expensive, it's that if you ever take too long to handle a tick message you end up with multiple tick messages in the inbox, potentially never managing to catch up resulting in the inbox growing. This then has performance implications and any other messages sent may have delay before they are handled. With send_after this failure mode is impossible.

We could add this function, but I think send_after is what you want for TUIs given the short interval and want to remain responsive even when under high load.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants