]> 127.0.0.1 Git - bili_danmu/.git/commitdiff
Improve 避免客户端实时回放导致卡顿 (#154)
authorqydysky <qydysky@foxmail.com>
Mon, 27 Jan 2025 06:17:01 +0000 (14:17 +0800)
committerGitHub <noreply@github.com>
Mon, 27 Jan 2025 06:17:01 +0000 (14:17 +0800)
* Improve 避免客户端实时回放导致卡顿

* Improve 避免客户端实时回放导致卡顿

* Improve 避免客户端实时回放导致卡顿

* Add 直播流保存写入超时

Reply/F.go
Reply/stream.go
demo/config/config_K_v.json
go.mod
go.sum

index 608ca8826be329cfe80c0bbc938fcd73028bd142..3e6341dd8b4824b52a8b0be4f3e613eba75d1d65 100644 (file)
@@ -1283,7 +1283,7 @@ func init() {
                                //      }
                                // }
 
-                               if e := currentStreamO.PusherToHttp(conn, w, r, startFunc, stopFunc); e != nil {
+                               if e := currentStreamO.PusherToHttp(flog, conn, w, r, startFunc, stopFunc); e != nil {
                                        flog.L(`W: `, e)
                                }
                        }
index 10b713ab0d8fd187741d30b321adf89468b4700f..c54b4dca596218f5cc420823b4412c14387ad7ea 100644 (file)
@@ -40,6 +40,7 @@ import (
        signal "github.com/qydysky/part/signal"
        slice "github.com/qydysky/part/slice"
        pstring "github.com/qydysky/part/strings"
+       pu "github.com/qydysky/part/util"
        pweb "github.com/qydysky/part/web"
 )
 
@@ -1556,28 +1557,42 @@ func (t *M4SStream) PusherToFile(contextC context.Context, filepath string, star
                return e
        }
 
-       contextC, done := pctx.WaitCtx(contextC)
+       ctx1, done := pctx.WaitCtx(contextC)
        defer done()
 
+       to := 2.0
+       if tmp, ok := t.common.K_v.LoadV("直播流保存写入超时").(float64); ok && tmp > 2 {
+               to = tmp
+       }
+
        _, _ = f.Write(t.getFirstBuf(), true)
        cancelRec := t.Stream_msg.Pull_tag(map[string]func([]byte) bool{
                `data`: func(b []byte) bool {
+                       defer pu.Callback(func(startT time.Time, args ...any) {
+                               if dru := time.Since(startT).Seconds(); dru > to {
+                                       t.log.L("W: ", "磁盘写入超时", dru)
+                                       done()
+                               }
+                       })()
+
                        select {
-                       case <-contextC.Done():
+                       case <-ctx1.Done():
                                return true
                        default:
                        }
                        if len(b) == 0 {
                                return true
                        }
-                       _, _ = f.Write(b, true)
+                       if n, err := f.Write(b, true); err != nil || n == 0 {
+                               done()
+                       }
                        return false
                },
                `close`: func(_ []byte) bool {
                        return true
                },
        })
-       <-contextC.Done()
+       <-ctx1.Done()
        cancelRec()
 
        if e := stopFunc(t); e != nil {
@@ -1590,7 +1605,7 @@ func (t *M4SStream) PusherToFile(contextC context.Context, filepath string, star
 // 流服务推送方法
 //
 // 在客户端存在某种代理时,将有可能无法监测到客户端关闭,这有可能导致goroutine泄漏
-func (t *M4SStream) PusherToHttp(conn net.Conn, w http.ResponseWriter, r *http.Request, startFunc func(*M4SStream) error, stopFunc func(*M4SStream) error) error {
+func (t *M4SStream) PusherToHttp(plog *log.Log_interface, conn net.Conn, w http.ResponseWriter, r *http.Request, startFunc func(*M4SStream) error, stopFunc func(*M4SStream) error) error {
        switch t.stream_type {
        case `m3u8`:
                fallthrough
@@ -1647,6 +1662,8 @@ func (t *M4SStream) PusherToHttp(conn net.Conn, w http.ResponseWriter, r *http.R
                return err
        }
 
+       w = pweb.WithCache(w)
+
        var cancelRec = t.Stream_msg.Pull_tag(map[string]func([]byte) bool{
                `data`: func(b []byte) bool {
                        select {
@@ -1657,8 +1674,13 @@ func (t *M4SStream) PusherToHttp(conn net.Conn, w http.ResponseWriter, r *http.R
                        if len(b) == 0 {
                                return true
                        }
+
                        _ = conn.SetWriteDeadline(time.Now().Add(time.Second * 30))
                        if n, err := w.Write(b); err != nil || n == 0 {
+                               if pweb.IsCacheBusy(err) {
+                                       plog.L(`I: `, r.RemoteAddr, "回放连接慢,缓存跳过")
+                                       return false
+                               }
                                return true
                        }
                        return false
index c9770e015670b6368815f1b0017093d1c2798f54..15f1a61faf1ee5a48a0d10c76bf9a865b5f56ba3 100644 (file)
@@ -95,6 +95,8 @@
     "直播流保存位置": "./live",
     "直播流保存天数-help": "当t日有1录播时,会尝试删除t-n日及之前的1or2个最早的录播,目录下有.keep文件将忽略。小于1的数将禁用此功能",
     "直播流保存天数": 4,
+    "直播流保存写入超时-help": "默认2,单位秒,小于默认无效,写入时间过大会影响流下载。当写入超过指定时长时,保存中止",
+    "直播流保存写入超时": 2,
     "标题修改检测s-help": "默认900秒,少于默认无效。直播间标题引入审核机制,触发审核时会接收到一个roomchange但标题不变,将持续检测指定时长,如通过审核将修改录播标题",
     "标题修改检测s": 900,
     "直播流保存到文件": true,
diff --git a/go.mod b/go.mod
index 20c6ebb429e7a95cacdfe8dda0277c8a38841873..f3fef5a122cc68e915ff31c6d3523eb1dfddeae9 100644 (file)
--- a/go.mod
+++ b/go.mod
@@ -5,7 +5,7 @@ go 1.23
 require (
        github.com/gotk3/gotk3 v0.6.4
        github.com/mdp/qrterminal/v3 v3.2.0
-       github.com/qydysky/part v0.28.20250120190624
+       github.com/qydysky/part v0.28.20250127053704
        github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e
        github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966
        golang.org/x/text v0.21.0
diff --git a/go.sum b/go.sum
index 474c27e8d0140b24e8188b83e140e4bf5bb8fd1d..46f848ac5bfc5b653119791740df23bcf57e0b22 100644 (file)
--- a/go.sum
+++ b/go.sum
@@ -48,8 +48,8 @@ github.com/qydysky/biliApi v0.0.0-20240725184407-15076dddb6fb h1:dtSpNF9hLQa09TU
 github.com/qydysky/biliApi v0.0.0-20240725184407-15076dddb6fb/go.mod h1:om024vfxALQ5vxsbaGoMm8IS0esLYBnEOpJI8FsGoDg=
 github.com/qydysky/brotli v0.0.0-20240828134800-e9913a6e7ed9 h1:k451T+bpsLr+Dq9Ujo+Qtx0iomRA1XXS5ttlEojvfuQ=
 github.com/qydysky/brotli v0.0.0-20240828134800-e9913a6e7ed9/go.mod h1:cI8/gy/wjy2Eb+p2IUj2ZuDnC8R5Vrx3O0VMPvMvphA=
-github.com/qydysky/part v0.28.20250120190624 h1:KyxUMxO0k5/loU1TP9usKJpN8YDGPgrIC6Yc37OeGws=
-github.com/qydysky/part v0.28.20250120190624/go.mod h1:RAb3G05OaqCSRWFJz9FnONB6iqF/Dk4R+Z5c/H7mWSg=
+github.com/qydysky/part v0.28.20250127053704 h1:UkhwV4IkqkP/u3H3IlnwG9lywSvsIIMgXkmu+MW/hq4=
+github.com/qydysky/part v0.28.20250127053704/go.mod h1:RAb3G05OaqCSRWFJz9FnONB6iqF/Dk4R+Z5c/H7mWSg=
 github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
 github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
 github.com/shirou/gopsutil v3.21.11+incompatible h1:+1+c1VGhc88SSonWP6foOcLhvnKlUeu/erjjvaPEYiI=