什么是 Leaf console

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

Leaf console 使用

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

  1. package internal
  2. import (
  3. "fmt"
  4. )
  5. func init() {
  6. skeleton.RegisterCommand("echo", "echo user inputs", commandEcho)
  7. }
  8. func commandEcho(args []interface{}) interface{} {
  9. return fmt.Sprintf("%v", args)
  10. }

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

  1. telnet localhost 3333

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

  1. Leaf# help
  2. Commands:
  3. help - this help text
  4. cpuprof - CPU profiling for the current process
  5. prof - writes a pprof-formatted snapshot
  6. echo - echo user inputs
  7. quit - exit console

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

  1. Leaf# echo a b c
  2. [a b c]

Leaf 内置命令简述

  1. Leaf# cpuprof help
  2. cpuprof writes runtime profiling data in the format expected by
  3. the pprof visualization tool
  4. Usage: cpuprof start|stop
  5. start - enables CPU profiling
  6. stop - stops the current CPU profile

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

  1. Leaf# prof help
  2. prof writes runtime profiling data in the format expected by
  3. the pprof visualization tool
  4. Usage: prof goroutine|heap|thread|block
  5. goroutine - stack traces of all current goroutines
  6. heap - a sampling of all heap allocations
  7. thread - stack traces that led to the creation of new OS threads
  8. block - stack traces that led to blocking on synchronization primitives

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

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