Flow optimization
This commit is contained in:
1
api.go
1
api.go
@@ -165,6 +165,7 @@ func apiHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// If still not found, request and cache the music item in a separate goroutine
|
// If still not found, request and cache the music item in a separate goroutine
|
||||||
|
// 直接进行流式播放
|
||||||
if !found {
|
if !found {
|
||||||
fmt.Println("[Info] Updating music item cache from API request.")
|
fmt.Println("[Info] Updating music item cache from API request.")
|
||||||
musicItem = requestAndCacheMusic(song, singer)
|
musicItem = requestAndCacheMusic(song, singer)
|
||||||
|
|||||||
33
file.go
33
file.go
@@ -1,14 +1,12 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// ListFiles function: Traverse all files in the specified directory and return a slice of the file path
|
// ListFiles function: Traverse all files in the specified directory and return a slice of the file path
|
||||||
@@ -122,36 +120,7 @@ func fileHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
// 特殊处理空music.mp3
|
// 特殊处理空music.mp3
|
||||||
isEmptyMusic := (err == nil && len(fileContent) == 0 && strings.HasSuffix(filePath, "/music.mp3"))
|
isEmptyMusic := (err == nil && len(fileContent) == 0 && strings.HasSuffix(filePath, "/music.mp3"))
|
||||||
if err != nil || isEmptyMusic {
|
if err != nil || isEmptyMusic {
|
||||||
// 智能等待
|
// 没有/空的music.mp3文件,直接返回404
|
||||||
if strings.HasPrefix(filePath, "/cache/music/") {
|
|
||||||
maxWaitSec := 10
|
|
||||||
if strings.HasSuffix(filePath, "/music.mp3") {
|
|
||||||
maxWaitSec = 60
|
|
||||||
}
|
|
||||||
|
|
||||||
// 指数退避重试:快速响应文件生成
|
|
||||||
start := time.Now()
|
|
||||||
for i := 0; ; i++ {
|
|
||||||
elapsed := time.Since(start)
|
|
||||||
if elapsed.Seconds() > float64(maxWaitSec) {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
// 重试间隔 = min(200ms * 2^i, 1s)
|
|
||||||
waitDuration := time.Duration(200*(1<<uint(i))) * time.Millisecond
|
|
||||||
if waitDuration > time.Second {
|
|
||||||
waitDuration = time.Second
|
|
||||||
}
|
|
||||||
time.Sleep(waitDuration)
|
|
||||||
|
|
||||||
// 只检查解码后的主路径(避免冗余检查)
|
|
||||||
if info, statErr := os.Stat(fullPath); statErr == nil && (!isEmptyMusic || info.Size() > 0) {
|
|
||||||
http.ServeFile(w, r, fullPath)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fmt.Printf("[FAST] Timeout after %.1fs waiting for: %s\n", time.Since(start).Seconds(), fullPath)
|
|
||||||
}
|
|
||||||
NotFoundHandler(w, r)
|
NotFoundHandler(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -167,6 +167,7 @@ func streamConvertToWriter(inputURL string, w http.ResponseWriter) error {
|
|||||||
"-threads", "0",
|
"-threads", "0",
|
||||||
"-ac", "1", "-ar", "24000", "-b:a", "32k", "-q:a", "9",
|
"-ac", "1", "-ar", "24000", "-b:a", "32k", "-q:a", "9",
|
||||||
"-f", "mp3",
|
"-f", "mp3",
|
||||||
|
"-map_metadata", "-1",
|
||||||
"pipe:1") // 输出到 stdout
|
"pipe:1") // 输出到 stdout
|
||||||
|
|
||||||
// 获取 stdout pipe
|
// 获取 stdout pipe
|
||||||
|
|||||||
Reference in New Issue
Block a user