Skynet open source framework based on Actor mode

The use of multiple processes to solve the problem of high concurrency is the process safety lock. The framework often causes many problems such as deadlocks or unreleased memory due to errors in part of the code. Using a single-process server framework to do message polling and task execution through a thread pool can avoid many problems caused by locks.

The original intention of the framework

  • Parallel Programming in the Era of Many Cores

  • The free dinner is over (The Free Lunch Is Over - Herb Sutter, 2005)

  • E5420(2.5GHz 2004) VS E5-2620v3(2.4GHz 2014)

  • We get more cores, more threads, larger caches, and more bandwidth. We can't get a higher frequency.

Erlang

  • Erlang open source in 1998

  • In 2006, it was introduced into the domestic C++ programmer circle Erlang China User Group. It started in 2007 and is now renamed Effective Cloud User Group.

  • Parallel and distributed Erlang uses the Actor model to increase parallel processing. Error handling mechanisms and storage management are distributed services. Erlang is not good at storage-intensive numerical calculations.

Actor mode

  • Everything is an Actor, everything is an Object? Parallel object-oriented model! Create actors/process messages/send messages. The sent message is decoupled from the sender and communicates asynchronously.

  • Frameworks provided by languages ​​without Actor Akka by Scala, CAF: C++ Actor Framework by C++, React.Net by .net

Skynet

  • 2010 thoughts

  • Implementation based on the first version of Erlang in December 2011

  • Rewrite based on C in July 2012

  • Open source release on August 1, 2012

  • April 22, 2014 v0.1.0

  • November 28, 2014 v0.9.1

CodeBase

  • 5000 lines of C core code message distribution, Actor scheduling, Timer management, socket library based on epoll/kqueue (support TCP/UDP)

  • 1000 lines of C core service code

  • 1000 lines of Lua core library

  • 5000 lines of Lua peripheral libraries Redis/MySQL/MongoDB Driver crypt, sproto, sharedata, etc

  • Single process + optional Lua sandbox

  • Optional distributed structure

  • MIT License

Message scheduling module

Message scheduling module

1. For friends who are engaged in game client development, the interface development is very cumbersome, the client technology is updated very quickly, and they rarely have access to the underlying development. Friends who want to transform to become game server development, learn skynet to understand the overall architecture of the server. Basic components and server-side programming thinking.

2. For the development of small games, skynet is a lightweight game server framework, while learning the server side, can carry out full-stack development

About Skynet, here is a learning video for everyone, which explains the following knowledge points in detail, the video link: https://ke.qq.com/course/2806743?flowToken=1030833

Asynchronous programming

  • coroutine VS callback

  • The memory overhead of Lua5.2 coroutine is only 208 bytes. C threads are difficult to reduce the memory overhead of the stack.

  • Coroutine has good support for exceptions, javascript requires promise mode

  • Take coroutine to avoid excessive GC

  • Remote procedure call RPC Beware of state changes in asynchronous processes

  • Fast failure mode

Actor sandbox

  • C module dynamic library *.so

  • Lua State! = Erlang Process

  • Lua sandbox isolation (30+20K memory footprint)

  • Lua5.2 (Lua JIT 2.0 optional) imperative language, lightweight coroutine, good C interactive performance

  • Lua library + service asynchronous socket library, Launcher, DB Driver

  • Shared data (sharedata/STM)

Protocol

  • In-process messaging text protocol (C service), custom serialization library (Lua service), memory data structure (custom)

  • Cross-process messaging custom protocol, sproto, google proto buffers, json, etc

  • Broadcast and multicast

Distributed solution

  • Skynet supports two distributed solutions. Harbor mode is used to expand the lack of computing power. Cluster mode provides flexibility and can be used together.

  • If not necessary, use cluster to do weak association on one machine

  • No hot update, only hot repair A/B rollover, regular maintenance, reduce complexity

MMORPG server performance

MMORPG

  • E5-2620v1 (2.0 GHz)*2, 32G RAM with hyperthreading enabled, the system can see 24 cores.

  • 10K TCP long connections

  • 200 people injured each other and the delay was within an acceptable range. Computational complexity O(n^2) Lua writing, CPU average cost 0.5% per person, system upper limit 2400%, <5M RAM/person

  • The advantage of multi-core lies in smooth processing time

Momo strives for hegemony

  • E5-2420v1 to E5-2650v3

  • In the initial stress test, the carrying capacity of a single 20K user decreased with the complexity of the business, and the carrying capacity increased linearly as the number of cores increased.

  • The full server peak value is close to 200K. There is no obvious delay in user login operations, and there is no obvious delay in opponent search (global single point).

Typical mobile game cluster

Telecom Mobile Games Cluster

  • Not divided by hardware

  • The player gets the token at the login

  • Players log in to any agent pool

  • Logic server processing ranking

  • Adjust online according to operational needs

  • Unified use of database clusters

  • Avoid single point

Debug and optimize

  • Built-in performance analysis module

  • Built-in monitoring protocol in Lua module

  • Replace CRT memory management library (jemalloc)

  • In-process message passing reduces copy

  • Optimize messages sent to itself

  • Merge timer request

  • Services requiring high performance are written in C/C++ (use with caution)

  • Writing C modules for Lua: AOI, pathfinding, multicast, formula calculation

  • Optimize login, find hot spots, avoid single points

Guess you like

Origin blog.csdn.net/Linuxhus/article/details/112472734