-
Notifications
You must be signed in to change notification settings - Fork 75
/
server.go
100 lines (88 loc) · 2.92 KB
/
server.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package diagnostics
import (
"context"
"fmt"
"net/http"
pprof "net/http/pprof"
"time"
)
import (
"github.com/bakito/go-log-logr-adapter/adapter"
)
import (
diagnostics_config "github.com/apache/dubbo-kubernetes/pkg/config/diagnostics"
"github.com/apache/dubbo-kubernetes/pkg/core"
"github.com/apache/dubbo-kubernetes/pkg/core/runtime/component"
)
var diagnosticsServerLog = core.Log.WithName("xds-server").WithName("diagnostics")
type diagnosticsServer struct {
config *diagnostics_config.DiagnosticsConfig
}
func (s *diagnosticsServer) NeedLeaderElection() bool {
return false
}
// Make sure that grpcServer implements all relevant interfaces
var (
_ component.Component = &diagnosticsServer{}
)
func (s *diagnosticsServer) Start(stop <-chan struct{}) error {
mux := http.NewServeMux()
mux.HandleFunc("/ready", func(resp http.ResponseWriter, _ *http.Request) {
resp.WriteHeader(http.StatusOK)
})
mux.HandleFunc("/healthy", func(resp http.ResponseWriter, _ *http.Request) {
resp.WriteHeader(http.StatusOK)
})
mux.HandleFunc("/debug/pprof/", pprof.Index)
mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
httpServer := &http.Server{
Addr: fmt.Sprintf(":%d", s.config.ServerPort),
Handler: mux,
ReadHeaderTimeout: time.Second,
ErrorLog: adapter.ToStd(diagnosticsServerLog),
}
diagnosticsServerLog.Info("starting diagnostic server", "interface", "0.0.0.0", "port", s.config.ServerPort)
errChan := make(chan error)
go func() {
defer close(errChan)
var err error
err = httpServer.ListenAndServe()
if err != nil {
switch err {
case http.ErrServerClosed:
diagnosticsServerLog.Info("shutting down server")
default:
diagnosticsServerLog.Error(err, "could not start HTTP Server")
errChan <- err
}
return
}
diagnosticsServerLog.Info("terminated normally")
}()
select {
case <-stop:
diagnosticsServerLog.Info("stopping")
return httpServer.Shutdown(context.Background())
case err := <-errChan:
return err
}
}