Commit 67e5d558 authored by kzkzzzz's avatar kzkzzzz

feat: 增加超时

parent 1ae63cd0
package errorx
package errorm
import (
"fmt"
......
......@@ -15,6 +15,7 @@ import (
"net/http"
"os"
"sync"
"time"
)
var (
......@@ -31,6 +32,22 @@ type response struct {
Result interface{} `json:"result"`
}
// 超时控制
type timeoutWrapper struct {
client.Client
}
func NewTimeoutWrapper(c client.Client) client.Client {
return &timeoutWrapper{c}
}
func (t *timeoutWrapper) Call(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error {
// 2秒超时
timeoutCtx, cancel := context.WithTimeout(ctx, time.Second*2)
defer cancel()
return t.Client.Call(timeoutCtx, req, rsp, opts...)
}
func main() {
pool, _ = ants.NewPool(16) // 协程池, 并发查询GetService用
defer pool.Release()
......@@ -64,7 +81,7 @@ func start() {
service := micro.NewService(
micro.Client(grpc.NewClient()),
micro.Selector(newSelector),
//micro.WrapClient(NewLogWrapper),
micro.WrapClient(NewTimeoutWrapper),
)
microClient = service.Client()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment