From 1cd0f10b2a267552217546c55d1feb383e02f7f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Mon, 27 Jun 2022 11:54:25 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=BB=E5=8A=A1=E6=94=AF=E6=8C=81=E8=B6=85?= =?UTF-8?q?=E6=97=B6=E8=87=AA=E5=8A=A8=E5=8F=96=E6=B6=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- task.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/task.go b/task.go index f9b0e28..f7c34e8 100644 --- a/task.go +++ b/task.go @@ -82,8 +82,24 @@ func (d *dispatch) Run(ctx context.Context, cfg *Config) *Result { var ( taskInstance ITask exist bool + cancelFunc context.CancelFunc ) + if nil == ctx { + if cfg.Timeout > 0 { + ctx, cancelFunc = context.WithCancel(context.Background()) + } else { + ctx = context.Background() + } + } else { + ctx, cancelFunc = context.WithCancel(ctx) + } + + if nil != cancelFunc { + // 带了超时时间 + cancelFunc() + } + result := &Result{ StartTime: time.Now().UnixNano(), FinishTime: 0,