forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_handler.go
69 lines (56 loc) · 1.8 KB
/
http_handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright 2020 PingCAP, Inc. Licensed under Apache-2.0.
package export
import (
"net"
"net/http"
"net/http/pprof"
"strings"
"time"
"github.com/pingcap/errors"
tcontext "github.com/pingcap/tidb/dumpling/context"
"github.com/pingcap/tidb/dumpling/log"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/soheilhy/cmux"
)
var cmuxReadTimeout = 10 * time.Second
func startHTTPServer(tctx *tcontext.Context, lis net.Listener) {
router := http.NewServeMux()
router.Handle("/metrics", promhttp.Handler())
router.HandleFunc("/debug/pprof/", pprof.Index)
router.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
router.HandleFunc("/debug/pprof/profile", pprof.Profile)
router.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
router.HandleFunc("/debug/pprof/trace", pprof.Trace)
httpServer := &http.Server{
Handler: router,
}
err := httpServer.Serve(lis)
err = errors.Cause(err)
if err != nil && !isErrNetClosing(err) && err != http.ErrServerClosed {
tctx.L().Warn("dumpling http handler return with error", log.ShortError(err))
}
}
func startDumplingService(tctx *tcontext.Context, addr string) error {
rootLis, err := net.Listen("tcp", addr)
if err != nil {
return errors.Annotate(err, "start listening")
}
// create a cmux
m := cmux.New(rootLis)
m.SetReadTimeout(cmuxReadTimeout) // set a timeout, ref: https://github.com/pingcap/tidb-binlog/pull/352
httpL := m.Match(cmux.HTTP1Fast())
go startHTTPServer(tctx, httpL)
err = m.Serve() // start serving, block
if err != nil && isErrNetClosing(err) {
err = nil
}
return err
}
var useOfClosedErrMsg = "use of closed network connection"
// isErrNetClosing checks whether is an ErrNetClosing error
func isErrNetClosing(err error) bool {
if err == nil {
return false
}
return strings.Contains(err.Error(), useOfClosedErrMsg)
}