1 Star 2 Fork 0

阿债/fiber-u8l

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
timeout.go 808 Bytes
一键复制 编辑 原始数据 按行查看 历史
阿债 提交于 2020-10-09 13:38 . 更新原版fiber到v2.0.6
package timeout
import (
"fmt"
"sync"
"time"
"gitee.com/azhai/fiber-u8l"
)
var once sync.Once
// New wraps a handler and aborts the process of the handler if the timeout is reached
func New(handler fiber.Handler, timeout time.Duration) fiber.Handler {
once.Do(func() {
fmt.Println("[Warning] timeout contains data race issues, not ready for production!")
})
if timeout <= 0 {
return handler
}
// logic is from fasthttp.TimeoutWithCodeHandler https://github.com/valyala/fasthttp/blob/master/server.go#L418
return func(ctx *fiber.Ctx) error {
ch := make(chan struct{}, 1)
go func() {
defer func() {
_ = recover()
}()
_ = handler(ctx)
ch <- struct{}{}
}()
select {
case <-ch:
case <-time.After(timeout):
return fiber.ErrRequestTimeout
}
return nil
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/azhai/fiber-u8l.git
git@gitee.com:azhai/fiber-u8l.git
azhai
fiber-u8l
fiber-u8l
v1.21.2

搜索帮助

0d507c66 1850385 C8b1a773 1850385