Repository: phoenixframework/phoenix
Pull request #3197 (2018-12-04 21:20:49)

Automatically hibernate channels after some time


Patch:
diff --git a/lib/phoenix/channel.ex b/lib/phoenix/channel.ex
index 71d53f574..427c7f65a 100644
--- a/lib/phoenix/channel.ex
+++ b/lib/phoenix/channel.ex
@@ -185,16 +185,15 @@ defmodule Phoenix.Channel do
 
   You have three options to choose from when shutting down a channel:
 
-    * `:normal` - in such cases, the exit won't be logged, there is no restart
-      in transient mode, and linked processes do not exit
+    * `:normal` - in such cases, the exit won't be logged and linked processes
+      do not exit
 
     * `:shutdown` or `{:shutdown, term}` - in such cases, the exit won't be
-      logged, there is no restart in transient mode, and linked processes exit
-      with the same reason unless they're trapping exits
+      logged and linked processes exit with the same reason unless they're
+      trapping exits
 
-    * any other term - in such cases, the exit will be logged, there are
-      restarts in transient mode, and linked processes exit with the same reason
-      unless they're trapping exits
+    * any other term - in such cases, the exit will be logged and linked
+      processes exit with the same reason unless they're trapping exits
 
   ## Subscribing to external topics
 
@@ -249,6 +248,16 @@ defmodule Phoenix.Channel do
         {:noreply, socket}
       end
 
+  ## Hibernation
+
+  From Erlang/OTP 20, channels automatically hibernate to save memory
+  after 15_000 milliseconds of inactivity. This can be customized by
+  passing the `:hibernate_after` option to `use Phoenix.Channel`:
+
+      use Phoenix.Channel, hibernate_after: 60_000
+
+  You can also set it to `:infinity` to fully disable it.
+
   ## Logging
 
   By default, channel `"join"` and `"handle_in"` events are logged, using
@@ -350,12 +359,13 @@ defmodule Phoenix.Channel do
       @phoenix_intercepts []
       @phoenix_log_join Keyword.get(opts, :log_join, :info)
       @phoenix_log_handle_in Keyword.get(opts, :log_handle_in, :debug)
+      @phoenix_hibernate_after Keyword.get(opts, :hibernate_after, 15_000)
 
       import unquote(__MODULE__)
       import Phoenix.Socket, only: [assign: 3]
 
       def start_link(triplet) do
-        GenServer.start_link(Phoenix.Channel.Server, triplet)
+        GenServer.start_link(Phoenix.Channel.Server, triplet, hibernate_after: @phoenix_hibernate_after)
       end
 
       def __socket__(:private) do
diff --git a/lib/phoenix/presence.ex b/lib/phoenix/presence.ex
index 3b041a97b..e16c2581b 100644
--- a/lib/phoenix/presence.ex
+++ b/lib/phoenix/presence.ex
@@ -5,9 +5,15 @@ defmodule Phoenix.Presence do
   This behaviour provides presence features such as fetching
   presences for a given topic, as well as handling diffs of
   join and leave events as they occur in real-time. Using this
-  module defines a supervisor and allows the calling module to
-  implement the `Phoenix.Tracker` behaviour which starts a
-  tracker process to handle presence information.
+  module defines a supervisor and a module that implements the
+  `Phoenix.Tracker` behaviour that uses `Phoenix.PubSub` to
+  broadcast presence updates.
+
+  In case you want to use only a subset of the functionality
+  provided by `Phoenix.Presence`, such as tracking processes
+  but without broadcasting updates, we recommend that you look
+  at the `Phoenix.Tracker` functionality from the `phoenix_pubsub`
+  project.
 
   ## Example Usage
 
diff --git a/priv/templates/phx.gen.schema/schema.ex b/priv/templates/phx.gen.schema/schema.ex
index fa1a6e705..6ef1af53d 100644
--- a/priv/templates/phx.gen.schema/schema.ex
+++ b/priv/templates/phx.gen.schema/schema.ex
@@ -1,7 +1,6 @@
 defmodule <%= inspect schema.module %> do
   use Ecto.Schema
   import Ecto.Changeset
-
 <%= if schema.binary_id do %>
   @primary_key {:id, :binary_id, autogenerate: true}
   @foreign_key_type :binary_id<% end %>


Test patch:
diff --git a/test/mix/tasks/phx.gen.schema_test.exs b/test/mix/tasks/phx.gen.schema_test.exs
index d6f769e8f..b701702d1 100644
--- a/test/mix/tasks/phx.gen.schema_test.exs
+++ b/test/mix/tasks/phx.gen.schema_test.exs
@@ -270,6 +270,16 @@ defmodule Mix.Tasks.Phx.Gen.SchemaTest do
     end
   end
 
+  test "generates schema without extra line break", config do
+    in_tmp_project config.test, fn ->
+      Gen.Schema.run(~w(Blog.Post posts title))
+
+      assert_file "lib/phoenix/blog/post.ex", fn file ->
+        assert file =~ "import Ecto.Changeset\n\n  schema"
+      end
+    end
+  end
+
   describe "inside umbrella" do
     test "raises with false context_app", config do
       in_tmp_umbrella_project config.test, fn ->