增加地图广播限制速率

This commit is contained in:
1
2025-11-09 06:50:12 +00:00
parent de3fb29407
commit 742bcd8a3f
6 changed files with 39 additions and 11 deletions

View File

@@ -8,9 +8,11 @@ import (
"github.com/antlabs/cronex"
"github.com/gogf/gf/v2/os/glog"
"github.com/xiaoqidun/limit"
sensitive "github.com/zmexing/go-sensitive-word"
)
var Limiter = limit.New()
var ctx = context.TODO()
var (

View File

@@ -53,7 +53,7 @@ func (h *Controller) MapList(data *maps.ListMapPlayerInboundInfo, c *player.Play
result1 := maps.NewOutInfo()
copier.CopyWithOption(result1, player.GetInfo(), copier.Option{DeepCopy: true})
result.Player = append(result.Player, *result1)
result.Player=LastFourElements(result.Player)
result.Player = LastFourElements(result.Player)
})
c.Canmon = true //可以刷怪
@@ -66,5 +66,5 @@ func LastFourElements[T any](s []T) []T {
return s
}
// 切片长度大于4时返回最后4个元素从n-4索引到末尾
return s[n-4:]
}
return s[n-30:]
}

View File

@@ -8,15 +8,17 @@ import (
func (h Controller) Walk(data *maps.WalkInInfo, c *player.Player) (result *maps.WalkOutInfo, err errorcode.ErrorCode) {
result = &maps.WalkOutInfo{
Flag: data.Flag,
Point: data.Point,
Path: data.Path,
Flag: data.Flag,
Point: data.Point,
Path: data.Path,
}
result.UserID = data.Head.UserID
c.Info.Pos = data.Point
//glog.Debug(context.Background(), err1)
data.Broadcast(c.Info.MapID, *result) //走路的广播
if !c.Info.Pos.BothLessThan50(data.Point) { //距离超过50才广播
data.Broadcast(c.Info.MapID, *result) //走路的广播
}
return nil, -1
}

View File

@@ -1,11 +1,15 @@
package maps
import (
"blazing/cool"
"blazing/logic/service/common"
"blazing/logic/service/player"
"blazing/logic/service/space"
"blazing/modules/blazing/model"
"github.com/gogf/gf/v2/util/gconv"
"golang.org/x/time/rate"
)
type WalkInInfo struct {
@@ -22,6 +26,10 @@ type WalkInInfo struct {
func (t *WalkInInfo) Broadcast(mapid uint32, o WalkOutInfo) {
r := cool.Limiter.Get("Broadcast"+gconv.String(mapid), rate.Limit(10), 5)
if !r.Allow() {
return
}
space.GetSpace(mapid).User.IterCb(func(playerID uint32, player common.PlayerI) {
t.Head.Result = 0
tt := t.Head.Pack(&o)

View File

@@ -12,13 +12,11 @@ import (
"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{
Main = gcmd.Command{
Name: "main",
Usage: "main",
Brief: "start http server",
@@ -60,7 +58,7 @@ func Limiter(r *ghttp.Request) {
// 3. 为任意键 "some-key" 获取一个速率限制器
// - rate.Limit(2): 表示速率为 "每秒2个请求"
// - 2: 表示桶的容量 (Burst)允许瞬时处理2个请求
rateLimiter := limiter.Get(r.GetClientIp(), rate.Limit(10), 5)
rateLimiter := cool.Limiter.Get(r.GetClientIp(), rate.Limit(10), 5)
if !rateLimiter.Allow() {
r.Response.WriteStatusExit(429) // Return 429 Too Many Requests

View File

@@ -2,6 +2,7 @@ package model
import (
"blazing/cool"
"math"
"time"
"github.com/creasty/defaults"
@@ -23,6 +24,23 @@ type Pos struct {
Y uint32 `struc:"uint32" default:"0"`
}
// 计算两个uint32的差值绝对值转为int64避免溢出
func absDiff(a, b uint32) int64 {
return int64(math.Abs(float64(int64(a) - int64(b))))
}
// 判断两个Pos的X和Y差值的绝对值是否均小于50
func bothDiffsLessThan50(pos1, pos2 Pos) bool {
xDiff := absDiff(pos1.X, pos2.X)
yDiff := absDiff(pos1.Y, pos2.Y)
return xDiff < 50 && yDiff < 50
}
func (p Pos) BothLessThan50(t Pos) bool {
return bothDiffsLessThan50(p, t)
}
// PeopleItemInfo 穿戴装备信息结构PeopleItemInfo
type PeopleItemInfo struct {
ID uint32 `struc:"uint32"` // 装备id