我有客户http服务:
s := &http.Server{
Addr: config.Port,
Handler: Controller.Log(http.DefaultServeMux),
ReadTimeout: 3 * time.Second,
WriteTimeout: 3 * time.Second,
}
http.HandleFunc("/exapmle/router/", exampleFunc)
err := s.ListenAndServe()
if err != nil {
log.Critical(err)
os.Exit(1)
}
Run Code Online (Sandbox Code Playgroud)
它不起作用:
go tool pprof http://localhost:8201/debug/pprof/profile
Run Code Online (Sandbox Code Playgroud)
返回错误:
Failed to fetch http://localhost:8201/debug/pprof/profile?seconds=30
Run Code Online (Sandbox Code Playgroud)
谢谢.
编辑:我认为问题是我重写默认的http服务器,net/http/pprof包注入http处理程序:
func init() {
http.Handle("/debug/pprof/", http.HandlerFunc(Index))
http.Handle("/debug/pprof/cmdline", http.HandlerFunc(Cmdline))
http.Handle("/debug/pprof/profile", http.HandlerFunc(Profile))
http.Handle("/debug/pprof/symbol", http.HandlerFunc(Symbol))
}
Run Code Online (Sandbox Code Playgroud)
处理程序在我的代码中不起作用.
我有结构与count属性需要线程安全访问.
我知道它可以用sync.Mutex或完成sync.RWMutex.但我不确定它是这样的:
type Status struct {
count uint32
attr1 string
attr2 string
}
func (s *Status) Get() uint32 {
return atomic.LoadUint32(&s.count)
}
func (s *Status) Add(n uint32) {
atomic.AddUint32(&s.count, n)
}
func (s *Status) Reset(n uint32) {
atomic.StoreUint32(&s.count, n)
}
Run Code Online (Sandbox Code Playgroud)
谢谢.
编辑:
我很困惑,直接访问字段s.count是不安全的.但是atomic.LoadUint32(&s.count)安全吗?
package main
import (
"fmt"
"unsafe"
)
type A struct {
a bool
b int64
c int
}
type B struct {
b int64
a bool
c int
}
type C struct {
}
func main() {
// output 24
fmt.Println(unsafe.Sizeof(A{}))
// output 16
fmt.Println(unsafe.Sizeof(B{}))
// output 0
fmt.Println(unsafe.Sizeof(C{}))
}
Run Code Online (Sandbox Code Playgroud)
结构A并B具有相同的字段,但如果以不同的顺序指定,则会产生不同的大小.为什么?
struct的大小C为零.系统分配了多少内存a := C{}?
谢谢.