1、暂存
This commit is contained in:
15
services/commonservice/commonservice.go
Normal file
15
services/commonservice/commonservice.go
Normal file
@ -0,0 +1,15 @@
|
||||
package commonservice
|
||||
|
||||
import (
|
||||
"github.com/go-admin-team/go-admin-core/sdk"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// 获取数据库连接
|
||||
func GetDBConnection() *gorm.DB {
|
||||
dbs := sdk.Runtime.GetDb()
|
||||
for _, db := range dbs {
|
||||
return db
|
||||
}
|
||||
return nil
|
||||
}
|
||||
63
services/commonservice/judge.go
Normal file
63
services/commonservice/judge.go
Normal file
@ -0,0 +1,63 @@
|
||||
package commonservice
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"go-admin/common/global"
|
||||
"go-admin/common/helper"
|
||||
"go-admin/models/binancedto"
|
||||
"time"
|
||||
|
||||
"github.com/bytedance/sonic"
|
||||
)
|
||||
|
||||
// 判断api订阅是否超时
|
||||
// wsType 1 现货 2 合约
|
||||
func JudgeWebsocketTimeout(apiKey string, wsType int) error {
|
||||
key := fmt.Sprintf(global.USER_SUBSCRIBE, apiKey)
|
||||
cache := binancedto.UserSubscribeState{}
|
||||
// 从redis中获取订阅信息
|
||||
subscribeInfo, err := helper.DefaultRedis.GetString(key)
|
||||
if err != nil {
|
||||
return errors.New("获取订阅信息失败")
|
||||
}
|
||||
|
||||
sonic.Unmarshal([]byte(subscribeInfo), &cache)
|
||||
|
||||
if cache.FuturesLastTime == nil && cache.SpotLastTime == nil {
|
||||
return errors.New("未订阅任何行情")
|
||||
}
|
||||
|
||||
var timeOutInterval time.Duration
|
||||
|
||||
if wsType == 1 {
|
||||
timeOutInterval = time.Minute * 2
|
||||
} else {
|
||||
timeOutInterval = time.Minute * 4
|
||||
}
|
||||
|
||||
switch wsType {
|
||||
case 1:
|
||||
// 现货
|
||||
if cache.SpotLastTime == nil {
|
||||
return errors.New("未订阅现货行情")
|
||||
}
|
||||
|
||||
if time.Since(*cache.SpotLastTime) > timeOutInterval {
|
||||
return errors.New("现货行情订阅超时")
|
||||
}
|
||||
case 2:
|
||||
// 合约
|
||||
if cache.FuturesLastTime == nil {
|
||||
return errors.New("未订阅合约行情")
|
||||
}
|
||||
|
||||
if time.Since(*cache.FuturesLastTime) > timeOutInterval {
|
||||
return errors.New("合约行情订阅超时")
|
||||
}
|
||||
default:
|
||||
return errors.New("无效的wsType")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user