代码拉取完成,页面将自动刷新
package middleware_http
import (
"errors"
"gitee.com/os-lee/easy-paas/common"
"gitee.com/os-lee/easy-paas/common/e"
"gitee.com/os-lee/easy-paas/common/flow"
"gitee.com/os-lee/easy-paas/common/logger"
"gitee.com/os-lee/easy-paas/gateway/internal/cache"
"gitee.com/os-lee/easy-paas/gateway/internal/dao"
"github.com/gin-gonic/gin"
"time"
)
// FlowQpsCount 统计QPS
func FlowQpsCount() gin.HandlerFunc {
return func(c *gin.Context) {
serverInterface, ok := c.Get("service")
if !ok {
common.ResponseError(c, e.ErrGateway, errors.New("从上下文中获取服务失败"))
c.Abort()
return
}
// 统计网关QPS
qwCounter := flow.QpsCounterHandler.GetQpsCounterItem(common.EasyGatewayQps, cache.RedisCli)
qwCounter.Requests++ // 增加请求计数
// 按服务统计QPS
serviceDetail := serverInterface.(*dao.ServiceDetail)
serviceName := serviceDetail.Info.ServiceName
svcCounter := flow.QpsCounterHandler.GetQpsCounterItem(serviceName, cache.RedisCli)
svcCounter.Requests++ // 增加请求计数
c.Next() // 继续处理请求
// 在请求结束后计算网关QPS
if time.Since(qwCounter.StartTime) >= time.Second {
qps := float64(qwCounter.Requests) / time.Since(qwCounter.StartTime).Seconds()
// 写入redis
err := qwCounter.WriteQPS(c, qps)
if err != nil {
// 写入QPS错误,不影响业务,记录日志即可
logger.Log.Errorf("将QPS写入redis错误, Err: %v", err)
}
qwCounter.Requests = 0 // 重置计数器
qwCounter.StartTime = time.Now() // 更新开始时间
}
// 在请求结束后计算各服务QPS
if time.Since(svcCounter.StartTime) >= time.Second {
qps := float64(svcCounter.Requests) / time.Since(svcCounter.StartTime).Seconds()
// 写入redis
err := svcCounter.WriteQPS(c, qps)
if err != nil {
// 写入QPS错误,不影响业务,记录日志即可
logger.Log.Errorf("将QPS写入redis错误, Err: %v", err)
}
svcCounter.Requests = 0 // 重置计数器
svcCounter.StartTime = time.Now() // 更新开始时间
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。