优化 Session 载入机制

This commit is contained in:
Max 2021-11-29 10:27:38 +08:00
parent aeaedea580
commit c3fcfa857c
6 changed files with 31 additions and 14 deletions

View file

@ -18,7 +18,9 @@ var runCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
defer gou.KillPlugins()
Boot()
engine.Load(config.Conf)
cfg := config.Conf
cfg.Session.IsCLI = true
engine.Load(cfg)
if len(args) < 1 {
fmt.Println(color.RedString("参数错误: 未指定处理名称"))
fmt.Println(color.WhiteString("xiang run <处理器名称> [参数表...]"))

View file

@ -63,6 +63,7 @@ type ServiceConfig struct {
type SessionConfig struct {
Debug bool `json:"debug,omitempty" env:"XIANG_SESSION_DEBUG" envDefault:"false"` // DEBUG 开关
Hosting bool `json:"hosting,omitempty" env:"XIANG_SESSION_HOSTING" envDefault:"true"` // 会话服务器
IsCLI bool `json:"iscli,omitempty" env:"XIANG_SESSION_ISCLI" envDefault:"false"` // 是否为客户端启动
Host string `json:"host,omitempty" env:"XIANG_SESSION_HOST" envDefault:"127.0.0.1"` // 会话服务器IP
Port int `json:"port,omitempty" env:"XIANG_SESSION_PORT" envDefault:"3322"` // 会话服务器端口
}

View file

@ -23,7 +23,8 @@ import (
// Load 根据配置加载 API, FLow, Model, Plugin
func Load(cfg config.Config) {
share.DBConnect(cfg.Database) // 创建数据库连接
share.DBConnect(cfg.Database) // 创建数据库连接
share.SessionConnect(cfg.Session) // 创建会话服务器链接
app.Load(cfg) // 加载应用信息
LoadEngine(cfg.Path)

View file

@ -3,6 +3,7 @@ package service
import (
"github.com/yaoapp/gou"
"github.com/yaoapp/xiang/config"
"github.com/yaoapp/xiang/share"
)
var shutdown = make(chan bool)
@ -10,6 +11,11 @@ var shutdownComplete = make(chan bool)
// Start 启动服务
func Start() {
if config.Conf.Session.Hosting && config.Conf.Session.IsCLI == false {
share.SessionServerStart()
}
gou.SetHTTPGuards(Guards)
gou.ServeHTTP(
gou.Server{
@ -28,6 +34,7 @@ func Start() {
func Stop(onComplete func()) {
shutdown <- true
<-shutdownComplete
share.SessionServerStop()
gou.KillPlugins()
onComplete()
}

View file

@ -1,7 +1,7 @@
package share
// VERSION 版本号
const VERSION = "0.9.10"
const VERSION = "0.9.11"
// DOMAIN 许可域(废弃)
const DOMAIN = "*.iqka.com"

View file

@ -15,15 +15,13 @@ import (
"github.com/yaoapp/xiang/config"
)
var sessServer *olric.Olric
// SessionConnect 加载会话信息
func SessionConnect() {
if config.Conf.Session.Hosting {
SessionServer()
return
}
func SessionConnect(conf config.SessionConfig) {
var clientConfig = &client.Config{
Servers: []string{fmt.Sprintf("%s:%d", config.Conf.Session.Host, config.Conf.Session.Port)},
Servers: []string{fmt.Sprintf("%s:%d", conf.Host, conf.Port)},
Serializer: serializer.NewMsgpackSerializer(),
Client: config_olric.NewClient(),
}
@ -37,8 +35,15 @@ func SessionConnect() {
session.MemoryUse(session.ClientDMap{DMap: dm})
}
// SessionServer 启动会话服务器
func SessionServer() {
// SessionServerStop 关闭会话服务器
func SessionServerStop() {
if sessServer != nil {
sessServer.Shutdown(context.Background())
}
}
// SessionServerStart 启动会话服务器
func SessionServerStart() {
c := config_olric.New("local")
c.BindAddr = config.Conf.Session.Host
@ -51,20 +56,21 @@ func SessionServer() {
// log.Println("[INFO] Olric is ready to accept connections")
}
db, err := olric.New(c)
var err error
sessServer, err = olric.New(c)
if err != nil {
log.Fatalf("Failed to create Olric instance: %v", err)
}
go func() {
err = db.Start() // Call Start at background. It's a blocker call.
err = sessServer.Start() // Call Start at background. It's a blocker call.
if err != nil {
log.Fatalf("olric.Start returned an error: %v", err)
}
}()
<-ctx.Done()
dm, err := db.NewDMap("local-session")
dm, err := sessServer.NewDMap("local-session")
if err != nil {
log.Fatalf("olric.NewDMap returned an error: %v", err)
}