转自: https://blog.smartlogic.io/elixir-dynamic-supervisors/

I have been working on my side project Grapevine, a community site for text based multiplayer games (MUDs.) The existing games primarily have a telnet interface for playing. This generally involves using a local client or hoping the game provides a web client for you. So I've been working on a general purpose client that any game can use. You can see it in action for my game MidMUD.

This involves a new web client that uses Phoenix Channels to spin up a backing :gen_tcp process. This process is started inside of a DynamicSupervisor, a new feature introduced in Elixir 1.6. We place the process inside of the supervisor to keep the process alive across page reloads. Lets see how it works!

Basic Flow

When a new web client starts they join a channel named play:client. The channel ensures that the game they are trying to connect to exists and starts the backing process.

{:ok, pid} = WebClient.connect(socket.assigns.user,
game_id: socket.assigns.game.id,
host: connection.host,
port: connection.port,
channel_pid: socket.channel_pid
)

This Client process starts itself inside of a DynamicSupervisor as a transientprocess and saves the calling channel PID in order to forward any received data to the front end. This uses the DynamicSupervisor.start_child/2 function.

def start_client(callback_module, opts) do
spec = {Client, [module: callback_module] ++ opts}
DynamicSupervisor.start_child(__MODULE__, spec)
end

Once the Client process is alive it parses the telnet stream and forwards any text directly to the web front end.

Handling Page Reloads

The Client process is globally registered with the user's ID and the game's ID as part of the name, {:webclient, {1, 1}}. With this, we can check :global to see if the process already exists before starting a new process. If the process exists we overtake the Client process with our new channel.

def connect(user, opts) do
case :global.whereis_name(pid(user, opts)) do
:undefined ->
ClientSupervisor.start_client(__MODULE__, opts ++ [name: {:global, pid(user, opts)}]) pid ->
set_channel(pid, opts[:channel_pid])
{:ok, pid}
end
end

set_channel/2 sends a message to the process which sets the internal channel_pid state for forwarding text to the web client.

Conclusion

With this setup I'm able to have a place to supervise Client processes and allow for session "stickiness" on page reloads. The DynamicSupervisor will delete the child_spec of the Client process after it terminates with a :normal reason. Otherwise the process will be restarted allowing for minor hiccups in the network.

All of this code is open source on GitHub, check out the Grapevine repo. I also worked on this during my Elixir live coding stream. I'll be back doing more live coding on that channel every Monday from 12PM-1PM Eastern.

 
 
 
 

Using Elixir Dynamic Supervisors的更多相关文章

  1. [Erlang 0108] Elixir 入门

    Erlang Resources里面关于Elixir的资料越来越多,加上Joe Armstrong的这篇文章,对Elixir的兴趣也越来越浓厚,投入零散时间学习了一下.零零散散,测试代码写了一些,Ev ...

  2. 基于Erlang VM的函数式编程语言Elixir

    Elixir官网:http://elixir-lang.org/ Elixir是一种函数式动态语言,用于构建可伸缩.易维护的应用程序. Elixir是基于Erlang VM的,其广为人知的特点是运行低 ...

  3. Elixir游戏服设计四

    上章说到我们要引入syn https://github.com/ostinelli/syn/ 看过文档,它并没有直接提供{via, Module, Name} 相关的方法.我们需要封装一下. Name ...

  4. Elixir游戏服设计三

    玩家进程用gen_server来建模,我不直接使用 use GenServer, 而是使用exactor,该库可以去掉反锁的接口定义. 我们新建一个 player_server_manager app ...

  5. Connecting Elixir Nodes with libcluster, locally and on Kubernetes

    转自:https://www.poeticoding.com/connecting-elixir-nodes-with-libcluster-locally-and-on-kubernetes/ Tr ...

  6. Install Erlang and Elixir in CentOS 7

    In this tutorial, we will be discussing about how to install Erlang and Elixir in CentOS 7 minimal s ...

  7. PatentTips - Systems, methods, and devices for dynamic resource monitoring and allocation in a cluster system

    BACKGROUND  1. Field  The embodiments of the disclosure generally relate to computer clusters, and m ...

  8. var和dynamic的区别

    1.var 1.均是声明动态类型的变量. 2.在编译阶段已经确定类型,在初始化的时候必须提供初始化的值. 3.无法作为方法参数类型,也无法作为返回值类型. 2.dynamic 1.均是声明动态类型的变 ...

  9. 遍历dynamic的方式

    一.遍历ExpandoObject /// <summary> /// 遍历ExpandoObject /// </summary> [TestMethod] public v ...

随机推荐

  1. Centos防火墙设置与端口开放的方法

    Centos升级到7之后,内置的防火墙已经从iptables变成了firewalld.所以,端口的开启还是要从两种情况来说明的,即iptables和firewalld.更多关于CentOs防火墙的最新 ...

  2. ftp上传操作

    采用 :FtpWebRequest 进行操作ftp. 1.代码上传文件必须是被动模式  UsePassive=false 2.最好采用二进制传输 UseBinary=true 注意缓冲区大小,还有注意 ...

  3. 数控AGC实现(转)

    相关链接:    一种混合式高动态范围AGC算法与FPGA实现     http://www.sohu.com/a/221438387_781333 基于FPGA的快速自动增益控制系统设计      ...

  4. Unity最新版打包AssetBundle和加载的方法

    一.设置assetBundleName二.构建AssetBundle包三.上传AssetBundle到服务器四.把AssetBundle放到本地五.操作AssetBundle六.完整例子七.Asset ...

  5. MVC中如何避免POST请求中出现的重复提交

    使用惯了Asp.Net的服务器控件了, 突然转到MVC框架上来遇到这么个问题. 比如说网速慢的时候, 用户频繁的点击提交按钮, 或者是按F5刷新页面等等.解决方法很简单. 解决方案 1. 在页面生成时 ...

  6. ylz 开发学习笔记一(注意事项)

    Eclipse快捷键 断点 F6执行下一步 等等 C+S+R 搜索文件名字      C+鼠标移动左击 跳入类文件    jsp 文件的断点是使用 debugger  之后再chrom 用F10单步调 ...

  7. [SpringMVC-初始] 初始SpringMVC

    关于SpringMVC的简介 A.SpringMVC概述 作用: SpringMVC框架是SpringFramWork中实现了MVC架构模式的轻量级子框架 用于将WEB层进行职责解耦,松散的耦合可插拔 ...

  8. Linux平台 获取程序依赖文件

    创建sh脚本文件 =========================================================== #!/bin/sh exe=$1 #发布的程序名称des=$2 ...

  9. web 架构 /http协议,状态码,django中常用命令

    什么是web应用? web应用 架构 :B/S架构 | C/S架构 网站:BS架构其实就是应用程序: B是浏览器 S是sever(实现了wsgi协议,实现了socket的服务端) + applicat ...

  10. python之pandas用法大全

    python之pandas用法大全 更新时间:2018年03月13日 15:02:28 投稿:wdc 我要评论 本文讲解了python的pandas基本用法,大家可以参考下 一.生成数据表1.首先导入 ...