Leaf console - GameKong/leafserver GitHub Wiki

什么是 Leaf console

在 Leaf console 中我们可以输入交互式命令和 Leaf server 交互。

Leaf console 使用

首先,我们需要配置 Leaf 的 console 的端口号(但不能配置 IP,出于安全考虑,只能通过本地访问 console)。编辑 Leafserver 的 bin/conf/server.json 文件,增加 ConsolePort 配置项,这里我增加 "ConsolePort": 3333(使用端口 3333 通讯)。现在在 game/internal 下建立一个 command.go 文件,敲入以下代码:

package internal

import (
	"fmt"
)

func init() {
	skeleton.RegisterCommand("echo", "echo user inputs", commandEcho)
}

func commandEcho(args []interface{}) interface{} {
	return fmt.Sprintf("%v", args)
}

这样,Leafserver 就可以接受命令 echo 了。我们可以通过 telnet 连接 Leafserver 来输入命令(也可以使用其他类似工具),例如:

telnet localhost 3333

现在我们敲击命令 help,可以得到以下输出:

Leaf# help
Commands:
help - this help text
cpuprof - CPU profiling for the current process
prof - writes a pprof-formatted snapshot
echo - echo user inputs
quit - exit console

这里可以看到命令和它们的简要帮助信息。除了我们自定义的命令 echo 之外,还有几个 Leaf 内置命令,这个我们后面再谈。我们尝试使用 echo 命令:

Leaf# echo a b c
[a b c]

Leaf 内置命令简述

Leaf# cpuprof help
cpuprof writes runtime profiling data in the format expected by 
the pprof visualization tool

Usage: cpuprof start|stop
  start - enables CPU profiling
  stop  - stops the current CPU profile

我们通过敲入 cpuprof help 获得上述帮助信息。cpuprof 用于开启(cpuprof start)和关闭(cpuprof stop)CPU profiling(至于 profiling 的内容就不是本文应该介绍的了,建议阅读:https://blog.golang.org/profiling-go-programs)。

Leaf# prof help
prof writes runtime profiling data in the format expected by 
the pprof visualization tool

Usage: prof goroutine|heap|thread|block
  goroutine - stack traces of all current goroutines
  heap      - a sampling of all heap allocations
  thread    - stack traces that led to the creation of new OS threads
  block     - stack traces that led to blocking on synchronization primitives

我们通过敲入 prof help 获得上述帮助信息。prof 用于获取运行时的 profiling 数据。在进行内存调优时,我们可能会使用到 prof heap

无论是 cpuprof 命令还是 prof 命令,输出文件的路径是可以配置的,我们只需要编辑 Leafserver 的 bin/conf/server.json 文件,增加 ProfilePath 配置项即可。