通道的增删接口实现,数据表和目录结构调整
This commit is contained in:
@@ -4,10 +4,10 @@ import (
|
||||
"log/slog"
|
||||
"os"
|
||||
"os/signal"
|
||||
"platform/init/env"
|
||||
"platform/init/logs"
|
||||
"platform/init/orm"
|
||||
"platform/init/rds"
|
||||
"platform/pkg/env"
|
||||
"platform/pkg/logs"
|
||||
"platform/pkg/orm"
|
||||
"platform/pkg/rds"
|
||||
"platform/web"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
7
cmd/playground/main.go
Normal file
7
cmd/playground/main.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package main
|
||||
|
||||
import "encoding/base64"
|
||||
|
||||
func main() {
|
||||
println(base64.URLEncoding.EncodeToString([]byte("app:123456")))
|
||||
}
|
||||
68
cmd/tasks/main.go
Normal file
68
cmd/tasks/main.go
Normal file
@@ -0,0 +1,68 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"platform/pkg/env"
|
||||
"platform/pkg/logs"
|
||||
"platform/pkg/orm"
|
||||
"platform/pkg/rds"
|
||||
"time"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
func main() {
|
||||
Start()
|
||||
}
|
||||
|
||||
func Start() {
|
||||
ctx := context.Background()
|
||||
|
||||
env.Init()
|
||||
logs.Init()
|
||||
rds.Init()
|
||||
orm.Init()
|
||||
|
||||
ticker := time.NewTicker(time.Second)
|
||||
defer ticker.Stop()
|
||||
|
||||
for curr := range ticker.C {
|
||||
err := process(ctx, curr)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func process(ctx context.Context, curr time.Time) error {
|
||||
|
||||
// 获取并删除
|
||||
script := redis.NewScript(`
|
||||
local result = redis.call('ZRANGEBYSCORE', KEYS[1], 0, ARGV[1])
|
||||
if #result > 0 then
|
||||
redis.call('ZREMRANGEBYSCORE', KEYS[1], 0, ARGV[1])
|
||||
end
|
||||
return result
|
||||
`)
|
||||
|
||||
// 计算时间范围
|
||||
// 执行脚本
|
||||
result, err := script.Run(ctx, rds.Client, []string{"tasks:session"}, curr.Unix()).Result()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 处理结果
|
||||
list, ok := result.([]string)
|
||||
if !ok {
|
||||
return errors.New("failed to convert result to []string")
|
||||
}
|
||||
for _, item := range list {
|
||||
// 从数据库删除授权信息
|
||||
slog.Debug(item)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
85
cmd/wrapper/main.go
Normal file
85
cmd/wrapper/main.go
Normal file
@@ -0,0 +1,85 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"os"
|
||||
"os/signal"
|
||||
"platform/pkg/env"
|
||||
"platform/pkg/logs"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
// 初始化环境
|
||||
env.Init()
|
||||
logs.Init()
|
||||
|
||||
// 上下文
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
|
||||
// 监听退出
|
||||
exit := make(chan os.Signal, 1)
|
||||
defer close(exit)
|
||||
signal.Notify(exit, os.Interrupt, os.Kill)
|
||||
defer signal.Stop(exit)
|
||||
|
||||
// 启动管理子线程
|
||||
errCh := make(chan error, 1)
|
||||
go func() {
|
||||
defer close(errCh)
|
||||
err := start(ctx)
|
||||
if err != nil {
|
||||
errCh <- err
|
||||
}
|
||||
}()
|
||||
|
||||
select {
|
||||
case <-exit:
|
||||
slog.Debug("exit by signal")
|
||||
cancel()
|
||||
case err := <-errCh:
|
||||
slog.Error("exit by error", "error", err)
|
||||
}
|
||||
}
|
||||
|
||||
// 连接池,硬编码提供 10000 的容量
|
||||
var idle = 100
|
||||
var maximum = 10000
|
||||
|
||||
var pool = make(map[string]*Node, 10000)
|
||||
|
||||
var tick = 1 * time.Minute
|
||||
|
||||
type Node struct {
|
||||
Ip string
|
||||
}
|
||||
|
||||
var last time.Time
|
||||
|
||||
func start(ctx context.Context) error {
|
||||
ticker := time.NewTicker(tick)
|
||||
go func() {
|
||||
<-ctx.Done()
|
||||
ticker.Stop()
|
||||
}()
|
||||
|
||||
for curr := range ticker.C {
|
||||
last = curr
|
||||
go func() {
|
||||
process(ctx, curr)
|
||||
}()
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func process(ctx context.Context, curr time.Time) {
|
||||
|
||||
// 查询节点状态
|
||||
|
||||
// 筛选在线节点添加到节点池
|
||||
|
||||
//
|
||||
}
|
||||
Reference in New Issue
Block a user