Ai
1 Star 0 Fork 1

m7s/plugin-debug

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
main.go 1.28 KB
一键复制 编辑 原始数据 按行查看 历史
banshan2 提交于 2022-04-23 22:09 +08:00 . 移动 debug 依赖到 monibuca
package debug
import (
"io"
"net/http"
"net/http/pprof"
"os"
"path/filepath"
"strings"
"go.uber.org/zap"
. "m7s.live/engine/v4"
)
type DebugConfig struct {
}
type WriteToFile struct {
header http.Header
io.Writer
}
func (w *WriteToFile) Header() http.Header {
// return w.w.Header()
return w.header
}
// func (w *WriteToFile) Write(p []byte) (int, error) {
// // w.w.Write(p)
// return w.Writer.Write(p)
// }
func (w *WriteToFile) WriteHeader(statusCode int) {
// w.w.WriteHeader(statusCode)
}
func (p *DebugConfig) OnEvent(event any) {
switch event.(type) {
case FirstConfig:
}
}
func (p *DebugConfig) Trace(w http.ResponseWriter, r *http.Request) {
pprof.Trace(w, r)
}
func (p *DebugConfig) Profile(w http.ResponseWriter, r *http.Request) {
file, err := os.OpenFile("cpu.profile", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
if err != nil {
plugin.Error("profile", zap.Error(err))
}
pprof.Profile(&WriteToFile{make(http.Header), file}, r)
file.Close()
if r.Host == "" {
r.Host = "localhost"
}
w.Write([]byte(strings.Join([]string{"go", "tool", "pprof", "-http :6060", ExecPath, filepath.Join(ExecDir, "cpu.profile")}, " ")))
}
func (p *DebugConfig) ServeHTTP(w http.ResponseWriter, r *http.Request) {
pprof.Index(w, r)
}
var plugin = InstallPlugin(&DebugConfig{})
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/m7s/plugin-debug.git
git@gitee.com:m7s/plugin-debug.git
m7s
plugin-debug
plugin-debug
v4

搜索帮助