Skip to content

Commit d643a22

Browse files
committed
refactor: 移除控制流相关代码
1 parent 958add5 commit d643a22

2 files changed

Lines changed: 106 additions & 110 deletions

File tree

internal/handler/emby.go

Lines changed: 55 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,23 @@ import (
2020
"regexp"
2121
"strconv"
2222
"strings"
23-
"sync"
24-
"sync/atomic"
2523

2624
"github.com/gin-gonic/gin"
2725
)
2826

29-
// 带引用计数的互斥锁
30-
type mutexWithRefCount struct {
31-
mu sync.Mutex
32-
refCount int32 // 使用 atomic 操作
33-
}
27+
// // 带引用计数的互斥锁
28+
// type mutexWithRefCount struct {
29+
// mu sync.Mutex
30+
// refCount int32 // 使用 atomic 操作
31+
// }
3432

3533
// Emby服务器处理器
3634
type EmbyServerHandler struct {
37-
server *emby.EmbyServer // Emby 服务器
38-
routerRules []RegexpRouteRule // 正则路由规则
39-
proxy *httputil.ReverseProxy // 反向代理
40-
httpStrmHandler StrmHandlerFunc
41-
playbackInfoMutex sync.Map // 视频流处理并发控制,确保同一个 item ID 的重定向请求串行化,避免重复获取缓存
35+
server *emby.EmbyServer // Emby 服务器
36+
routerRules []RegexpRouteRule // 正则路由规则
37+
proxy *httputil.ReverseProxy // 反向代理
38+
httpStrmHandler StrmHandlerFunc
39+
// playbackInfoMutex sync.Map // 视频流处理并发控制,确保同一个 item ID 的重定向请求串行化,避免重复获取缓存
4240
}
4341

