diff --git a/README.md b/README.md index 1103dd2..88bfadb 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,13 @@ ## TODO +考虑一个方案限制接口请求速率,无侵入更好 + trade/create 性能问题,缩短事务时间,考虑其他方式实现可靠分布式事务 jsonb 类型转换问题,考虑一个高效的 any 到 struct 转换工具 端口资源池的 gc 实现 -考虑一个方案限制接口请求速率,无侵入更好 标准化生产环境 cors 配置 diff --git a/scripts/sql/init.sql b/scripts/sql/init.sql index 3058d89..504ba1e 100644 --- a/scripts/sql/init.sql +++ b/scripts/sql/init.sql @@ -503,7 +503,7 @@ create table proxy ( secret text, type int not null, status int not null, - meta jsonb not null, + meta jsonb, created_at timestamptz default current_timestamp, updated_at timestamptz default current_timestamp, deleted_at timestamptz diff --git a/web/handlers/proxy.go b/web/handlers/proxy.go index fda1969..95dda48 100644 --- a/web/handlers/proxy.go +++ b/web/handlers/proxy.go @@ -1,10 +1,9 @@ package handlers import ( - "log/slog" "net/netip" "platform/pkg/env" - g "platform/web/globals" + "platform/web/core" s "platform/web/services" "time" @@ -16,18 +15,9 @@ func DebugRegisterProxyBaiYin(c *fiber.Ctx) error { return fiber.ErrNotFound } - ok, err := g.Redis.SetNX(c.Context(), "debug:channel:register:127.0.0.1", true, 0).Result() + err := s.Proxy.RegisterBaiyin("1a:2b:3c:4d:5e:6f", netip.AddrFrom4([4]byte{127, 0, 0, 1}), "test", "test") if err != nil { - return err - } - slog.Info("注册代理", "ok", ok) - if !ok { - return fiber.ErrConflict - } - - err = s.Proxy.RegisterBaiyin("1a:2b:3c:4d:5e:6f", netip.AddrFrom4([4]byte{127, 0, 0, 1}), "test", "test") - if err != nil { - return err + return core.NewServErr("注册失败", err) } return nil diff --git a/web/routes.go b/web/routes.go index 3813c99..e7ff573 100644 --- a/web/routes.go +++ b/web/routes.go @@ -73,11 +73,11 @@ func ApplyRouters(app *fiber.App) { edge.Post("/assign", handlers.AssignEdge) edge.Post("/all", handlers.AllEdgesAvailable) + callbacks := app.Group("/callback") + callbacks.Get("/identify", handlers.IdentifyCallbackNew) + // 临时 debug := app.Group("/debug") debug.Get("/sms/:phone", handlers.DebugGetSmsCode) debug.Get("/proxy/register", handlers.DebugRegisterProxyBaiYin) - - callbacks := app.Group("/callback") - callbacks.Get("/identify", handlers.IdentifyCallbackNew) } diff --git a/web/services/channel.go b/web/services/channel.go index 0c615ce..c3155ff 100644 --- a/web/services/channel.go +++ b/web/services/channel.go @@ -259,10 +259,14 @@ func addChans(chans []netip.AddrPort) error { var RedisScriptAddChans = ` local free_key = KEYS[1] local all_key = KEYS[2] -local chans = ARGV +local chans = ARGV -redis.call("SADD", free_key, unpack(chans)) -redis.call("SADD", all_key, unpack(chans)) +local batch_size = 5000 +for i = 1, #chans, batch_size do + local end_index = math.min(i + batch_size - 1, #chans) + redis.call("SADD", free_key, unpack(chans, i, end_index)) + redis.call("SADD", all_key, unpack(chans, i, end_index)) +end return 1 ` diff --git a/web/services/proxy.go b/web/services/proxy.go index a8db972..e77cbc4 100644 --- a/web/services/proxy.go +++ b/web/services/proxy.go @@ -40,7 +40,7 @@ func (s *proxyService) RegisterBaiyin(Mac string, IP netip.Addr, username, passw } err := addChans(chans) if err != nil { - return core.NewServErr("添加通道失败") + return core.NewServErr("添加通道失败", err) } // 保存代理信息