From 98cd1c846b5d9397b33de6faf06676f60c024560 Mon Sep 17 00:00:00 2001 From: zc Date: Sun, 14 Jan 2024 15:14:14 +0800 Subject: [PATCH] fix --- core/clients/common.go | 10 +++++++++- core/clients/server.go | 5 ++++- core/handler/server/log.go | 5 ----- go.mod | 2 +- go.sum | 4 ++-- pkg/livelog/file.go | 4 ++++ 6 files changed, 20 insertions(+), 10 deletions(-) diff --git a/core/clients/common.go b/core/clients/common.go index b264dfd..41ee817 100644 --- a/core/clients/common.go +++ b/core/clients/common.go @@ -17,9 +17,12 @@ package clients import ( "context" "errors" + "fmt" + "io" "net/http" "net/url" "strconv" + "strings" "github.com/go-resty/resty/v2" @@ -45,11 +48,16 @@ func handleClientError(resp *resty.Response, err error) error { return err } if resp.StatusCode() >= http.StatusBadRequest { - body := resp.String() + b, err := io.ReadAll(resp.RawBody()) + if err != nil { + return fmt.Errorf("read body error failed: %v", err) + } + body := strings.TrimSpace(string(b)) errStr, err := strconv.Unquote(body) if err != nil { errStr = body } + switch errStr { case constant.ErrNoRecord.Error(): return constant.ErrNoRecord diff --git a/core/clients/server.go b/core/clients/server.go index d8ed098..6e0148e 100644 --- a/core/clients/server.go +++ b/core/clients/server.go @@ -346,7 +346,10 @@ func (c *serverV1) LogWatch(ctx context.Context, namespace, name string, number, logCh := make(chan *livelog.Line) errCh := make(chan error) go func() { - sseParser := sse.NewParser(resp.RawBody()) + body := resp.RawBody() + defer body.Close() + + sseParser := sse.NewParser(body) err = sseParser.ReadEventLoop(func(message *sse.Message, err error) error { select { case <-ctx.Done(): diff --git a/core/handler/server/log.go b/core/handler/server/log.go index 9664baa..7bfa461 100644 --- a/core/handler/server/log.go +++ b/core/handler/server/log.go @@ -16,7 +16,6 @@ package server import ( "context" - "fmt" "io" "net/http" "strconv" @@ -116,7 +115,6 @@ func logWatch() http.HandlerFunc { } if closeCh == nil { // TODO step pending 时 livelog 未创建,导致 close nil 的处理 - wrapper.InternalError(w, "already closed") return } @@ -130,12 +128,9 @@ func logWatch() http.HandlerFunc { go func() { select { case <-ctx.Done(): - fmt.Println("============= Context Done =============") case <-sender.WaitForClose(): - fmt.Println("============= Sender Done =============") case <-closeCh: errCh <- io.EOF - fmt.Println("============= Watch Close =============") } }() _ = sse.SendLoop[*livelog.Line](ctx, sender, lineCh, errCh) diff --git a/go.mod b/go.mod index cab9f46..01695a2 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/zc2638/ink go 1.21.0 require ( - github.com/99nil/gopkg v0.0.0-20240114061414-5c020efdb0c4 + github.com/99nil/gopkg v0.0.0-20240114071414-7dc8bfdd19e5 github.com/docker/distribution v2.8.2+incompatible github.com/docker/docker v24.0.5+incompatible github.com/docker/go-units v0.5.0 diff --git a/go.sum b/go.sum index b504efa..ca03b1c 100644 --- a/go.sum +++ b/go.sum @@ -53,8 +53,8 @@ cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9 cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3fOKtUw0Xmo= dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= gioui.org v0.0.0-20210308172011-57750fc8a0a6/go.mod h1:RSH6KIUZ0p2xy5zHDxgAM4zumjgTw83q2ge/PI+yyw8= -github.com/99nil/gopkg v0.0.0-20240114061414-5c020efdb0c4 h1:a200oSwBJf8X8dBfWLXs09cD0MBFIOoqAqvd2BY5/4k= -github.com/99nil/gopkg v0.0.0-20240114061414-5c020efdb0c4/go.mod h1:yQy7Xa2r1VHYfgqfCuDE6VGDmBt8xJgDVTZ2Bw3xTW0= +github.com/99nil/gopkg v0.0.0-20240114071414-7dc8bfdd19e5 h1:sYpx9nb+ihnkXed2Ow+0bHo0B4OkoyM3P835MarqQ2I= +github.com/99nil/gopkg v0.0.0-20240114071414-7dc8bfdd19e5/go.mod h1:yQy7Xa2r1VHYfgqfCuDE6VGDmBt8xJgDVTZ2Bw3xTW0= github.com/AdaLogics/go-fuzz-headers v0.0.0-20210715213245-6c3934b029d8/go.mod h1:CzsSbkDixRphAF5hS6wbMKq0eI6ccJRb7/A0M6JBnwg= github.com/Azure/azure-pipeline-go v0.2.3/go.mod h1:x841ezTBIMG6O3lAcl8ATHnsOPVl2bqk7S3ta6S6u4k= github.com/Azure/azure-sdk-for-go v16.2.1+incompatible/go.mod h1:9XXNKU+eRnpl9moKnB4QOLf1HestfXbmab5FXxiDBjc= diff --git a/pkg/livelog/file.go b/pkg/livelog/file.go index b862f30..8a7f51b 100644 --- a/pkg/livelog/file.go +++ b/pkg/livelog/file.go @@ -254,5 +254,9 @@ func (f *file) Delete(_ context.Context, id string) error { return err } } + + f.mux.Lock() + delete(f.clients, id) + f.mux.Unlock() return nil }