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