Skip to content

Instantly share code, notes, and snippets.

@xiongxin
Last active September 1, 2018 09:47
Show Gist options
  • Select an option

  • Save xiongxin/9eb2cee3f1174b7baadc0b55487847f5 to your computer and use it in GitHub Desktop.

Select an option

Save xiongxin/9eb2cee3f1174b7baadc0b55487847f5 to your computer and use it in GitHub Desktop.

Revisions

  1. xiongxin revised this gist Sep 1, 2018. 5 changed files with 0 additions and 0 deletions.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
  2. xiongxin revised this gist Sep 1, 2018. 6 changed files with 121 additions and 11 deletions.
    11 changes: 0 additions & 11 deletions Initialization.md
    Original file line number Diff line number Diff line change
    @@ -1,11 +0,0 @@
    Client

    ```elixir
    def start_link(opts \\ []) do
    GenServer.start_link(__MODULE__, :ok, opts)
    end
    ```
    Returns
    ```elixir
    {:ok, pid}
    ```
    15 changes: 15 additions & 0 deletions Out of band messages.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    ```
    def handle_info(msg, state) do
    new_state = f(state, msg)
    {:noreply, new_state}
    end
    ```


    ```
    {:noreply, new_state}
    {:noreply, new_state, 5_000}
    {:noreply, new_state, :hibernate}
    RETURN VAL
    {:stop, reason*, new_state}
    ```
    22 changes: 22 additions & 0 deletions Termination.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    ```
    def stop(pid, reason \\ :normal, timeout \\ :infinity) do
    GenServer.stop(pid, reason, timeout)
    end
    ```

    回调函数

    ```
    def terminate(reason, state) do
    # Perform cleanup here
    # …
    end
    ```

    reason参数的示例值
    ```
    :normal
    :shutdown
    {:shutdown, term}
    term
    ```
    31 changes: 31 additions & 0 deletions 初始化服务器.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    来源:https://raw.githubusercontent.com/benjamintanweihao/elixir-cheatsheets/master/GenServer_CheatSheet.pdf

    Client

    ```elixir
    def start_link(opts \\ []) do
    GenServer.start_link(__MODULE__, :ok, opts)
    end
    ```
    Returns
    ```elixir
    {:ok, pid}
    ```

    Callback
    ```
    def init(:ok) do
    state = init_state()
    {:ok, state} # 这里的 :ok 对应初始化的 :ok
    end
    ```

    回调可以返回的值
    ```
    {:ok, state}
    {ok, state, 5_000}
    {:ok, state, :hibernate}
    {:stop, reason*}
    RETURN VALUES
    :ignore
    ```
    26 changes: 26 additions & 0 deletions 同步请求操作.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    Client发送请求
    ```
    def sync_op(pid, args) do
    GenServer.call(pid, {:sync_op, args}) # :sync_op 是我们指定操作的名称
    end
    ```

    Callback
    ```
    def handle_call({:sync_op, args}, from, state) do
    new_state = f(state, args)
    {:reply, new_state}
    end
    ```

    回调返回值示例
    ```
    {:reply, reply, new_state}
    {:reply, reply, new_state, 5_000}
    {:reply, reply, new_state, :hibernate}
    {:noreply, new_state}
    {:noreply, new_state, 5_000}
    {:noreply, new_state, :hibernate}
    {:stop, reason*, reply, new_state}
    {:stop, reason*, new_state}
    ```
    27 changes: 27 additions & 0 deletions 异步操作.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    `handle_cast/2 must be used for asynchronous requests, when you don’t care about a reply. A cast does not even guarantee the server has received the message and, for this reason, should be used sparingly. For example, the create/2 function we have defined in this chapter should have used call/2. We have used cast/2 for didactic purposes.`

    根据文档,异步操作不保证一定能发送成功,还是尽量少使用

    Client发送请求
    ```
    def async_op(pid, args) do
    GenServer.cast(pid, {:async_op, args})
    end
    ```

    Callback
    ```
    def handle_cast({:async_op, args}, state) do
    new_state = f(state, args)
    {:noreply, new_state}
    end
    ```

    回调返回值示例
    ```
    {:noreply, new_state}
    {:noreply, new_state, 5_000}
    {:noreply, new_state, :hibernate}
    RETURN VAL
    {:stop, reason*, new_state}
    ```
  3. xiongxin created this gist Sep 1, 2018.
    11 changes: 11 additions & 0 deletions Initialization.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    Client

    ```elixir
    def start_link(opts \\ []) do
    GenServer.start_link(__MODULE__, :ok, opts)
    end
    ```
    Returns
    ```elixir
    {:ok, pid}
    ```