You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
72 lines
1.1 KiB
Go
72 lines
1.1 KiB
Go
4 years ago
|
package main
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"net/http"
|
||
|
"runtime"
|
||
|
"time"
|
||
|
|
||
|
"zero/core/fx"
|
||
|
"zero/core/logx"
|
||
|
"zero/core/service"
|
||
|
"zero/core/stat"
|
||
|
"zero/ngin"
|
||
|
)
|
||
|
|
||
|
const duration = time.Millisecond
|
||
|
|
||
|
func main() {
|
||
|
go func() {
|
||
|
ticker := time.NewTicker(time.Second)
|
||
|
defer ticker.Stop()
|
||
|
for range ticker.C {
|
||
|
fmt.Printf("cpu: %d\n", stat.CpuUsage())
|
||
|
}
|
||
|
}()
|
||
|
|
||
|
logx.Disable()
|
||
|
engine := ngin.MustNewEngine(ngin.NgConf{
|
||
|
ServiceConf: service.ServiceConf{
|
||
|
Log: logx.LogConf{
|
||
|
Mode: "console",
|
||
|
},
|
||
|
},
|
||
|
Host: "0.0.0.0",
|
||
|
Port: 3333,
|
||
|
CpuThreshold: 800,
|
||
|
})
|
||
|
defer engine.Stop()
|
||
|
engine.AddRoute(ngin.Route{
|
||
|
Method: http.MethodGet,
|
||
|
Path: "/",
|
||
|
Handler: func(w http.ResponseWriter, r *http.Request) {
|
||
|
if err := fx.DoWithTimeout(func() error {
|
||
|
job(duration)
|
||
|
return nil
|
||
|
}, time.Millisecond*100); err != nil {
|
||
|
w.WriteHeader(http.StatusServiceUnavailable)
|
||
|
}
|
||
|
},
|
||
|
})
|
||
|
engine.Start()
|
||
|
}
|
||
|
|
||
|
func job(duration time.Duration) {
|
||
|
done := make(chan int)
|
||
|
|
||
|
for i := 0; i < runtime.NumCPU(); i++ {
|
||
|
go func() {
|
||
|
for {
|
||
|
select {
|
||
|
case <-done:
|
||
|
return
|
||
|
default:
|
||
|
}
|
||
|
}
|
||
|
}()
|
||
|
}
|
||
|
|
||
|
time.Sleep(duration)
|
||
|
close(done)
|
||
|
}
|