64 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			64 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| 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
 | |
| }
 |