mirror of
https://github.com/TeaOSLab/EdgeCommon.git
synced 2025-11-03 12:20:27 +08:00
删除TOA相关不需要的内容
This commit is contained in:
@@ -1,96 +1,18 @@
|
||||
// Copyright 2023 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||
//go:build !plus
|
||||
|
||||
package nodeconfigs
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/iwind/TeaGo/rands"
|
||||
"net"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
// DefaultTOAConfig 默认的TOA配置
|
||||
func DefaultTOAConfig() *TOAConfig {
|
||||
return &TOAConfig{
|
||||
IsOn: false,
|
||||
Debug: false,
|
||||
OptionType: 0xfe,
|
||||
MinQueueId: 100,
|
||||
AutoSetup: true,
|
||||
}
|
||||
// NewTOAConfig 默认的TOA配置
|
||||
func NewTOAConfig() *TOAConfig {
|
||||
return &TOAConfig{}
|
||||
}
|
||||
|
||||
// TOAConfig TOA相关配置
|
||||
type TOAConfig struct {
|
||||
IsOn bool `yaml:"isOn" json:"isOn"` // 是否启用
|
||||
Debug bool `yaml:"debug" json:"debug"`
|
||||
OptionType uint8 `yaml:"optionType" json:"optionType"`
|
||||
MinQueueId uint8 `yaml:"minQueueId" json:"minQueueId"`
|
||||
AutoSetup bool `yaml:"autoSetup" json:"autoSetup"`
|
||||
MinLocalPort uint16 `yaml:"minLocalPort" json:"minLocalPort"` // 本地可使用的最小端口 TODO
|
||||
MaxLocalPort uint16 `yaml:"maxLocalPort" json:"maxLocalPort"` // 本地可使用的最大端口 TODO
|
||||
SockPath string `yaml:"sockPath" json:"sockPath"` // Sock文件路径 TODO
|
||||
ByPassPorts []uint16 `yaml:"byPassPorts" json:"byPassPorts"` // 忽略的端口 TODO
|
||||
|
||||
minLocalPort int
|
||||
maxLocalPort int
|
||||
IsOn bool `yaml:"isOn" json:"isOn"`
|
||||
}
|
||||
|
||||
func (this *TOAConfig) Init() error {
|
||||
// LocalPort
|
||||
minPort := this.MinLocalPort
|
||||
maxPort := this.MaxLocalPort
|
||||
if minPort == 0 {
|
||||
minPort = 1025
|
||||
}
|
||||
if maxPort == 0 {
|
||||
maxPort = 65534
|
||||
}
|
||||
if minPort > maxPort {
|
||||
minPort, maxPort = maxPort, minPort
|
||||
}
|
||||
this.minLocalPort = int(minPort)
|
||||
this.maxLocalPort = int(maxPort)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// SockFile Sock路径
|
||||
func (this *TOAConfig) SockFile() string {
|
||||
if len(this.SockPath) == 0 {
|
||||
return "/tmp/edge-toa.sock"
|
||||
}
|
||||
return this.SockPath
|
||||
}
|
||||
|
||||
// RandLocalPort 获取随机端口
|
||||
func (this *TOAConfig) RandLocalPort() uint16 {
|
||||
listener, err := net.Listen("tcp", ":0")
|
||||
if err != nil {
|
||||
return uint16(rands.Int(this.minLocalPort, this.maxLocalPort))
|
||||
}
|
||||
_ = listener.Close()
|
||||
return uint16(listener.Addr().(*net.TCPAddr).Port)
|
||||
}
|
||||
|
||||
// AsArgs 转换为参数的形式
|
||||
func (this *TOAConfig) AsArgs() (args []string) {
|
||||
args = append(args, "run")
|
||||
args = append(args, "-option-type="+fmt.Sprintf("%d", this.OptionType))
|
||||
args = append(args, "-min-queue-id="+fmt.Sprintf("%d", this.MinQueueId))
|
||||
args = append(args, "-max-queue-id="+fmt.Sprintf("%d", this.MaxQueueId()))
|
||||
if this.AutoSetup {
|
||||
args = append(args, "-auto-setup")
|
||||
}
|
||||
if this.Debug {
|
||||
args = append(args, "-debug")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// MaxQueueId 获取队列ID最大值
|
||||
func (this *TOAConfig) MaxQueueId() uint8 {
|
||||
var maxQueueId = int(this.MinQueueId) + runtime.NumCPU() - 1
|
||||
if maxQueueId > 255 {
|
||||
maxQueueId = 255
|
||||
}
|
||||
return uint8(maxQueueId)
|
||||
}
|
||||
|
||||
@@ -1,77 +0,0 @@
|
||||
package nodeconfigs
|
||||
|
||||
import (
|
||||
"net"
|
||||
"runtime"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestTOAConfig_RandLocalPort(t *testing.T) {
|
||||
{
|
||||
toa := &TOAConfig{}
|
||||
err := toa.Init()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log(toa.RandLocalPort())
|
||||
}
|
||||
{
|
||||
toa := &TOAConfig{
|
||||
MinLocalPort: 1,
|
||||
MaxLocalPort: 2,
|
||||
}
|
||||
err := toa.Init()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log(toa.RandLocalPort())
|
||||
}
|
||||
}
|
||||
|
||||
func TestTOAConfig_FreePort(t *testing.T) {
|
||||
before := time.Now()
|
||||
listener, err := net.Listen("tcp", ":0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log(listener.Addr())
|
||||
_ = listener.Close()
|
||||
t.Log(time.Since(before).Seconds()*1000, "ms")
|
||||
time.Sleep(30 * time.Second)
|
||||
}
|
||||
|
||||
|
||||
func TestTOAConfig_AsArgs(t *testing.T) {
|
||||
toa := &TOAConfig{
|
||||
IsOn: false,
|
||||
Debug: true,
|
||||
OptionType: 0xfe,
|
||||
MinQueueId: 10,
|
||||
MaxQueueId: 20,
|
||||
AutoSetup: true,
|
||||
MinLocalPort: 0,
|
||||
MaxLocalPort: 0,
|
||||
SockPath: "",
|
||||
ByPassPorts: nil,
|
||||
}
|
||||
err := toa.Init()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log(toa.AsArgs())
|
||||
}
|
||||
|
||||
func BenchmarkTOAConfig_RandLocalPort(b *testing.B) {
|
||||
runtime.GOMAXPROCS(1)
|
||||
|
||||
toa := &TOAConfig{
|
||||
MinLocalPort: 1,
|
||||
MaxLocalPort: 2,
|
||||
}
|
||||
_ = toa.Init()
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
_ = toa.RandLocalPort()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user