代码拉取完成,页面将自动刷新
package serverinterceptors
import (
"context"
"fmt"
"gitee.com/tylf2018/go-micro-framework/pkg/code"
"gitee.com/tylf2018/go-micro-framework/pkg/errors"
"runtime/debug"
"strings"
"sync"
"time"
"google.golang.org/grpc"
)
// UnaryTimeoutInterceptor returns a func that sets timeout to incoming unary requests. 返回一个函数 为一元请求 设置超时时间
func UnaryTimeoutInterceptor(timeout time.Duration) grpc.UnaryServerInterceptor {
return func(ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (any, error) {
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
var resp any // 2个 goroutine 操作全局变量 需要加锁 因为会并发地读写同一个全局变量
var err error
var lock sync.Mutex
done := make(chan struct{})
// create channel with buffer size 1 to avoid goroutine leak
panicChan := make(chan any, 1)
go func() {
defer func() {
if p := recover(); p != nil {
// attach call stack to avoid missing in different goroutine
panicChan <- fmt.Sprintf("%+v\n\n%s", p, strings.TrimSpace(string(debug.Stack())))
}
}()
lock.Lock()
defer lock.Unlock()
resp, err = handler(ctx, req)
close(done) // 关闭通道
}()
select {
case p := <-panicChan: // 错误 抛出
panic(p)
case <-done: // done 关闭 说明运行成功 执行以下逻辑
lock.Lock()
defer lock.Unlock()
return resp, err
case <-ctx.Done(): // 超时了 弹出错误信息
err := ctx.Err()
if err == context.Canceled {
err = errors.WithCode(code.ErrCanceledGrpc.Code(), err.Error())
//err = status.Error(codes.Canceled, err.Error())
} else if err == context.DeadlineExceeded {
err = errors.WithCode(code.ErrDeadlineExceededGrpc.Code(), err.Error())
//err = status.Error(codes.DeadlineExceeded, err.Error())
}
return nil, errors.ToGrpcError(err)
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。