Personally think Golang is very suitable for the development of the server side of the game, write down this article to summarize. From the point of view of online games: To successfully operate a online game, to a large extent depends on the player's spontaneous formation of the community. Only the player spontaneously form a stable ecosystem, the game can continue to avoid the emergence of ghost towns. This requires a large number of users to import, at the same time the amount of online users reached a critical point, it is possible to complete. Therefore, it is necessary for many people to be online at the same time. Then look at the common play of online games, in addition to the leaderboard such statistics and data summary of the function, there is no need for a lot of CPU time application. In previous projects, the CPU consumption of the various damage calculations generated by instant combat was not very significant. Players need to complete an operation, through the client-server side-client such a back and forth, in order to achieve high response speed, to meet the player experience, server-side processing can not take up too much time. Therefore, the corresponding CPU consumption per request is relatively small. The IO of online games is mainly divided into two aspects, one is network IO and the other is disk IO. The network IO aspect, can divide into the fine arts resources IO and the game logic instruction Io, here mainly analyzes the game logic the IO. The IO of the game logic is similar to CPU usage, with a small number of bytes per request, but the concurrency is quite high because many people are simultaneously online. In addition, the broadcast of map information will bring more frequent network communication. The disk IO aspect, mainly is the game data preservation. With different databases, there will be a big difference. In previous projects, the process of moving from MySQL to MongoDB, a memory database, is no longer a bottleneck. In general, it is also a buffer of memory to avoid a large number of small chunks of data read and write scenarios. For these characteristics of online games, Golang's language features are ideal for developing game server-side. First, the Go language provides the goroutine mechanism as a native concurrency mechanism. The memory required for each goroutine is very small, and in practice, a large number of goroutine can be initiated to respond to concurrent connections. Goroutine is very similar to the Greenlet in Gevent, when encountering IO blocking, the scheduler automatically switches to another goroutine execution, ensuring that the CPU does not wait for IO to occur. Compared with gevent, Goroutine does not need to use multiple processes to extract the performance of multicore machines without the underlying Gil limitations of Python. By setting the maximum number of threads, you can control the threads that go starts, and each thread executes a goroutine, allowing the CPU to run at full load. At the same time, the Go language provides a unique communication mechanism channel for Goroutine. When the channel reads and writes, it also suspends the current operation of the channel Goroutine, which is a synchronous blocking communication. This achieves the purpose of communication, but also to achieve synchronization, with CSIn the view of the P model, the concurrency model is triggered by a set of processes and events between processes to trigger the resolution of tasks. Although the mainstream programming languages, as long as they are Turing-complete, they can all achieve the same function. But the go language provides this inter-process communication mechanism, it is very elegant to reveal the nature of the Association of Communication, to avoid the previous lock explicit use to the programmer's psychological burden, is indeed a major advantage. For the development of online games, the programmer can write the game logic according to the single-threaded block, no need to consider the problem of thread scheduling, and the problem of data dependency between threads. Because of the channel communication between threads, the data dependencies between threads have been expressed, and the go Scheduler is properly handled. In addition, the Go language provides the GC mechanism, as well as the protection of pointers to use, can greatly reduce the programmer's development pressure, improve development efficiency. Looking to the future, I expect the go language community to provide more isolation mechanisms between goroutine. Individuals highly appreciate the philosophy of the Erlang community's brittle disintegration, pushing the application to break down as early as possible, and then fork out new processes to process new requests. For the co-process mechanism, it is necessary for the programmer to ensure that the function executed does not have a dead loop, causing the thread to die.
Why go language is suitable for developing online games server side