Merge pull request #1080 from Joftal/dev

修复循环监控时间失效的问题:HTTP 响应返回后,r.Context() 被取消
This commit is contained in:
kira1928 authored and GitHub committed 2026-02-25 14:14:49 +09:00
commit ddb3344680
3 files changed
+12 -7

No files matched your search

+1
View File
@@ -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)
+2
View File
@@ -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
+9 -7
View File
@@ -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)
@@ -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(r.Context(), 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)
@@ -752,7 +753,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(),
@@ -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 := r.Context()
ctx := inst.Ctx
var jsonBody map[string]any
json.Unmarshal(b, &jsonBody)
newConfig, err := configs.NewConfigWithBytes([]byte(jsonBody["config"].(string)))