feat: refact scheduler

This commit is contained in:
engigu
2026-02-07 21:44:09 +08:00
parent cdf3b3dbdc
commit f746c871fa
37 changed files with 2888 additions and 1316 deletions
+21 -20
View File
@@ -4,6 +4,7 @@ import (
"bytes"
"compress/zlib"
"encoding/base64"
"io"
)
// CompressToBase64 compresses data using zlib and encodes to base64
@@ -22,23 +23,23 @@ func CompressToBase64(data string) (string, error) {
return base64.StdEncoding.EncodeToString(buf.Bytes()), nil
}
// // DecompressFromBase64 decodes base64 and decompresses zlib data
// func DecompressFromBase64(data string) (string, error) {
// if data == "" {
// return "", nil
// }
// decoded, err := base64.StdEncoding.DecodeString(data)
// if err != nil {
// return "", err
// }
// zr, err := zlib.NewReader(bytes.NewReader(decoded))
// if err != nil {
// return "", err
// }
// defer zr.Close()
// result, err := io.ReadAll(zr)
// if err != nil {
// return "", err
// }
// return string(result), nil
// }
// DecompressFromBase64 decodes base64 and decompresses zlib data
func DecompressFromBase64(data string) (string, error) {
if data == "" {
return "", nil
}
decoded, err := base64.StdEncoding.DecodeString(data)
if err != nil {
return "", err
}
zr, err := zlib.NewReader(bytes.NewReader(decoded))
if err != nil {
return "", err
}
defer zr.Close()
result, err := io.ReadAll(zr)
if err != nil {
return "", err
}
return string(result), nil
}
+43
View File
@@ -0,0 +1,43 @@
package utils
import (
"bufio"
"io"
"unicode/utf8"
"golang.org/x/text/encoding/simplifiedchinese"
"golang.org/x/text/transform"
)
// ToUTF8 converts potentially non-UTF8 data (like GBK on Windows) to UTF-8
func ToUTF8(data []byte) string {
if utf8.Valid(data) {
return string(data)
}
// Try GBK (common on Windows)
reader := transform.NewReader(
bufio.NewReader(
&byteReader{data: data},
),
simplifiedchinese.GBK.NewDecoder(),
)
result, err := io.ReadAll(reader)
if err != nil {
return string(data)
}
return string(result)
}
type byteReader struct {
data []byte
pos int
}
func (r *byteReader) Read(p []byte) (n int, err error) {
if r.pos >= len(r.data) {
return 0, io.EOF
}
n = copy(p, r.data[r.pos:])
r.pos += n
return n, nil
}
+20
View File
@@ -0,0 +1,20 @@
package utils
import (
"runtime"
"strconv"
"strings"
)
// GetGoroutineID 获取当前 Goroutine ID
// 注意:这只是为了调试和日志目的,不应该用于业务逻辑
func GetGoroutineID() int64 {
var buf [64]byte
n := runtime.Stack(buf[:], false)
idField := strings.Fields(strings.TrimPrefix(string(buf[:n]), "goroutine "))[0]
id, err := strconv.ParseInt(idField, 10, 64)
if err != nil {
return -1
}
return id
}
+51
View File
@@ -0,0 +1,51 @@
package utils
import (
"crypto/sha256"
"encoding/hex"
"net"
"os"
"runtime"
"sort"
"strings"
)
// GenerateMachineID 生成机器识别码
func GenerateMachineID() string {
var parts []string
// 主机名
if hostname, err := os.Hostname(); err == nil {
parts = append(parts, hostname)
}
// 获取所有非回环网卡的 MAC 地址,排序后取第一个(最稳定)
if interfaces, err := net.Interfaces(); err == nil {
var macs []string
for _, iface := range interfaces {
// 跳过回环接口、没有 MAC 地址的接口、虚拟接口
if iface.Flags&net.FlagLoopback != 0 || len(iface.HardwareAddr) == 0 {
continue
}
// 跳过 docker/veth 等虚拟网卡
name := strings.ToLower(iface.Name)
if strings.HasPrefix(name, "docker") || strings.HasPrefix(name, "veth") ||
strings.HasPrefix(name, "br-") || strings.HasPrefix(name, "virbr") {
continue
}
macs = append(macs, iface.HardwareAddr.String())
}
sort.Strings(macs)
// 只使用第一个 MAC 地址(最稳定)
if len(macs) > 0 {
parts = append(parts, macs[0])
}
}
// 操作系统和架构
parts = append(parts, runtime.GOOS, runtime.GOARCH)
data := strings.Join(parts, "|")
hash := sha256.Sum256([]byte(data))
return hex.EncodeToString(hash[:])
}