From 67e5d55814b878f2141a3768c9fce1b91c1e6fd4 Mon Sep 17 00:00:00 2001 From: kzkzzzz Date: Fri, 10 Jun 2022 18:06:07 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E8=B6=85=E6=97=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/{errorx => errorm}/error.go | 2 +- tool/gateway/gateway.go | 19 ++++++++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) rename common/{errorx => errorm}/error.go (98%) diff --git a/common/errorx/error.go b/common/errorm/error.go similarity index 98% rename from common/errorx/error.go rename to common/errorm/error.go index 20cf33d..de301b6 100644 --- a/common/errorx/error.go +++ b/common/errorm/error.go @@ -1,4 +1,4 @@ -package errorx +package errorm import ( "fmt" diff --git a/tool/gateway/gateway.go b/tool/gateway/gateway.go index 808ae3b..1d3e18e 100644 --- a/tool/gateway/gateway.go +++ b/tool/gateway/gateway.go @@ -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() -- 2.22.0