Skip to content

Commit

Permalink
优化 Session 载入机制
Browse files Browse the repository at this point in the history
  • Loading branch information
trheyi committed Nov 29, 2021
1 parent aeaedea commit c3fcfa8
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 14 deletions.
4 changes: 3 additions & 1 deletion cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 <处理器名称> [参数表...]"))
Expand Down
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"` // 会话服务器端口
}
Expand Down
3 changes: 2 additions & 1 deletion engine/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@ package service
import (
"github.com/yaoapp/gou"
"github.com/yaoapp/xiang/config"
"github.com/yaoapp/xiang/share"
)

var shutdown = make(chan bool)
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{
Expand All @@ -28,6 +34,7 @@ func Start() {
func Stop(onComplete func()) {
shutdown <- true
<-shutdownComplete
share.SessionServerStop()
gou.KillPlugins()
onComplete()
}
2 changes: 1 addition & 1 deletion share/const.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package share

// VERSION 版本号
const VERSION = "0.9.10"
const VERSION = "0.9.11"

// DOMAIN 许可域(废弃)
const DOMAIN = "*.iqka.com"
Expand Down
28 changes: 17 additions & 11 deletions share/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
Expand All @@ -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
Expand All @@ -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)
}
Expand Down

0 comments on commit c3fcfa8

Please sign in to comment.