Skip to content

Support for handle_continue #234

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

Closed
wants to merge 4 commits into from
Closed

Conversation

palm86
Copy link

@palm86 palm86 commented May 16, 2019

This tries so address issue #227. But I need some help to figure it out. I've done what seems necesary to me, but many tests fail with a timeout now and I haven't given any though to additional tests yet.

@@ -882,11 +882,17 @@ defmodule GenStage do

@callback init(args :: term) ::
{:producer, state}
| {:producer, state, :hibernate | {:continue, term}}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current PR doesn't handle hiberrnate, does it? In any case, there are other discussions we need to have regarding hibernate, so probably better to focus on continue for now. :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, but the existing documentation for the init function mentions hibernate, which confused me. I'll remove the hibernate reference from the docs (around lines 833-835) and from this callback.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll remove the hibernate reference from the docs (around lines 833-835) and from this callback.

Fantastic, thank you!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, actually, the handle_info callback (and the others) also contains :hibernate. I guess that was a copy paste from the GenServer docs?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! Actually we do support hibernate there but not on init. So I guess we crossed this line anyway. So I would go ahead and support it on init too!

lib/gen_stage.ex Outdated
@@ -1726,7 +1770,7 @@ defmodule GenStage do
end
end

defp init_producer(mod, opts, state) do
defp init_producer(mod, opts, state, continue \\ :no_continue) do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like to use default arguments to private functions. We can directly pass :no_continue when calling this function. :)

@josevalim
Copy link
Member

Looks great, can you please add some tests too? Thanks!

@palm86
Copy link
Author

palm86 commented May 17, 2019

Looks great, can you please add some tests too? Thanks!

I'd love to, but would welcome some pointers regarding why the current tests fail:

     test/gen_stage_test.exs:1584
     ** (EXIT from #PID<0.815.0>) an exception was raised:
         ** (ErlangError) Erlang error: :timeout_value
             (stdlib) gen_server.erl:411: :gen_server.loop/7
             (stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3

If I only update producers to handle continue in the init, all is well, but also updating the consumers and producer-consumers to handle continue in the init, the tests fail. I suspect it has something to do with subscriptions.

@josevalim
Copy link
Member

josevalim commented May 17, 2019 via email

lib/gen_stage.ex Outdated
@@ -2407,7 +2407,7 @@ defmodule GenStage do

## Consumer helpers

defp consumer_init_subscribe(producers, stage, continue) do
defp consumer_init_subscribe(producers, stage, continue_or_hibernate) do
fold_fun = fn
to, {:ok, stage, :no_continue} ->
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need this clause if we have a case at the end.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, I'll remove it.

lib/gen_stage.ex Outdated
:lists.foldl(fold_fun, {:ok, stage, continue_or_hibernate}, producers)
|> case do
{:ok, stage, :no_continue} -> {:ok, stage}
{:ok, stage, continue_or_hibernate} -> {:ok, stage, continue_or_hibernate}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need this clause either.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, the otherwise is enough. Will chang.e

@josevalim
Copy link
Member

Looks good, I have added some comments. Next step is to add some tests too. :)

@palm86
Copy link
Author

palm86 commented May 27, 2019

Looks good, I have added some comments. Next step is to add some tests too. :)

Thanks. I think there is still some functionality missing though. Currently, I only handle continue as returned from the init. I should probably handle continue as returned from the other callback implementations as well.

@josevalim
Copy link
Member

I have just realized something. Today, if you return subscribe_to in your init callback, handle_subscribe would be called BEFORE the handle_continue, and it probably should be called after. I think that, instead of return {:continue, ...} and let the underlying GenServer take care of it, we should probably handle and call continue ourselves, as we need to do before handle_subscribe.

@palm86
Copy link
Author

palm86 commented May 27, 2019 via email

@josevalim
Copy link
Member

Ping!

@palm86
Copy link
Author

palm86 commented Jul 15, 2019 via email

@josevalim
Copy link
Member

Hi @palm86, it seems we both got busy with other things, so I will go ahead and close this for now. If you still have an interest on this feature, we will gladly reopen it. Thanks!

@josevalim josevalim closed this Feb 3, 2020
@palm86 palm86 mentioned this pull request Sep 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants