Skip to content
This repository has been archived by the owner on Oct 12, 2023. It is now read-only.

Commit

Permalink
listening info 改用緩存
Browse files Browse the repository at this point in the history
  • Loading branch information
eric2788 committed Apr 25, 2022
1 parent cbd21f2 commit 4626a3d
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 33 deletions.
8 changes: 1 addition & 7 deletions controller/listening/listening.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
7 changes: 6 additions & 1 deletion services/api/room.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}
}

}
Expand Down
7 changes: 6 additions & 1 deletion services/api/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}
}
}

Expand Down
23 changes: 1 addition & 22 deletions services/blive/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand All @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down
5 changes: 3 additions & 2 deletions services/blive/live_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ var (
)

var (
ErrNotFound = errors.New("房間不存在")
ErrTooFast = errors.New("請求頻繁")
ErrNotFound = errors.New("房間不存在")
ErrTooFast = errors.New("請求頻繁")
ErrCacheNotFound = errors.New("緩存不存在")
)

func GetListening() []interface{} {
Expand Down

0 comments on commit 4626a3d

Please sign in to comment.