You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
package main
import (
"context""fmt""log""net/http""net/http/pprof""os""os/signal""strconv""sync""syscall""time"
)
var (
port=1339wait=3*time.Second
)
funcinit() {
httpMux:=http.NewServeMux()
httpMux.HandleFunc("/debug/pprof/", pprof.Index)
httpMux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
httpMux.HandleFunc("/debug/pprof/profile", pprof.Profile)
httpMux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
httpMux.HandleFunc("/debug/pprof/trace", pprof.Trace)
httpMux.HandleFunc("/check", func(w http.ResponseWriter, r*http.Request) {
w.Write([]byte(`{"alive": true}`))
})
gofunc() {
deferfunc() {
iferr:=recover(); err!=nil {
log.Println("PProf exec recover: ", err)
}
}()
log.Println("server PProf run on: ", port)
iferr:=http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", port), httpMux); err!=nil {
log.Println("PProf listen error: ", err)
}
}()
}
funcmain() {
ctx1, cancelFn:=context.WithCancel(context.Background())
demo(ctx1)
// graceful exitch:=make(chan os.Signal, 1)
// We'll accept graceful shutdowns when quit via SIGINT (Ctrl+C)// recv signal to exit main goroutine// window signalsignal.Notify(ch, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, syscall.SIGHUP)
// linux signal if you use linux on production,please use this code.// signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM, syscall.SIGUSR2, os.Interrupt, syscall.SIGHUP)// Block until we receive our signal.sig:=<-chcancelFn()
log.Println("exit signal: ", sig.String())
// Create a deadline to wait for.ctx, cancel:=context.WithTimeout(context.Background(), wait)
defercancel()
<-ctx.Done()
log.Println("services shutting down")
}
typewaitGroupstruct {
sync.WaitGroup
}
func (w*waitGroup) Wrap(fnfunc()) {
w.Add(1)
gofunc() {
fn()
w.Done()
}()
}
func (w*waitGroup) WrapRecover(fnfunc()) {
w.Add(1)
gofunc() {
deferfunc() {
iferr:=recover(); err!=nil {
log.Println("exec recover: ", err)
}
}()
fn()
w.Done()
}()
}
funcgetData(iint64) string {
returnstrconv.FormatInt(i, 10)
}
funcdemo(ctx context.Context) error {
gofunc() {
ticker:=time.NewTicker(500*time.Millisecond)
deferticker.Stop()
variint64varwg=&waitGroup{}
for {
select {
case<-ticker.C:
data:=getData(i)
i++wg.Wrap(func() {
log.Println("data: ", data)
})
case<-ctx.Done():
wg.Wait() // when the program exits, call the wait method here return
}
}
}()
returnnil
}
When I start this demo func in the web service, the ctx1 of this demo has not been cancelled. Here we use the wrap method as a callback function. The internal waitgroup is only doing Add(1), Done() operations, please pay attention to me The way it is used in this way. When I was doing pprof analysis, I found that sync block and blocking syscall are more serious indicators. Is there a problem when I use waitgroup like this?
In a concurrent scenario, will the mixed use of waitgroup and goroutine cause the program to be abnormal or hang up?
The text was updated successfully, but these errors were encountered:
Unlike many projects, the Go project does not use GitHub Issues for general discussion or asking questions. GitHub Issues are used for tracking bugs and proposals only.
Unlike many projects, the Go project does not use GitHub Issues for general discussion or asking questions. GitHub Issues are used for tracking bugs and proposals only.
What did you do?
main.go
When I start this demo func in the web service, the ctx1 of this demo has not been cancelled. Here we use the wrap method as a callback function. The internal waitgroup is only doing Add(1), Done() operations, please pay attention to me The way it is used in this way. When I was doing pprof analysis, I found that sync block and blocking syscall are more serious indicators. Is there a problem when I use waitgroup like this?
In a concurrent scenario, will the mixed use of waitgroup and goroutine cause the program to be abnormal or hang up?
The text was updated successfully, but these errors were encountered: