Skip to content

Commit

Permalink
fix timeout middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
zc2638 committed Jan 12, 2024
1 parent efa47ab commit ccd83fc
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
6 changes: 4 additions & 2 deletions core/clients/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"io"
"strconv"
"strings"
"time"

"github.com/99nil/gopkg/sse"
"github.com/go-resty/resty/v2"
Expand Down Expand Up @@ -74,7 +75,7 @@ type server struct {

func (s *server) V1() ServerV1 {
addr := strings.TrimSuffix(s.Address, "/")
rc := resty.New().SetBaseURL(addr + "/api/core/v1")
rc := resty.New().SetBaseURL(addr + "/api/core/v1").SetTimeout(time.Minute)
return &serverV1{rc: rc}
}

Expand Down Expand Up @@ -363,7 +364,8 @@ func (c *serverV1) LogWatch(ctx context.Context, namespace, name string, number,
if strings.ToLower(message.Data) == "eof" {
return io.EOF
}
return errors.New(message.Data)
errCh <- errors.New(message.Data)
return nil
}
if message.Event != "data" {
return nil
Expand Down
1 change: 0 additions & 1 deletion core/command/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ func NewDaemon() *cobra.Command {
srv.WriteTimeout = 0
srv.Handler = handler.New(log, db, ll, sched)
log.Info(fmt.Sprintf("Daemon listen on %s", srv.Addr))

return srv.RunAndStop(context.Background())
},
}
Expand Down
9 changes: 8 additions & 1 deletion core/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"log/slog"
"net/http"
"regexp"
"time"

"github.com/99nil/gopkg/ctr"
Expand Down Expand Up @@ -102,11 +103,17 @@ func serviceMiddleware(log *wslog.Logger, ll livelog.Interface, sched scheduler.
}
}

var logWatchRe = regexp.MustCompile(`/api/core/.+/box/.+/.+/build/.+/logs/.+/.+`)

func timeoutMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if logWatchRe.MatchString(r.URL.Path) {
next.ServeHTTP(w, r)
return
}

ctx, cancel := context.WithTimeout(r.Context(), constant.DefaultHTTPTimeout)
defer cancel()

next.ServeHTTP(w, r.WithContext(ctx))
})
}
5 changes: 5 additions & 0 deletions core/handler/server/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package server

import (
"context"
"fmt"
"io"
"net/http"
"strconv"
Expand Down Expand Up @@ -114,6 +115,7 @@ func logWatch() http.HandlerFunc {
return
}
if closeCh == nil {
// TODO step pending 时 livelog 未创建,导致 close nil 的处理
wrapper.InternalError(w, "already closed")
return
}
Expand All @@ -128,9 +130,12 @@ 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)
Expand Down

0 comments on commit ccd83fc

Please sign in to comment.