How should Erlang deal with common data

为君一笑 提交于 2019-12-21 02:53:27

问题


(I have almost no experience with Erlang, just read some books) Suppose that I'm building game server using Erlang.

It's very common circulation each user to check something(eg, finding the closest player), so there's a manager class for that usually.

In above case, we use mutex lock.

As I know Erlang create new Erlang-process per each TCP connection(user session) normally. Then, how the list of user session can be circulated?

If I have parent process for those user sessions and ask to parent process, it can be a bottle-neck?


回答1:


Now, before you continue, you may look at these questions and answers: Erlang: Distributed work on an array , Erlang gen_server with long-running tasks , and What is the best, most efficient, Client pool technique with Erlang

Now, learn about ETS Tables and Mnesia. Then you can perhaps look at GProc. Making a process to hold state data especially for a game is dangerous coz the state may get lost when the process exits. Its easy to restart a crashed process, but the data it was holding is lost. Normally, we need in-memory data storage which cannot get lost UNLESS the WHOLE erlang VM goes Down (In which case, if you need persistence beyond RAM, you will need DETS Tables or Mnesia still ).

Process Dictionaries are usually discouraged, yet they still prove to be extremely useful. However, personally, ETS Tables have saved me alot. They can be transfered from one crashing process to the next one alive. They can be dumped to disk or loaded from Disk and they can handle a lot of data.

Just study more on IN-Memory storage used in Erlang. Its BTW also possible to use storage outside the erlang VM, like Memcached, Raik, CouchBase, e.t.c. However, in some cases, a Game can be broken down to just a data structure like Queues, or Lists or Records , a data structure that can be saved within the process dictionary. Also, it will depend on how the game services to clients are rendered. If the game uses HTTP (RESTFUL), then you will also learn which Erlang libraries to apply (Mochiweb, Yaws, e.t.c.). Just follow this whole tutorial, you will discover everything.



来源:https://stackoverflow.com/questions/11340878/how-should-erlang-deal-with-common-data

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!