From a552d1c9f952142eebace911c10da138ae310b7b Mon Sep 17 00:00:00 2001 From: Aykhan Shahsuvarov Date: Fri, 18 Apr 2025 15:39:17 +0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A8=20Replace=20'time.AfterFunc'=20wit?= =?UTF-8?q?h=20'context.WithTimeout'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- requests/run.go | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/requests/run.go b/requests/run.go index 409ec1f..5905ec3 100644 --- a/requests/run.go +++ b/requests/run.go @@ -20,8 +20,11 @@ import ( // - ctx: The context for managing request lifecycle and cancellation. // - requestConfig: The configuration for the request, including timeout, proxies, and other settings. func Run(ctx context.Context, requestConfig *config.RequestConfig) (Responses, error) { - ctx, cancel := context.WithCancel(ctx) - defer cancel() + if requestConfig.Duration > 0 { + var cancel context.CancelFunc + ctx, cancel = context.WithTimeout(ctx, requestConfig.Duration) + defer cancel() + } clients := getClients( ctx, @@ -34,10 +37,6 @@ func Run(ctx context.Context, requestConfig *config.RequestConfig) (Responses, e return nil, types.ErrInterrupt } - if requestConfig.Duration > 0 { - time.AfterFunc(requestConfig.Duration, func() { cancel() }) - } - responses := releaseDodos(ctx, requestConfig, clients) if ctx.Err() != nil && len(responses) == 0 { return nil, types.ErrInterrupt