代码拉取完成,页面将自动刷新
package flow
import (
"context"
"fmt"
"gitee.com/os-lee/easy-paas/common"
"github.com/go-redis/redis/v8"
"strconv"
"sync"
"time"
)
// QpsCounterHandler QPS计算器
var (
QpsCounterHandler *QpsCounter
)
func init() {
QpsCounterHandler = newQpsCounter()
}
type QpsCounter struct {
QpsCounterMap sync.Map
}
func newQpsCounter() *QpsCounter {
return &QpsCounter{
QpsCounterMap: sync.Map{},
}
}
type QpsCounterItem struct {
RedisCli *redis.Client
serviceName string
Requests int64 // 请求计数
StartTime time.Time // 开始时间
}
func newQpsCounterItem(serviceName string, redisCli *redis.Client) *QpsCounterItem {
return &QpsCounterItem{
RedisCli: redisCli,
serviceName: serviceName,
Requests: 0,
StartTime: time.Now(),
}
}
func (qc *QpsCounter) GetQpsCounterItem(serviceName string, redisCli *redis.Client) *QpsCounterItem {
if value, ok := qc.QpsCounterMap.Load(serviceName); ok {
return value.(*QpsCounterItem)
}
item := newQpsCounterItem(serviceName, redisCli)
qc.QpsCounterMap.Store(serviceName, item)
return item
}
func (qci *QpsCounterItem) WriteQPS(ctx context.Context, qps float64) error {
// 写入网关当前QPS
gwKey := fmt.Sprintf("%s_%s", common.ReqCounterByService, common.EasyGatewayReq)
err := qci.RedisCli.Set(ctx, gwKey,
strconv.FormatFloat(qps, 'f', 2, 64), 5*time.Second).Err()
// 写入各业务QPS
svcKey := fmt.Sprintf("%s_%s", common.ReqCounterByService, qci.serviceName)
err = qci.RedisCli.Set(ctx, svcKey,
strconv.FormatFloat(qps, 'f', 2, 64), 5*time.Second).Err()
return err
}
func (qci *QpsCounterItem) GetQps(ctx context.Context) (string, error) {
key := fmt.Sprintf("%s_%s", common.ReqCounterByService, qci.serviceName)
return qci.RedisCli.Get(ctx, key).Result()
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。