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 20cf33d02812cba67f722682626ca9f9cca72d27..de301b69434377774ff7610f232f3a8226c247a3 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 808ae3b76b3de00bf9b7ad8fc066411b06a838a9..1d3e18ebd1c56d5f97be03bce80b512e1859932c 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()