initial import
This commit is contained in:
11
example/http/shedding/Dockerfile
Normal file
11
example/http/shedding/Dockerfile
Normal file
@@ -0,0 +1,11 @@
|
||||
FROM alpine
|
||||
|
||||
RUN apk update --no-cache
|
||||
RUN apk add --no-cache ca-certificates
|
||||
RUN apk add --no-cache tzdata
|
||||
ENV TZ Asia/Shanghai
|
||||
|
||||
WORKDIR /app
|
||||
COPY main /app/main
|
||||
|
||||
CMD ["./main"]
|
||||
63
example/http/shedding/main.go
Normal file
63
example/http/shedding/main.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"math"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"zero/core/httpx"
|
||||
"zero/core/logx"
|
||||
"zero/core/service"
|
||||
"zero/ngin"
|
||||
)
|
||||
|
||||
var (
|
||||
port = flag.Int("port", 3333, "the port to listen")
|
||||
timeout = flag.Int64("timeout", 1000, "timeout of milliseconds")
|
||||
cpu = flag.Int64("cpu", 500, "cpu threshold")
|
||||
)
|
||||
|
||||
type Request struct {
|
||||
User string `form:"user,optional"`
|
||||
}
|
||||
|
||||
func handle(w http.ResponseWriter, r *http.Request) {
|
||||
var req Request
|
||||
err := httpx.Parse(r, &req)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
var result float64
|
||||
for i := 0; i < 30000; i++ {
|
||||
result += math.Sqrt(float64(i))
|
||||
}
|
||||
time.Sleep(time.Millisecond * 5)
|
||||
httpx.OkJson(w, result)
|
||||
}
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
logx.Disable()
|
||||
engine := ngin.MustNewEngine(ngin.NgConf{
|
||||
ServiceConf: service.ServiceConf{
|
||||
Log: logx.LogConf{
|
||||
Mode: "console",
|
||||
},
|
||||
},
|
||||
Port: *port,
|
||||
Timeout: *timeout,
|
||||
CpuThreshold: *cpu,
|
||||
})
|
||||
defer engine.Stop()
|
||||
|
||||
engine.AddRoute(ngin.Route{
|
||||
Method: http.MethodGet,
|
||||
Path: "/",
|
||||
Handler: handle,
|
||||
})
|
||||
engine.Start()
|
||||
}
|
||||
Reference in New Issue
Block a user