From 4626a3d82557f9ebe924ec1a0f9edd9d79176508 Mon Sep 17 00:00:00 2001 From: eric2788 Date: Mon, 25 Apr 2022 12:06:13 +0800 Subject: [PATCH] =?UTF-8?q?listening=20info=20=E6=94=B9=E7=94=A8=E7=B7=A9?= =?UTF-8?q?=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controller/listening/listening.go | 8 +------- services/api/room.go | 7 ++++++- services/api/user.go | 7 ++++++- services/blive/api.go | 23 +---------------------- services/blive/live_server.go | 5 +++-- 5 files changed, 17 insertions(+), 33 deletions(-) diff --git a/controller/listening/listening.go b/controller/listening/listening.go index 36b0e90..f067111 100644 --- a/controller/listening/listening.go +++ b/controller/listening/listening.go @@ -30,17 +30,11 @@ func GetListenRoom(c *gin.Context) { if err != nil { if err == blive.ErrNotFound { - log.Infof("用戶索取 %v 房間資訊時不存在 (%v)", room, c.ClientIP()) + log.Infof("用戶索取 %v 房間資訊時不存在 (%v)", id, c.ClientIP()) c.IndentedJSON(404, gin.H{ "error": "房間不存在", }) return - } else if err == blive.ErrTooFast { - log.Infof("用戶索取 %v 房間資訊時請求頻繁 (%v)", room, c.ClientIP()) - c.IndentedJSON(412, gin.H{ - "error": "請求頻繁,稍後再嘗試", - }) - return } log.Warnf("嘗試獲取房間 %v 的直播資訊時出現錯誤: %v (%v)", id, err, c.ClientIP()) diff --git a/services/api/room.go b/services/api/room.go index 031e323..783b7e7 100644 --- a/services/api/room.go +++ b/services/api/room.go @@ -3,6 +3,7 @@ package api import ( "encoding/json" "fmt" + "github.com/eric2788/biligo-live-ws/services/blive" "github.com/eric2788/biligo-live-ws/services/database" "github.com/sirupsen/logrus" "io" @@ -26,7 +27,11 @@ func GetRoomInfoCache(room int64) (*RoomInfo, error) { if err := database.GetFromDB(dbKey, roomInfo); err == nil { return roomInfo, nil } else { - return nil, err + if _, ok := err.(*database.EmptyError); ok { + return nil, blive.ErrCacheNotFound + } else { + return nil, err + } } } diff --git a/services/api/user.go b/services/api/user.go index 3e2a08e..d3eb109 100644 --- a/services/api/user.go +++ b/services/api/user.go @@ -3,6 +3,7 @@ package api import ( "encoding/json" "fmt" + "github.com/eric2788/biligo-live-ws/services/blive" "github.com/eric2788/biligo-live-ws/services/database" "io" "net/http" @@ -19,7 +20,11 @@ func GetUserInfoCache(uid int64) (*UserInfo, error) { if err := database.GetFromDB(dbKey, userInfo); err == nil { return userInfo, nil } else { - return nil, err + if _, ok := err.(*database.EmptyError); ok { + return nil, blive.ErrCacheNotFound + } else { + return nil, err + } } } diff --git a/services/blive/api.go b/services/blive/api.go index 6dbd5eb..abd2fc8 100644 --- a/services/blive/api.go +++ b/services/blive/api.go @@ -3,16 +3,11 @@ package blive import ( "errors" "github.com/eric2788/biligo-live-ws/services/api" - "time" ) func GetListeningInfo(room int64) (*ListeningInfo, error) { - if coolingDown.Contains(room) { - return nil, ErrTooFast - } - - liveInfo, err := GetLiveInfo(room) + liveInfo, err := GetLiveInfoCache(room) if err != nil { return nil, err } @@ -148,10 +143,6 @@ func GetLiveInfoCache(room int64) (*LiveInfo, error) { // GetLiveInfo 獲取直播資訊,不強制更新緩存 func GetLiveInfo(room int64) (*LiveInfo, error) { - if coolingDown.Contains(room) { - return nil, ErrTooFast - } - // 已在 exception 內, 則返回不存在 if excepted.Contains(room) { return nil, ErrNotFound @@ -167,12 +158,6 @@ func GetLiveInfo(room int64) (*LiveInfo, error) { // 房間資訊請求過快被攔截 if info.Code == -412 { log.Warnf("錯誤: 房間 %v 請求頻繁被攔截", room) - log.Warnf("十分鐘後再允許請求此房間 %v", room) - coolingDown.Add(room) - go func() { - <-time.After(time.Minute * 10) - coolingDown.Remove(room) - }() return nil, ErrTooFast } @@ -200,12 +185,6 @@ func GetLiveInfo(room int64) (*LiveInfo, error) { // 用戶資訊請求過快被攔截 if user.Code == -412 { log.Warnf("錯誤: 用戶 %v 請求頻繁被攔截", data.Uid) - log.Warnf("十分鐘後再允許請求此房間 %v", room) - coolingDown.Add(room) - go func() { - <-time.After(time.Minute * 10) - coolingDown.Remove(room) - }() return nil, ErrTooFast } diff --git a/services/blive/live_server.go b/services/blive/live_server.go index a53d4e8..53e0b93 100644 --- a/services/blive/live_server.go +++ b/services/blive/live_server.go @@ -21,8 +21,9 @@ var ( ) var ( - ErrNotFound = errors.New("房間不存在") - ErrTooFast = errors.New("請求頻繁") + ErrNotFound = errors.New("房間不存在") + ErrTooFast = errors.New("請求頻繁") + ErrCacheNotFound = errors.New("緩存不存在") ) func GetListening() []interface{} {