原文链接 leaf模块 package leaf import ( "github.com/name5566/leaf/cluster" "github.com/name5566/leaf/conf" "github.com/name5566/leaf/console" "github.com/name5566/leaf/log" "github.com/name5566/leaf/module" "os" "os/signal" ) func Run(mods ...module.Module) { // logger if conf.LogLevel != "" { logger, err := log.New(conf.LogLevel, conf.LogPath, conf.LogFlag) if err != nil { panic(err) } log.Export(logger) defer logger.Close() } log.Release("Leaf %v starting up", version) // module for i := 0; i < len(mods); i++ { module.Register(mods[i]) } module.Init() // cluster cluster.Init() // console console.Init() // close c := make(chan os.Signal, 1) signal.Notify(c, os.Interrupt, os.Kill) sig := <-c log.Release("Leaf closing down (signal: %v)", sig) console.Destroy() cluster.Destroy() module.Destroy() } 复制代码 main()模块调用 func main() { lconf.LogLevel = conf.Server.LogLevel lconf.LogPath = conf.Server.LogPath lconf.LogFlag = conf.LogFlag lconf.ConsolePort = conf.Server.ConsolePort lconf.ProfilePath = conf.Server.ProfilePath leaf.Run( game.Module, gate.Module, login.Module, ) } 复制代码 package module import ( "github.com/name5566/leaf/conf" "github.com/name5566/leaf/log" "runtime" "sync" ) type Module interface { OnInit() OnDestroy() Run(closeSig chan bool) } type module struct { mi Module closeSig chan bool wg sync.WaitGroup } var mods []*module func Register(mi Module) { m := new(module) m.mi = mi m.closeSig = make(chan bool, 1) mods = append(mods, m) } func Init() { for i := 0; i < len(mods); i++ { mods[i].mi.OnInit() } for i := 0; i < len(mods); i++ { m := mods[i] m.wg.Add(1) go run(m) } } func Destroy() { for i := len(mods) - 1; i >= 0; i-- { m := mods[i] m.closeSig <- true m.wg.Wait() destroy(m) } } func run(m *module) { m.mi.Run(m.closeSig) m.wg.Done() } func destroy(m *module) { defer func() { if r := recover(); r != nil { if conf.LenStackBuf > 0 { buf := make([]byte, conf.LenStackBuf) l := runtime.Stack(buf, false) log.Error("%v: %s", r, buf[:l]) } else { log.Error("%v", r) } } }() m.mi.OnDestroy() } 复制代码