Skip to content

Commit

Permalink
Fix FreePort DNS deps & Session bug
Browse files Browse the repository at this point in the history
  • Loading branch information
trheyi committed Apr 10, 2022
1 parent d8d4bd7 commit c775df1
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 33 deletions.
4 changes: 2 additions & 2 deletions engine/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ func Load(cfg config.Config) (err error) {
gou.LoadCrypt(`{}`, "PASSWORD")

// 第二步: 建立数据库 & 会话连接
share.DBConnect(cfg.DB) // 创建数据库连接
share.SessionConnect(cfg.Session) // 创建会话服务器链接
share.DBConnect(cfg.DB) // 创建数据库连接
// share.SessionConnect(cfg.Session) // 创建会话服务器链接

// 加载应用引擎
if os.Getenv("YAO_DEV") != "" {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/armon/go-metrics v0.3.10 // indirect
github.com/baiyubin/aliyun-sts-go-sdk v0.0.0-20180326062324-cfa1a18b161f // indirect
github.com/buraksezer/consistent v0.9.0 // indirect
github.com/buraksezer/olric v0.4.2
github.com/buraksezer/olric v0.4.3
github.com/caarlos0/env/v6 v6.9.1
github.com/elazarl/go-bindata-assetfs v1.0.1
github.com/fatih/color v1.13.0
Expand Down
8 changes: 3 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ github.com/buraksezer/connpool v0.4.0/go.mod h1:qPiG7gKXo+EjrwG/yqn2StZM4ek6gcYn
github.com/buraksezer/consistent v0.0.0-20191006190839-693edf70fd72/go.mod h1:OEE5igu/CDjGegM1Jn6ZMo7R6LlV/JChAkjfQQIRLpg=
github.com/buraksezer/consistent v0.9.0 h1:Zfs6bX62wbP3QlbPGKUhqDw7SmNkOzY5bHZIYXYpR5g=
github.com/buraksezer/consistent v0.9.0/go.mod h1:6BrVajWq7wbKZlTOUPs/XVfR8c0maujuPowduSpZqmw=
github.com/buraksezer/olric v0.4.2 h1:1W5UCYFYrkeD6OHGyTi6R9YTLJ3vXcVkgas4ZbFQMfw=
github.com/buraksezer/olric v0.4.2/go.mod h1:xNt+/QiiVuqioJzgTMuiV2LynTNu87L1Tp5289XuU78=
github.com/buraksezer/olric v0.4.3 h1:A8QKRv/2BiIOWmoCzbsRniW/zbuN80vAJXMfEo4N7qc=
github.com/buraksezer/olric v0.4.3/go.mod h1:xNt+/QiiVuqioJzgTMuiV2LynTNu87L1Tp5289XuU78=
github.com/caarlos0/env/v6 v6.9.1 h1:zOkkjM0F6ltnQ5eBX6IPI41UP/KDGEK7rRPwGCNos8k=
github.com/caarlos0/env/v6 v6.9.1/go.mod h1:hvp/ryKXKipEkcuYjs9mI4bBCg+UI0Yhgm5Zu0ddvwc=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
Expand Down Expand Up @@ -147,8 +147,6 @@ github.com/go-playground/universal-translator v0.18.0/go.mod h1:UvRDBj+xPUEGrFYl
github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4=
github.com/go-playground/validator/v10 v10.10.0 h1:I7mrTYv78z8k8VXa/qJlOlEXn/nBh+BF8dHX5nt/dr0=
github.com/go-playground/validator/v10 v10.10.0/go.mod h1:74x4gJWsvQexRdW8Pn3dXSGrTK4nAUsbPlLADvpJkos=
github.com/go-redis/redis v6.15.9+incompatible h1:K0pv1D7EQUjfyoMql+r/jZqCLizCGKFlFgcHWWmHQjg=
github.com/go-redis/redis v6.15.9+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8wamY7mA7PouImQ2Jvg6kA=
github.com/go-redis/redis/v8 v8.11.4/go.mod h1:2Z2wHZXdQpCDXEGzqMockDpNyYvi2l4Pxt6RJr792+w=
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
Expand Down Expand Up @@ -207,8 +205,8 @@ github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
Expand Down
3 changes: 2 additions & 1 deletion network/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func ProcessIP(process *gou.Process) interface{} {

// FreePort xiang.network.FreePort 获取可用端口
func FreePort() int {
addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
addr, err := net.ResolveTCPAddr("tcp", ":0")
if err != nil {
exception.New("获取可用端口失败 %s", 500, err.Error()).Throw()
return 0
Expand All @@ -52,6 +52,7 @@ func FreePort() int {
exception.New("获取可用端口失败 %s", 500, err.Error()).Throw()
return 0
}

defer l.Close()
return l.Addr().(*net.TCPAddr).Port
}
Expand Down
54 changes: 30 additions & 24 deletions share/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,47 @@ import (
"log"

klog "github.com/yaoapp/kun/log"
"github.com/yaoapp/yao/network"

"github.com/buraksezer/olric"
"github.com/buraksezer/olric/client"
config_olric "github.com/buraksezer/olric/config"
"github.com/buraksezer/olric/serializer"
"github.com/yaoapp/gou/session"
"github.com/yaoapp/kun/exception"
"github.com/yaoapp/yao/config"
"github.com/yaoapp/yao/network"
)

var sessServer *olric.Olric

// SessionPort Session 端口
var SessionPort int

// SessionMemberPort Session Member Port
var SessionMemberPort int

func init() {
SessionPort = network.FreePort()
klog.Trace("session port: %d", SessionPort)
SessionMemberPort = network.FreePort()
klog.Trace("Session port: %d, Memeber Port:", SessionPort, SessionMemberPort)
}

// SessionConnect 加载会话信息
func SessionConnect(conf config.SessionConfig) {
// SessionConnect 加载会话信息 (废弃->共享方案用 Redis 替代)
// func SessionConnect() {

var clientConfig = &client.Config{
Servers: []string{fmt.Sprintf("%s:%d", "127.0.0.1", SessionPort)},
Serializer: serializer.NewMsgpackSerializer(),
Client: config_olric.NewClient(),
}
// stats, err := sessServer.Stats()
// fmt.Println(stats, err)

c, err := client.New(clientConfig)
if err != nil {
exception.New("会话服务器连接失败 %s", 500, err.Error()).Throw()
}
// var clientConfig = &client.Config{
// Servers: []string{fmt.Sprintf("%s:%d", "127.0.0.1", SessionPort)},
// Serializer: serializer.NewMsgpackSerializer(),
// Client: config_olric.NewClient(),
// }

dm := c.NewDMap("local-session")
session.MemoryUse(session.ClientDMap{DMap: dm})
}
// c, err := client.New(clientConfig)
// if err != nil {
// exception.New("会话服务器连接失败 %s", 500, err.Error()).Throw()
// }

// dm := c.NewDMap("local-session")
// session.MemoryUse(session.ClientDMap{DMap: dm})
// }

// SessionServerStop 关闭会话服务器
func SessionServerStop() {
Expand All @@ -56,6 +59,7 @@ func SessionServerStop() {
// SessionServerStart 启动会话服务器
func SessionServerStart() {

// SessionPort := 0
c := &config_olric.Config{
BindAddr: "127.0.0.1",
BindPort: SessionPort,
Expand All @@ -74,9 +78,10 @@ func SessionServerStart() {
if err != nil {
panic(fmt.Sprintf("unable to create a new memberlist config: %v", err))
}
// m.BindAddr = config.Conf.Session.Host
m.BindPort = SessionPort
m.AdvertisePort = SessionPort
m.BindAddr = "127.0.0.1"
m.BindPort = SessionMemberPort
// m.AdvertiseAddr = "127.0.0.1"
// m.AdvertisePort = SessionPort
c.MemberlistConfig = m

// c.MemberlistConfig.BindAddr = config.Conf.Session.Host
Expand All @@ -94,14 +99,15 @@ func SessionServerStart() {
}

sessServer, err = olric.New(c)

if err != nil {
klog.Error("Failed to create Olric instance: %v", err)
}

go func() {
// fmt.Println("Session Port IS:", SessionPort) // DEBUG
err = sessServer.Start() // Call Start at background. It's a blocker call.
if err != nil {
fmt.Println(err)
klog.Panic("olric.Start returned an error: %v", err)
}
}()
Expand Down

0 comments on commit c775df1

Please sign in to comment.