在玩家断开连接时,使用 sync.Once 确保只保存一次玩家数据, 防止因并发或多次触发导致的数据异常。 feat(fight): 增加战斗资格判断与邀请取消功能 - 新增 Player.CanFight() 方法用于统一判断是否可以参与战斗 - 在多个战斗相关接口中加入 CanFight 检查 - 添加“取消战斗邀请”指令及处理逻辑(cmd: 2402) - 修复部分错误码不准确的问题,提升提示一致性 refactor(login): 优化登录流程并增强健壮性 - 提前校验 session 合法性 - 增强获取玩家信息后的空指针检查 - 调整挖矿数据重置方式为 defer 执行 - 优化日志输出内容,便于调试追踪 docs(model): 更新部门、菜单等模型字段命名规范 将 orderNum 字段改为 ordernum,保持数据库列名风格一致, 同时更新了 base_sys_role 中 userId 为 userid。 perf(rate-limit): 提高登录接口的限流 Burst 容量 调整限流器配置,将请求 burst 容量从 2 提升至 5, 以应对短时间高频访问场景,改善用户体验。 chore(build): 忽略新增编译产物和临时文件 在 .gitignore 中添加 logic/logic2、login/login 等新生成文件路径, 避免误提交二进制文件到版本控制。
71 lines
2.0 KiB
Go
71 lines
2.0 KiB
Go
package cmd
|
||
|
||
import (
|
||
"context"
|
||
"runtime"
|
||
|
||
"blazing/cool"
|
||
|
||
i18n "blazing/modules/base/middleware"
|
||
|
||
"github.com/gogf/gf/v2/frame/g"
|
||
"github.com/gogf/gf/v2/net/ghttp"
|
||
"github.com/gogf/gf/v2/os/gcmd"
|
||
"github.com/gogf/gf/v2/os/gfile"
|
||
"github.com/xiaoqidun/limit"
|
||
"golang.org/x/time/rate"
|
||
)
|
||
|
||
var (
|
||
limiter = limit.New()
|
||
Main = gcmd.Command{
|
||
Name: "main",
|
||
Usage: "main",
|
||
Brief: "start http server",
|
||
Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
|
||
// g.Dump(g.DB("test").GetConfig())
|
||
if cool.IsRedisMode {
|
||
go cool.ListenFunc(ctx)
|
||
}
|
||
go robot()
|
||
s := g.Server()
|
||
s.Use(Limiter, ghttp.MiddlewareHandlerResponse)
|
||
s.EnableAdmin()
|
||
s.SetServerAgent(cool.Config.Name)
|
||
s.BindHookHandler("/*", ghttp.HookBeforeServe, beforeServeHook)
|
||
runtime.SetMutexProfileFraction(1) // (非必需)开启对锁调用的跟踪
|
||
runtime.SetBlockProfileRate(1) // (非必需)开启对阻塞操作的跟踪
|
||
s.EnablePProf()
|
||
// 如果存在 data/cool-admin-vue/dist 目录,则设置为主目录
|
||
if gfile.IsDir("public") {
|
||
s.SetServerRoot("public")
|
||
}
|
||
// i18n 信息
|
||
s.BindHandler("/i18n", i18n.I18nInfo)
|
||
// g.Server().BindMiddleware("/*", MiddlewareCORS)
|
||
s.Run()
|
||
return nil
|
||
},
|
||
}
|
||
)
|
||
|
||
func beforeServeHook(r *ghttp.Request) {
|
||
//glog.Debugf(r.GetCtx(), "beforeServeHook [is file:%v] URI:%s", r.IsFileRequest(), r.RequestURI)
|
||
r.Response.CORSDefault()
|
||
}
|
||
|
||
// Limiter is a middleware that implements rate limiting for all HTTP requests.
|
||
// It returns HTTP 429 (Too Many Requests) when the rate limit is exceeded.
|
||
func Limiter(r *ghttp.Request) {
|
||
// 3. 为任意键 "some-key" 获取一个速率限制器
|
||
// - rate.Limit(2): 表示速率为 "每秒2个请求"
|
||
// - 2: 表示桶的容量 (Burst),允许瞬时处理2个请求
|
||
rateLimiter := limiter.Get(r.GetClientIp(), rate.Limit(10), 5)
|
||
|
||
if !rateLimiter.Allow() {
|
||
r.Response.WriteStatusExit(429) // Return 429 Too Many Requests
|
||
r.ExitAll()
|
||
}
|
||
r.Middleware.Next()
|
||
}
|