小编lei*_*lin的帖子

golang - net/http/pprof不起作用

我有客户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)

处理程序在我的代码中不起作用.

go pprof

6
推荐指数
1
解决办法
2247
查看次数

这是否具有并发访问的线程安全性?

我有结构与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)安全吗?

concurrency synchronization thread-safety go

5
推荐指数
1
解决办法
158
查看次数

什么是chrome devtool时间轴框架概述矩形?

在此输入图像描述 (点击查看大图)

蓝色 - 加载,黄色 - 脚本,紫色 - 渲染,绿色 - 绘画

但是有很多轮廓矩形,那是什么?

javascript google-chrome-devtools

3
推荐指数
1
解决办法
792
查看次数

如果字段顺序不同,则Struct具有不同的大小

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)
  1. 结构AB具有相同的字段,但如果以不同的顺序指定,则会产生不同的大小.为什么?

  2. struct的大小C为零.系统分配了多少内存a := C{}

谢谢.

memory struct sizeof padding go

3
推荐指数
1
解决办法
223
查看次数