Skip to content

Commit

Permalink
+ FreePort Helper & Auto Session port
Browse files Browse the repository at this point in the history
  • Loading branch information
trheyi committed Feb 9, 2022
1 parent 54a53ca commit 1056def
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ jobs:
with:
src: ./dist/release/*
host: ${{ secrets.SSH_HOST }}
remote: /data/down/
remote: /data/release/ui/
port: ${{ secrets.SSH_PORT }}
user: ${{ secrets.SSH_USER }}
key: ${{ secrets.SSH_KEY }}
Expand Down
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ var langs = map[string]string{
"NEXT:": "下一步:",
"Listening": " 监听",
"✨LISTENING✨": "✨服务正在运行✨",
"SessionPort": "会话服务端口",
}

// L 多语言切换
Expand Down
1 change: 1 addition & 0 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ var startCmd = &cobra.Command{
fmt.Println(color.WhiteString(L("Frontend")), color.GreenString(" http://%s%s/", host, port))
fmt.Println(color.WhiteString(L("Dashboard")), color.GreenString(" http://%s%s/xiang/login/admin", host, port))
fmt.Println(color.WhiteString(L("API")), color.GreenString(" http://%s%s/api", host, port))
fmt.Println(color.WhiteString(L("SessionPort")), color.GreenString(" %d", share.SessionPort))
fmt.Println(color.WhiteString(L("Listening")), color.GreenString(" %s:%d", config.Conf.Host, config.Conf.Port))

fmt.Println("")
Expand Down
22 changes: 22 additions & 0 deletions network/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,25 @@ func IP() map[string]string {
func ProcessIP(process *gou.Process) interface{} {
return IP()
}

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

l, err := net.ListenTCP("tcp", addr)
if err != nil {
exception.New("获取可用端口失败 %s", 500, err.Error()).Throw()
return 0
}
defer l.Close()
return l.Addr().(*net.TCPAddr).Port
}

// ProcessFreePort xiang.network.FreePort 获取可用端口
func ProcessFreePort(process *gou.Process) interface{} {
return FreePort()
}
12 changes: 12 additions & 0 deletions network/ip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,15 @@ func TestProcessIP(t *testing.T) {
assert.True(t, ok)
assert.True(t, len(resp) > 0)
}

func TestFreePort(t *testing.T) {
port := FreePort()
assert.True(t, port > 0)
}

func TestProcessFreePort(t *testing.T) {
res := gou.NewProcess("xiang.network.FreePort").Run()
port, ok := res.(int)
assert.True(t, ok)
assert.True(t, port > 0)
}
1 change: 1 addition & 0 deletions network/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import "github.com/yaoapp/gou"
func init() {
// 注册处理器
gou.RegisterProcessHandler("xiang.network.ip", ProcessIP)
gou.RegisterProcessHandler("xiang.network.FreePort", ProcessFreePort)
gou.RegisterProcessHandler("xiang.network.Get", ProcessGet)
gou.RegisterProcessHandler("xiang.network.Post", ProcessPost)
gou.RegisterProcessHandler("xiang.network.PostJSON", ProcessPostJSON)
Expand Down
3 changes: 3 additions & 0 deletions share/const.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package share

// Logs
// (session -> freeport)

// VERSION 版本号
const VERSION = "0.9.0"

Expand Down
16 changes: 13 additions & 3 deletions share/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,24 @@ import (
"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

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

// SessionConnect 加载会话信息
func SessionConnect(conf config.SessionConfig) {

var clientConfig = &client.Config{
Servers: []string{fmt.Sprintf("%s:%d", conf.Host, conf.Port)},
Servers: []string{fmt.Sprintf("%s:%d", "127.0.0.1", SessionPort)},
Serializer: serializer.NewMsgpackSerializer(),
Client: config_olric.NewClient(),
}
Expand All @@ -46,9 +55,10 @@ func SessionServerStop() {

// SessionServerStart 启动会话服务器
func SessionServerStart() {

c := &config_olric.Config{
BindAddr: config.Conf.Session.Host,
BindPort: config.Conf.Session.Port,
BindAddr: "127.0.0.1",
BindPort: SessionPort,
ReadRepair: false,
ReplicaCount: 1,
WriteQuorum: 1,
Expand Down

0 comments on commit 1056def

Please sign in to comment.