-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Add gen_server timeout
example
#14530
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -349,6 +349,41 @@ defmodule GenServer do | |||||
message arriving, `handle_info/2` is called with `:timeout` as the first | ||||||
argument. | ||||||
|
||||||
For example: | ||||||
|
||||||
defmodule Counter do | ||||||
use GenServer | ||||||
|
||||||
@timeout 5000 | ||||||
|
||||||
@impl true | ||||||
def init(count) do | ||||||
{:ok, count, @timeout} | ||||||
end | ||||||
|
||||||
@impl true | ||||||
def handle_call(:succ, _from, count) do | ||||||
new_count = count + 1 | ||||||
{:reply, new_count, new_count, @timeout} | ||||||
end | ||||||
|
||||||
@impl true | ||||||
def handle_info(:timeout, count) do | ||||||
{:stop, :normal, count} | ||||||
end | ||||||
end | ||||||
|
||||||
A `Counter` server will exit with `:normal` if there are no messages in 5 seconds | ||||||
after the initialization or after the last `succ` call: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't understand what There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. https://en.wikipedia.org/wiki/Successor_function There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would suggest to not use abbreviations in documentation. It's clearer to use :success or :increment instead. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
{:ok, counter_pid} = GenServer.start(Counter, 50) | ||||||
GenServer.call(counter_pid, :succ) | ||||||
#=> 51 | ||||||
|
||||||
# After 5 secs | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
Process.alive?(counter_pid) | ||||||
#=> false | ||||||
|
||||||
## When (not) to use a GenServer | ||||||
|
||||||
So far, we have learned that a `GenServer` can be used as a supervised process | ||||||
|
Uh oh!
There was an error while loading. Please reload this page.