From d46b8d25b33029917b76009ded7d8627bc9a50f8 Mon Sep 17 00:00:00 2001 From: Joftal <42807405+Joftal@users.noreply.github.com> Date: Mon, 23 Feb 2026 20:09:37 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=BE=AA=E7=8E=AF?= =?UTF-8?q?=E7=9B=91=E6=8E=A7=E6=97=B6=E9=97=B4=E5=A4=B1=E6=95=88=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/cmd/bililive/bililive.go | 1 + src/instance/instance.go | 2 ++ src/servers/handler.go | 14 +++++++------- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/cmd/bililive/bililive.go b/src/cmd/bililive/bililive.go index 490acde..417322c 100644 --- a/src/cmd/bililive/bililive.go +++ b/src/cmd/bililive/bililive.go @@ -236,6 +236,7 @@ func main() { // 这样可以通过取消根 context 来优雅地关闭所有 goroutine rootCtx, rootCancel := context.WithCancel(context.Background()) ctx := context.WithValue(rootCtx, instance.Key, inst) + inst.Ctx = ctx logger := log.New(ctx) logger.Infof("%s Version: %s Link Start", consts.AppName, consts.AppVersion) diff --git a/src/instance/instance.go b/src/instance/instance.go index a088dc0..7975917 100644 --- a/src/instance/instance.go +++ b/src/instance/instance.go @@ -1,6 +1,7 @@ package instance import ( + "context" "sync" "github.com/bililive-go/bililive-go/src/interfaces" @@ -8,6 +9,7 @@ import ( ) type Instance struct { + Ctx context.Context // 应用级 context,不随 HTTP 请求结束而取消 WaitGroup sync.WaitGroup Lives LiveMap Cache gcache.Cache diff --git a/src/servers/handler.go b/src/servers/handler.go index dfa067a..a17f277 100644 --- a/src/servers/handler.go +++ b/src/servers/handler.go @@ -423,7 +423,7 @@ func parseLiveAction(writer http.ResponseWriter, r *http.Request) { } switch vars["action"] { case "start": - if err := startListening(r.Context(), live); err != nil { + if err := startListening(inst.Ctx, live); err != nil { resp.ErrNo = http.StatusBadRequest resp.ErrMsg = err.Error() writeJsonWithStatusCode(writer, http.StatusBadRequest, resp) @@ -437,7 +437,7 @@ func parseLiveAction(writer http.ResponseWriter, r *http.Request) { "live_id": string(live.GetLiveId()), }) case "stop": - if err := stopListening(r.Context(), live.GetLiveId()); err != nil { + if err := stopListening(inst.Ctx, live.GetLiveId()); err != nil { resp.ErrNo = http.StatusBadRequest resp.ErrMsg = err.Error() writeJsonWithStatusCode(writer, http.StatusBadRequest, resp) @@ -604,9 +604,9 @@ func switchStreamHandler(writer http.ResponseWriter, r *http.Request) { return } - if recorderMgr.HasRecorder(r.Context(), live.GetLiveId()) { + if recorderMgr.HasRecorder(inst.Ctx, live.GetLiveId()) { // 重启录制器以应用新的流设置 - if err := recorderMgr.RestartRecorder(r.Context(), live); err != nil { + if err := recorderMgr.RestartRecorder(inst.Ctx, live); err != nil { resp.ErrNo = http.StatusInternalServerError resp.ErrMsg = "重启录制器失败: " + err.Error() writeJsonWithStatusCode(writer, http.StatusInternalServerError, resp) @@ -677,7 +677,7 @@ func addLives(writer http.ResponseWriter, r *http.Request) { gjson.ParseBytes(b).ForEach(func(key, value gjson.Result) bool { isListen := value.Get("listen").Bool() urlStr := strings.Trim(value.Get("url").String(), " ") - if retInfo, err := addLiveImpl(r.Context(), urlStr, isListen); err != nil { + if retInfo, err := addLiveImpl(instance.GetInstance(r.Context()).Ctx, urlStr, isListen); err != nil { msg := urlStr + ": " + err.Error() applog.GetLogger().Error(msg) errorMessages = append(errorMessages, msg) @@ -752,7 +752,7 @@ func removeLive(writer http.ResponseWriter, r *http.Request) { }) return } - if err := removeLiveImpl(r.Context(), live); err != nil { + if err := removeLiveImpl(inst.Ctx, live); err != nil { writeJsonWithStatusCode(writer, http.StatusBadRequest, commonResp{ ErrNo: http.StatusBadRequest, ErrMsg: err.Error(), @@ -826,7 +826,7 @@ func putRawConfig(writer http.ResponseWriter, r *http.Request) { }) return } - ctx := r.Context() + ctx := instance.GetInstance(r.Context()).Ctx var jsonBody map[string]any json.Unmarshal(b, &jsonBody) newConfig, err := configs.NewConfigWithBytes([]byte(jsonBody["config"].(string))) From 850a9db269bdd42d4e1e875f5ecbee0ff6b3aecc Mon Sep 17 00:00:00 2001 From: Joftal <42807405+Joftal@users.noreply.github.com> Date: Wed, 25 Feb 2026 10:47:12 +0800 Subject: [PATCH 2/2] =?UTF-8?q?addLives=20=E5=92=8C=20putRawConfig=20?= =?UTF-8?q?=E6=8F=90=E5=89=8D=E8=8E=B7=E5=8F=96=20inst=EF=BC=8C=E6=B6=88?= =?UTF-8?q?=E9=99=A4=E9=93=BE=E5=BC=8F=E8=B0=83=E7=94=A8=E7=9A=84=20nil=20?= =?UTF-8?q?panic=20=E9=A3=8E=E9=99=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/servers/handler.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/servers/handler.go b/src/servers/handler.go index a17f277..bbf555e 100644 --- a/src/servers/handler.go +++ b/src/servers/handler.go @@ -665,6 +665,7 @@ func stopListening(ctx context.Context, liveId types.LiveID) error { ] */ func addLives(writer http.ResponseWriter, r *http.Request) { + inst := instance.GetInstance(r.Context()) b, err := io.ReadAll(r.Body) if err != nil { writeJSON(writer, map[string]any{ @@ -677,7 +678,7 @@ func addLives(writer http.ResponseWriter, r *http.Request) { gjson.ParseBytes(b).ForEach(func(key, value gjson.Result) bool { isListen := value.Get("listen").Bool() urlStr := strings.Trim(value.Get("url").String(), " ") - if retInfo, err := addLiveImpl(instance.GetInstance(r.Context()).Ctx, urlStr, isListen); err != nil { + if retInfo, err := addLiveImpl(inst.Ctx, urlStr, isListen); err != nil { msg := urlStr + ": " + err.Error() applog.GetLogger().Error(msg) errorMessages = append(errorMessages, msg) @@ -818,6 +819,7 @@ func getRawConfig(writer http.ResponseWriter, r *http.Request) { } func putRawConfig(writer http.ResponseWriter, r *http.Request) { + inst := instance.GetInstance(r.Context()) b, err := io.ReadAll(r.Body) if err != nil { writeJsonWithStatusCode(writer, http.StatusBadRequest, commonResp{ @@ -826,7 +828,7 @@ func putRawConfig(writer http.ResponseWriter, r *http.Request) { }) return } - ctx := instance.GetInstance(r.Context()).Ctx + ctx := inst.Ctx var jsonBody map[string]any json.Unmarshal(b, &jsonBody) newConfig, err := configs.NewConfigWithBytes([]byte(jsonBody["config"].(string)))