Skip to content

Commit

Permalink
fix sse
Browse files Browse the repository at this point in the history
  • Loading branch information
zc2638 committed Jan 15, 2024
1 parent 98cd1c8 commit 5ac70dd
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 12 deletions.
12 changes: 7 additions & 5 deletions core/clients/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package clients
import (
"context"
"errors"
"fmt"
"io"
"net/http"
"net/url"
Expand Down Expand Up @@ -48,11 +47,14 @@ func handleClientError(resp *resty.Response, err error) error {
return err
}
if resp.StatusCode() >= http.StatusBadRequest {
b, err := io.ReadAll(resp.RawBody())
if err != nil {
return fmt.Errorf("read body error failed: %v", err)
body := resp.String()
if body == "" {
b, err := io.ReadAll(resp.RawBody())
if err == nil {
body = strings.TrimSpace(string(b))
}
}
body := strings.TrimSpace(string(b))

errStr, err := strconv.Unquote(body)
if err != nil {
errStr = body
Expand Down
7 changes: 3 additions & 4 deletions core/clients/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,9 +356,6 @@ func (c *serverV1) LogWatch(ctx context.Context, namespace, name string, number,
return context.Canceled
default:
}
if errors.Is(err, io.EOF) {
return nil
}
if err != nil {
return err
}
Expand All @@ -382,7 +379,9 @@ func (c *serverV1) LogWatch(ctx context.Context, namespace, name string, number,
logCh <- &line
return nil
})
errCh <- err
if err != nil {
errCh <- err
}
close(errCh)
}()
return logCh, errCh, nil
Expand Down
2 changes: 2 additions & 0 deletions core/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ var corsOpts = cors.Options{

func New(log *wslog.Logger, db *gorm.DB, ll livelog.Interface, sched scheduler.Interface) http.Handler {
apiMiddlewares := chi.Middlewares{
middleware.Logger,
middleware.Recoverer,
cors.New(corsOpts).Handler,
serviceMiddleware(log, ll, sched, db),
timeoutMiddleware,
Expand Down
12 changes: 12 additions & 0 deletions core/handler/server/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,5 +134,17 @@ func logWatch() http.HandlerFunc {
}
}()
_ = sse.SendLoop[*livelog.Line](ctx, sender, lineCh, errCh)

// 断开连接并返回状态码204 No Content
//conn, ok := w.(http.CloseNotifier).CloseNotify()
//select {
//case <-conn:
// return
//default:
// closeErr := conn.Close()
// if closeErr != nil && !ok {
// panic(closeErr)
// }
//}
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/zc2638/ink
go 1.21.0

require (
github.com/99nil/gopkg v0.0.0-20240114071414-7dc8bfdd19e5
github.com/99nil/gopkg v0.0.0-20240115131111-1e1dfcd22774
github.com/docker/distribution v2.8.2+incompatible
github.com/docker/docker v24.0.5+incompatible
github.com/docker/go-units v0.5.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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-20240114071414-7dc8bfdd19e5 h1:sYpx9nb+ihnkXed2Ow+0bHo0B4OkoyM3P835MarqQ2I=
github.com/99nil/gopkg v0.0.0-20240114071414-7dc8bfdd19e5/go.mod h1:yQy7Xa2r1VHYfgqfCuDE6VGDmBt8xJgDVTZ2Bw3xTW0=
github.com/99nil/gopkg v0.0.0-20240115131111-1e1dfcd22774 h1:paJaQit3DgK1B018j8ybrnoQ78MS594nGM6FSxUsN/0=
github.com/99nil/gopkg v0.0.0-20240115131111-1e1dfcd22774/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=
Expand Down

0 comments on commit 5ac70dd

Please sign in to comment.