2 Star 0 Fork 339

hxchjm / go-zero

forked from kevwan / go-zero 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
server.go 1.20 KB
一键复制 编辑 原始数据 按行查看 历史
kevwan 提交于 2020-08-08 16:40 . update package reference
package main
import (
"net/http"
"runtime"
"time"
"github.com/tal-tech/go-zero/core/logx"
"github.com/tal-tech/go-zero/core/service"
"github.com/tal-tech/go-zero/core/stat"
"github.com/tal-tech/go-zero/core/syncx"
"github.com/tal-tech/go-zero/rest"
)
func main() {
logx.Disable()
stat.SetReporter(nil)
server := rest.MustNewServer(rest.RestConf{
ServiceConf: service.ServiceConf{
Name: "breaker",
Log: logx.LogConf{
Mode: "console",
},
},
Host: "0.0.0.0",
Port: 8080,
MaxConns: 1000,
Timeout: 3000,
})
latch := syncx.NewLimit(10)
server.AddRoute(rest.Route{
Method: http.MethodGet,
Path: "/heavy",
Handler: func(w http.ResponseWriter, r *http.Request) {
if latch.TryBorrow() {
defer latch.Return()
runtime.LockOSThread()
defer runtime.UnlockOSThread()
begin := time.Now()
for {
if time.Now().Sub(begin) > time.Millisecond*50 {
break
}
}
} else {
w.WriteHeader(http.StatusInternalServerError)
}
},
})
server.AddRoute(rest.Route{
Method: http.MethodGet,
Path: "/good",
Handler: func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
},
})
defer server.Stop()
server.Start()
}
Go
1
https://gitee.com/hxchjm/go-zero.git
git@gitee.com:hxchjm/go-zero.git
hxchjm
go-zero
go-zero
3eb88c4e4bf1

搜索帮助

53164aa7 5694891 3bd8fe86 5694891