Skip to content

Commit

Permalink
*: timeout for cli and server (pingcap#149)
Browse files Browse the repository at this point in the history
  • Loading branch information
xhebox committed Mar 13, 2023
1 parent 38dea47 commit 568df0b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
FROM alpine:edge as builder

ADD https://raw.githubusercontent.com/njhallett/apk-fastest-mirror/main/apk-fastest-mirror.sh /
RUN sh /apk-fastest-mirror.sh -t 50 && apk add --no-cache --progress git make go
RUN sed -i 's/https/http/g' /apk-fastest-mirror.sh && sh /apk-fastest-mirror.sh -t 50 && apk add --no-cache --progress git make go
ARG BUILDFLAGS
ARG GOPROXY
ADD . /proxy
Expand Down
10 changes: 10 additions & 0 deletions lib/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ package cli

import (
"crypto/tls"
"net"
"net/http"
"time"

"github.com/pingcap/TiProxy/lib/config"
"github.com/pingcap/TiProxy/lib/util/cmd"
Expand Down Expand Up @@ -72,6 +74,14 @@ func GetRootCmd(tlsConfig *tls.Config) *cobra.Command {
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
TLSClientConfig: tlsConfig,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext,
MaxIdleConns: 100,
IdleConnTimeout: 30 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
},
}
ctx.CUrls = *curls
Expand Down
13 changes: 11 additions & 2 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"crypto/tls"
"net"
"net/http"
"time"

ginzap "github.com/gin-contrib/zap"
"github.com/gin-gonic/gin"
Expand All @@ -44,6 +45,8 @@ import (
const (
// DefAPILimit is the global API limit per second.
DefAPILimit = 100
// DefConnTimeout is used as timeout duration in the HTTP server.
DefConnTimeout = 30 * time.Second
)

type Server struct {
Expand Down Expand Up @@ -105,11 +108,11 @@ func NewServer(ctx context.Context, sctx *sctx.Context) (srv *Server, err error)
gin.Recovery(),
ginzap.Ginzap(slogger, "", true),
func(c *gin.Context) {
_ = limit.Take()
if !ready.Load() {
c.Abort()
c.JSON(http.StatusInternalServerError, "service not ready")
}
_ = limit.Take()
},
)

Expand All @@ -130,7 +133,13 @@ func NewServer(ctx context.Context, sctx *sctx.Context) (srv *Server, err error)
}

srv.wg.Run(func() {
slogger.Info("HTTP closed", zap.Error(engine.RunListener(srv.HTTPListener)))
hsrv := http.Server{
Handler: engine.Handler(),
ReadHeaderTimeout: DefConnTimeout,
WriteTimeout: DefConnTimeout,
IdleTimeout: DefConnTimeout,
}
slogger.Info("HTTP closed", zap.Error(hsrv.Serve(srv.HTTPListener)))
})
}

Expand Down

0 comments on commit 568df0b

Please sign in to comment.