Browse Source

signIn 进行限频

master
DengBiao 1 year ago
parent
commit
6480186b00
1 changed files with 23 additions and 0 deletions
  1. +23
    -0
      internal/logic/domain/device/device_service.go

+ 23
- 0
internal/logic/domain/device/device_service.go View File

@@ -2,6 +2,12 @@ package device


import ( import (
"context" "context"
"errors"
"fmt"
"gim/internal/business/comm/utils"
"gim/pkg/db"
"gim/pkg/gerrors"
"gim/pkg/grpclib"
"gim/pkg/logger" "gim/pkg/logger"
"gim/pkg/pb" "gim/pkg/pb"
"gim/pkg/rpc" "gim/pkg/rpc"
@@ -14,6 +20,11 @@ type deviceService struct{}


var DeviceService = new(deviceService) var DeviceService = new(deviceService)


const (
SignInKey = "%s:gim_sign_in:%s" // 占位符: masterId, key的第一个字母
SignInExpire = 30 * time.Second
)

// Register 注册设备 // Register 注册设备
func (*deviceService) Register(ctx context.Context, device *Device) error { func (*deviceService) Register(ctx context.Context, device *Device) error {
err := DeviceDao.Save(device) err := DeviceDao.Save(device)
@@ -26,6 +37,18 @@ func (*deviceService) Register(ctx context.Context, device *Device) error {


// SignIn 长连接登录 // SignIn 长连接登录
func (*deviceService) SignIn(ctx context.Context, userId, deviceId int64, token string, connAddr string, clientAddr string) error { func (*deviceService) SignIn(ctx context.Context, userId, deviceId int64, token string, connAddr string, clientAddr string) error {
//TODO::限制操作
masterId, _ := grpclib.GetCtxMasterId(ctx)
cacheKey := fmt.Sprintf(SignInKey, masterId, utils.Int64ToStr(userId))
if db.RedisUtil.Exists(cacheKey) {
return errors.New("限频!!!")
} else {
err := db.RedisUtil.Set(SignInKey, "already", SignInExpire)
if err != nil {
return gerrors.WrapError(err)
}
}

_, err := rpc.GetBusinessIntClient().Auth(ctx, &pb.AuthReq{UserId: userId, DeviceId: deviceId, Token: token}) _, err := rpc.GetBusinessIntClient().Auth(ctx, &pb.AuthReq{UserId: userId, DeviceId: deviceId, Token: token})
if err != nil { if err != nil {
return err return err


Loading…
Cancel
Save