4442
// 初始化
@@ -128,15 +126,15 @@ func (*EmbyServerHandler) GetSubtitleCacheRegexp() *regexp.Regexp {
128126
// /Items/:itemId/PlaybackInfo
129127
// 强制将 HTTPStrm 设置为支持直链播放和转码、AlistStrm 设置为支持直链播放并且禁止转码
130128
func (embyServerHandler *EmbyServerHandler) ModifyPlaybackInfo(rw *http.Response) error {
131-
// 检查 IsPlayback 参数,如果为 false 则不做修改直接返回
132-
// 从响应的请求中获取参数,因为响应对象包含原始请求
133-
// 使用不区分大小写的方式获取查询参数
134-
isPlayback := getQueryValueCaseInsensitive(rw.Request.URL.Query(), "IsPlayback")
135-
logging.Debugf("IsPlayback 参数值: '%s' (请求 URL: %s)", isPlayback, rw.Request.URL.String())
136-
if strings.ToLower(isPlayback) == "false" {
137-
logging.Debug("IsPlayback=false,跳过 PlaybackInfo 修改")
138-
return nil
139-
}
129+
// // 检查 IsPlayback 参数,如果为 false 则不做修改直接返回
130+
// // 从响应的请求中获取参数,因为响应对象包含原始请求
131+
// // 使用不区分大小写的方式获取查询参数
132+
// isPlayback := getQueryValueCaseInsensitive(rw.Request.URL.Query(), "IsPlayback")
133+
// logging.Debugf("IsPlayback 参数值: '%s' (请求 URL: %s)", isPlayback, rw.Request.URL.String())
134+
// if strings.ToLower(isPlayback) == "false" {
135+
// logging.Debug("IsPlayback=false,跳过 PlaybackInfo 修改")
136+
// return nil
137+
// }
140138

141139
defer rw.Body.Close()
142140
body, err := io.ReadAll(rw.Body)
@@ -254,42 +252,42 @@ func (embyServerHandler *EmbyServerHandler) VideosHandler(ctx *gin.Context) {
254252
// EmbyServer >= 4.9 ====> mediaSourceID = mediasource_31
255253
mediaSourceID := ctx.Query("mediasourceid")
256254

257-
// 从 URL 中提取 item ID(例如:/emby/videos/43609/stream 中的 43609)
258-
var itemID string
259-
if matches := constants.EmbyRegexp.Router.VideosHandler.FindStringSubmatch(orginalPath); len(matches) > 0 {
260-
parts := strings.Split(orginalPath, "/")
261-
for i, part := range parts {
262-
if part == "videos" && i+1 < len(parts) {
263-
itemID = parts[i+1]
264-
break
265-
}
266-
}
267-
}
268-
269-
// 并发控制:确保同一个 item ID 只有一个任务在运行
270-
// 将整个处理流程放在锁内,避免重复查询和重复获取重定向 URL
271-
var muWrapper *mutexWithRefCount
272-
if itemID != "" {
273-
// 加载或创建 mutex wrapper
274-
value, _ := embyServerHandler.playbackInfoMutex.LoadOrStore(itemID, &mutexWithRefCount{})
275-
muWrapper = value.(*mutexWithRefCount)
276-
277-
// 增加引用计数
278-
atomic.AddInt32(&muWrapper.refCount, 1)
279-
280-
// 锁定并处理
281-
muWrapper.mu.Lock()
282-
defer func() {
283-
muWrapper.mu.Unlock()
284-
// 减少引用计数
285-
refCount := atomic.AddInt32(&muWrapper.refCount, -1)
286-
// 如果没有其他 goroutine 在使用,删除这个 mutex 以避免内存泄漏
287-
if refCount == 0 {
288-
embyServerHandler.playbackInfoMutex.Delete(itemID)
289-
}
290-
}()
291-
logging.Debugf("开始处理 item %s 的 VideosHandler 请求", itemID)
292-
}
255+
// // 从 URL 中提取 item ID(例如:/emby/videos/43609/stream 中的 43609)
256+
// var itemID string
257+
// if matches := constants.EmbyRegexp.Router.VideosHandler.FindStringSubmatch(orginalPath); len(matches) > 0 {
258+
// parts := strings.Split(orginalPath, "/")
259+
// for i, part := range parts {
260+
// if part == "videos" && i+1 < len(parts) {
261+
// itemID = parts[i+1]
262+
// break
263+
// }
264+
// }
265+
// }
266+
267+
// // 并发控制:确保同一个 item ID 只有一个任务在运行
268+
// // 将整个处理流程放在锁内,避免重复查询和重复获取重定向 URL
269+
// var muWrapper *mutexWithRefCount
270+
// if itemID != "" {
271+
// // 加载或创建 mutex wrapper
272+
// value, _ := embyServerHandler.playbackInfoMutex.LoadOrStore(itemID, &mutexWithRefCount{})
273+
// muWrapper = value.(*mutexWithRefCount)
274+
275+
// // 增加引用计数
276+
// atomic.AddInt32(&muWrapper.refCount, 1)
277+
278+
// // 锁定并处理
279+
// muWrapper.mu.Lock()
280+
// defer func() {
281+
// muWrapper.mu.Unlock()
282+
// // 减少引用计数
283+
// refCount := atomic.AddInt32(&muWrapper.refCount, -1)
284+
// // 如果没有其他 goroutine 在使用,删除这个 mutex 以避免内存泄漏
285+
// if refCount == 0 {
286+
// embyServerHandler.playbackInfoMutex.Delete(itemID)
287+
// }
288+
// }()
289+
// logging.Debugf("开始处理 item %s 的 VideosHandler 请求", itemID)
290+
// }
293291

294292
logging.Debugf("请求 ItemsServiceQueryItem:%s", mediaSourceID)
295293
itemResponse, err := embyServerHandler.server.ItemsServiceQueryItem(strings.Replace(mediaSourceID, "mediasource_", "", 1), 1, "Path,MediaSources") // 查询 item 需要去除前缀仅保留数字部分

internal/handler/jellyfin.go

Lines changed: 51 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,17 @@ import (
2020
"regexp"
2121
"strconv"
2222
"strings"
23-
"sync"
24-
"sync/atomic"
2523

2624
"github.com/gin-gonic/gin"
2725
)
2826

2927
// Jellyfin 服务器处理器
3028
type JellyfinHandler struct {
31-
server *jellyfin.Jellyfin // Jellyfin 服务器
32-
routerRules []RegexpRouteRule // 正则路由规则
33-
proxy *httputil.ReverseProxy // 反向代理
34-
httpStrmHandler StrmHandlerFunc
35-
playbackInfoMutex sync.Map // 视频流处理并发控制,确保同一个 item ID 的重定向请求串行化,避免重复获取缓存
29+
server *jellyfin.Jellyfin // Jellyfin 服务器
30+
routerRules []RegexpRouteRule // 正则路由规则
31+
proxy *httputil.ReverseProxy // 反向代理
32+
httpStrmHandler StrmHandlerFunc
33+
// playbackInfoMutex sync.Map // 视频流处理并发控制,确保同一个 item ID 的重定向请求串行化,避免重复获取缓存
3634
}
3735

3836
func NewJellyfinHander(addr string, apiKey string) (*JellyfinHandler, error) {
@@ -104,15 +102,15 @@ func (*JellyfinHandler) GetSubtitleCacheRegexp() *regexp.Regexp {
104102
// /Items/:itemId
105103
// 强制将 HTTPStrm 设置为支持直链播放和转码、AlistStrm 设置为支持直链播放并且禁止转码
106104
func (jellyfinHandler *JellyfinHandler) ModifyPlaybackInfo(rw *http.Response) error {
107-
// 检查 IsPlayback 参数,如果为 false 则不做修改直接返回
108-
// 从响应的请求中获取参数,因为响应对象包含原始请求
109-
// 使用不区分大小写的方式获取查询参数
110-
isPlayback := getQueryValueCaseInsensitive(rw.Request.URL.Query(), "IsPlayback")
111-
logging.Debugf("IsPlayback 参数值: '%s' (请求 URL: %s)", isPlayback, rw.Request.URL.String())
112-
if strings.ToLower(isPlayback) == "false" {
113-
logging.Debug("IsPlayback=false,跳过 PlaybackInfo 修改")
114-
return nil
115-
}
105+
// // 检查 IsPlayback 参数,如果为 false 则不做修改直接返回
106+
// // 从响应的请求中获取参数,因为响应对象包含原始请求
107+
// // 使用不区分大小写的方式获取查询参数
108+
// isPlayback := getQueryValueCaseInsensitive(rw.Request.URL.Query(), "IsPlayback")
109+
// logging.Debugf("IsPlayback 参数值: '%s' (请求 URL: %s)", isPlayback, rw.Request.URL.String())
110+
// if strings.ToLower(isPlayback) == "false" {
111+
// logging.Debug("IsPlayback=false,跳过 PlaybackInfo 修改")
112+
// return nil
113+
// }
116114

117115
defer rw.Body.Close()
118116
data, err := io.ReadAll(rw.Body)
@@ -220,43 +218,43 @@ func (jellyfinHandler *JellyfinHandler) VideosHandler(ctx *gin.Context) {
220218
return
221219
}
222220

223-
// 从 URL 中提取 item ID(例如:/Videos/813a630bcf9c3f693a2ec8c498f868d2/stream 中的 813a630bcf9c3f693a2ec8c498f868d2)
224-
var itemID string
225-
path := ctx.Request.URL.Path
226-
if matches := constants.JellyfinRegexp.Router.VideosHandler.FindStringSubmatch(path); len(matches) > 0 {
227-
parts := strings.Split(path, "/")
228-
for i, part := range parts {
229-
if part == "Videos" && i+1 < len(parts) {
230-
itemID = parts[i+1]
231-
break
232-
}
233-
}
234-
}
235-
236-
// 并发控制:确保同一个 item ID 只有一个任务在运行
237-
// 将整个处理流程放在锁内,避免重复查询和重复获取重定向 URL
238-
var muWrapper *mutexWithRefCount
239-
if itemID != "" {
240-
// 加载或创建 mutex wrapper
241-
value, _ := jellyfinHandler.playbackInfoMutex.LoadOrStore(itemID, &mutexWithRefCount{})
242-
muWrapper = value.(*mutexWithRefCount)
243-
244-
// 增加引用计数
245-
atomic.AddInt32(&muWrapper.refCount, 1)
246-
247-
// 锁定并处理
248-
muWrapper.mu.Lock()
249-
defer func() {
250-
muWrapper.mu.Unlock()
251-
// 减少引用计数
252-
refCount := atomic.AddInt32(&muWrapper.refCount, -1)
253-
// 如果没有其他 goroutine 在使用,删除这个 mutex 以避免内存泄漏
254-
if refCount == 0 {
255-
jellyfinHandler.playbackInfoMutex.Delete(itemID)
256-
}
257-
}()
258-
logging.Debugf("开始处理 item %s 的 VideosHandler 请求", itemID)
259-
}
221+
// // 从 URL 中提取 item ID(例如:/Videos/813a630bcf9c3f693a2ec8c498f868d2/stream 中的 813a630bcf9c3f693a2ec8c498f868d2)
222+
// var itemID string
223+
// path := ctx.Request.URL.Path
224+
// if matches := constants.JellyfinRegexp.Router.VideosHandler.FindStringSubmatch(path); len(matches) > 0 {
225+
// parts := strings.Split(path, "/")
226+
// for i, part := range parts {
227+
// if part == "Videos" && i+1 < len(parts) {
228+
// itemID = parts[i+1]
229+
// break
230+
// }
231+
// }
232+
// }
233+
234+
// // 并发控制:确保同一个 item ID 只有一个任务在运行
235+
// // 将整个处理流程放在锁内,避免重复查询和重复获取重定向 URL
236+
// var muWrapper *mutexWithRefCount
237+
// if itemID != "" {
238+
// // 加载或创建 mutex wrapper
239+
// value, _ := jellyfinHandler.playbackInfoMutex.LoadOrStore(itemID, &mutexWithRefCount{})
240+
// muWrapper = value.(*mutexWithRefCount)
241+
242+
// // 增加引用计数
243+
// atomic.AddInt32(&muWrapper.refCount, 1)
244+
245+
// // 锁定并处理
246+
// muWrapper.mu.Lock()
247+
// defer func() {
248+
// muWrapper.mu.Unlock()
249+
// // 减少引用计数
250+
// refCount := atomic.AddInt32(&muWrapper.refCount, -1)
251+
// // 如果没有其他 goroutine 在使用,删除这个 mutex 以避免内存泄漏
252+
// if refCount == 0 {
253+
// jellyfinHandler.playbackInfoMutex.Delete(itemID)
254+
// }
255+
// }()
256+
// logging.Debugf("开始处理 item %s 的 VideosHandler 请求", itemID)
257+
// }
260258

261259
mediaSourceID := ctx.Query("mediasourceid")
262260
logging.Debugf("请求 ItemsServiceQueryItem:%s", mediaSourceID)

0 commit comments

Comments
 (0)