优化 Session 载入机制
This commit is contained in:
parent
aeaedea580
commit
c3fcfa857c
6 changed files with 31 additions and 14 deletions
|
|
@ -18,7 +18,9 @@ var runCmd = &cobra.Command{
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
defer gou.KillPlugins()
|
defer gou.KillPlugins()
|
||||||
Boot()
|
Boot()
|
||||||
engine.Load(config.Conf)
|
cfg := config.Conf
|
||||||
|
cfg.Session.IsCLI = true
|
||||||
|
engine.Load(cfg)
|
||||||
if len(args) < 1 {
|
if len(args) < 1 {
|
||||||
fmt.Println(color.RedString("参数错误: 未指定处理名称"))
|
fmt.Println(color.RedString("参数错误: 未指定处理名称"))
|
||||||
fmt.Println(color.WhiteString("xiang run <处理器名称> [参数表...]"))
|
fmt.Println(color.WhiteString("xiang run <处理器名称> [参数表...]"))
|
||||||
|
|
|
||||||
|
|
@ -63,6 +63,7 @@ type ServiceConfig struct {
|
||||||
type SessionConfig struct {
|
type SessionConfig struct {
|
||||||
Debug bool `json:"debug,omitempty" env:"XIANG_SESSION_DEBUG" envDefault:"false"` // DEBUG 开关
|
Debug bool `json:"debug,omitempty" env:"XIANG_SESSION_DEBUG" envDefault:"false"` // DEBUG 开关
|
||||||
Hosting bool `json:"hosting,omitempty" env:"XIANG_SESSION_HOSTING" envDefault:"true"` // 会话服务器
|
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
|
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"` // 会话服务器端口
|
Port int `json:"port,omitempty" env:"XIANG_SESSION_PORT" envDefault:"3322"` // 会话服务器端口
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -23,7 +23,8 @@ import (
|
||||||
// Load 根据配置加载 API, FLow, Model, Plugin
|
// Load 根据配置加载 API, FLow, Model, Plugin
|
||||||
func Load(cfg config.Config) {
|
func Load(cfg config.Config) {
|
||||||
|
|
||||||
share.DBConnect(cfg.Database) // 创建数据库连接
|
share.DBConnect(cfg.Database) // 创建数据库连接
|
||||||
|
share.SessionConnect(cfg.Session) // 创建会话服务器链接
|
||||||
|
|
||||||
app.Load(cfg) // 加载应用信息
|
app.Load(cfg) // 加载应用信息
|
||||||
LoadEngine(cfg.Path)
|
LoadEngine(cfg.Path)
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ package service
|
||||||
import (
|
import (
|
||||||
"github.com/yaoapp/gou"
|
"github.com/yaoapp/gou"
|
||||||
"github.com/yaoapp/xiang/config"
|
"github.com/yaoapp/xiang/config"
|
||||||
|
"github.com/yaoapp/xiang/share"
|
||||||
)
|
)
|
||||||
|
|
||||||
var shutdown = make(chan bool)
|
var shutdown = make(chan bool)
|
||||||
|
|
@ -10,6 +11,11 @@ var shutdownComplete = make(chan bool)
|
||||||
|
|
||||||
// Start 启动服务
|
// Start 启动服务
|
||||||
func Start() {
|
func Start() {
|
||||||
|
|
||||||
|
if config.Conf.Session.Hosting && config.Conf.Session.IsCLI == false {
|
||||||
|
share.SessionServerStart()
|
||||||
|
}
|
||||||
|
|
||||||
gou.SetHTTPGuards(Guards)
|
gou.SetHTTPGuards(Guards)
|
||||||
gou.ServeHTTP(
|
gou.ServeHTTP(
|
||||||
gou.Server{
|
gou.Server{
|
||||||
|
|
@ -28,6 +34,7 @@ func Start() {
|
||||||
func Stop(onComplete func()) {
|
func Stop(onComplete func()) {
|
||||||
shutdown <- true
|
shutdown <- true
|
||||||
<-shutdownComplete
|
<-shutdownComplete
|
||||||
|
share.SessionServerStop()
|
||||||
gou.KillPlugins()
|
gou.KillPlugins()
|
||||||
onComplete()
|
onComplete()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
package share
|
package share
|
||||||
|
|
||||||
// VERSION 版本号
|
// VERSION 版本号
|
||||||
const VERSION = "0.9.10"
|
const VERSION = "0.9.11"
|
||||||
|
|
||||||
// DOMAIN 许可域(废弃)
|
// DOMAIN 许可域(废弃)
|
||||||
const DOMAIN = "*.iqka.com"
|
const DOMAIN = "*.iqka.com"
|
||||||
|
|
|
||||||
|
|
@ -15,15 +15,13 @@ import (
|
||||||
"github.com/yaoapp/xiang/config"
|
"github.com/yaoapp/xiang/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var sessServer *olric.Olric
|
||||||
|
|
||||||
// SessionConnect 加载会话信息
|
// SessionConnect 加载会话信息
|
||||||
func SessionConnect() {
|
func SessionConnect(conf config.SessionConfig) {
|
||||||
if config.Conf.Session.Hosting {
|
|
||||||
SessionServer()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
var clientConfig = &client.Config{
|
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(),
|
Serializer: serializer.NewMsgpackSerializer(),
|
||||||
Client: config_olric.NewClient(),
|
Client: config_olric.NewClient(),
|
||||||
}
|
}
|
||||||
|
|
@ -37,8 +35,15 @@ func SessionConnect() {
|
||||||
session.MemoryUse(session.ClientDMap{DMap: dm})
|
session.MemoryUse(session.ClientDMap{DMap: dm})
|
||||||
}
|
}
|
||||||
|
|
||||||
// SessionServer 启动会话服务器
|
// SessionServerStop 关闭会话服务器
|
||||||
func SessionServer() {
|
func SessionServerStop() {
|
||||||
|
if sessServer != nil {
|
||||||
|
sessServer.Shutdown(context.Background())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// SessionServerStart 启动会话服务器
|
||||||
|
func SessionServerStart() {
|
||||||
|
|
||||||
c := config_olric.New("local")
|
c := config_olric.New("local")
|
||||||
c.BindAddr = config.Conf.Session.Host
|
c.BindAddr = config.Conf.Session.Host
|
||||||
|
|
@ -51,20 +56,21 @@ func SessionServer() {
|
||||||
// log.Println("[INFO] Olric is ready to accept connections")
|
// 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 {
|
if err != nil {
|
||||||
log.Fatalf("Failed to create Olric instance: %v", err)
|
log.Fatalf("Failed to create Olric instance: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
go func() {
|
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 {
|
if err != nil {
|
||||||
log.Fatalf("olric.Start returned an error: %v", err)
|
log.Fatalf("olric.Start returned an error: %v", err)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
<-ctx.Done()
|
<-ctx.Done()
|
||||||
dm, err := db.NewDMap("local-session")
|
dm, err := sessServer.NewDMap("local-session")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("olric.NewDMap returned an error: %v", err)
|
log.Fatalf("olric.NewDMap returned an error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue