/
profile.go
200 lines (157 loc) · 4.12 KB
/
profile.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package launch
import (
"context"
"io"
"net"
"net/http"
netpprof "net/http/pprof"
"net/url"
"runtime/pprof"
"strconv"
"strings"
"time"
"github.com/ProtoconNet/mitum2/isaac"
isaacnetwork "github.com/ProtoconNet/mitum2/isaac/network"
"github.com/ProtoconNet/mitum2/network/quicstream"
"github.com/ProtoconNet/mitum2/util"
"github.com/ProtoconNet/mitum2/util/encoder"
"github.com/ProtoconNet/mitum2/util/hint"
"github.com/pkg/errors"
)
var (
PprofRequestHeaderHint = hint.MustNewHint("pprof-header-v0.0.1")
HandlerPrefixPprof = "pprof"
)
type dummyResponseWriter struct {
w io.Writer
}
func (dummyResponseWriter) Header() http.Header {
return http.Header{}
}
func (w dummyResponseWriter) Write(b []byte) (int, error) {
i, err := w.w.Write(b)
if err != nil {
return i, errors.WithStack(err)
}
return i, nil
}
func (dummyResponseWriter) WriteHeader(int) {
}
func NetworkHandlerPprofFunc(encs *encoder.Encoders) quicstream.Handler {
return func(remote net.Addr, r io.Reader, w io.Writer) error {
if !strings.HasPrefix(remote.String(), "127.") {
return errors.Errorf("not allowed")
}
e := util.StringErrorFunc("failed pprof")
ctx, cancel := context.WithTimeout(context.Background(), time.Second*2) //nolint:gomnd //...
defer cancel()
enc, hb, err := isaacnetwork.HandlerReadHead(ctx, encs, r)
if err != nil {
return e(err, "")
}
var header PprofRequestHeader
if err = encoder.Decode(enc, hb, &header); err != nil {
return e(err, "")
}
if err = header.IsValid(nil); err != nil {
return e(err, "")
}
req, err := http.NewRequest(http.MethodGet, "", nil)
if err != nil {
return e(err, "")
}
gc := "0"
if header.GC() {
gc = "1"
}
req.URL = &url.URL{
Host: "localhost",
RawQuery: url.Values{
"seconds": []string{strconv.FormatUint(header.Seconds(), 10)},
"gc": []string{gc},
}.Encode(),
}
res := isaacnetwork.NewResponseHeaderWithType(true, nil, isaac.NetworkResponseRawContentType)
if err := isaacnetwork.WriteResponse(w, res, nil, enc); err != nil {
return e(err, "")
}
netpprof.Handler(header.Label()).ServeHTTP(dummyResponseWriter{w: w}, req)
return nil
}
}
type PprofRequestHeader struct {
label string
isaacnetwork.BaseHeader
seconds uint64
gc bool
}
func NewPprofRequestHeader(label string, seconds uint64, gc bool) PprofRequestHeader {
return PprofRequestHeader{
BaseHeader: isaacnetwork.NewBaseHeader(PprofRequestHeaderHint),
label: label,
seconds: seconds,
gc: gc,
}
}
func (h PprofRequestHeader) IsValid([]byte) error {
e := util.ErrInvalid.Errorf("invalid PprofRequestHeader")
if err := h.BaseHinter.IsValid(PprofRequestHeaderHint.Type().Bytes()); err != nil {
return e.Wrap(err)
}
if len(h.label) < 1 {
return e.Errorf("empty profile label")
}
if h.seconds < 1 {
return e.Errorf("empty seconds")
}
if p := pprof.Lookup(h.label); p == nil {
return e.Errorf("unknown profile label, %q", h.label)
}
return nil
}
func (PprofRequestHeader) HandlerPrefix() string {
return HandlerPrefixPprof
}
func (h PprofRequestHeader) Label() string {
return h.label
}
func (h PprofRequestHeader) Seconds() uint64 {
return h.seconds
}
func (h PprofRequestHeader) GC() bool {
return h.gc
}
type pprofRequestHeaderJSONMarshaler struct {
Label string `json:"label"`
Seconds uint64 `json:"seconds"`
GC bool `json:"gc"`
}
func (h PprofRequestHeader) MarshalJSON() ([]byte, error) {
return util.MarshalJSON(struct {
pprofRequestHeaderJSONMarshaler
isaacnetwork.BaseHeader
}{
BaseHeader: h.BaseHeader,
pprofRequestHeaderJSONMarshaler: pprofRequestHeaderJSONMarshaler{
Label: h.label,
Seconds: h.seconds,
GC: h.gc,
},
})
}
func (h *PprofRequestHeader) UnmarshalJSON(b []byte) error {
e := util.StringErrorFunc("failed to unmarshal OperationHeader")
var u pprofRequestHeaderJSONMarshaler
if err := util.UnmarshalJSON(b, &u); err != nil {
return e(err, "")
}
var uh hint.BaseHinter
if err := util.UnmarshalJSON(b, &uh); err != nil {
return err
}
h.BaseHeader = isaacnetwork.NewBaseHeader(uh.Hint())
h.label = u.Label
h.seconds = u.Seconds
h.gc = u.GC
return nil
}