From 2c2ca90a33e6fdc146ee52e8bf4864a19dbdda32 Mon Sep 17 00:00:00 2001 From: Sunny Rajesh Dubey Date: Wed, 10 Dec 2025 19:26:12 +0530 Subject: [PATCH] reuse HTTP client in task service to enable connection pooling --- internal/service/task_service.go | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/internal/service/task_service.go b/internal/service/task_service.go index 43033f5bf..21e8bdd2d 100644 --- a/internal/service/task_service.go +++ b/internal/service/task_service.go @@ -254,6 +254,22 @@ func (s *TaskService) ExecutePendingTasks(ctx context.Context, maxTasks int) err tracing.AddAttribute(ctx, "execution_mode", "http") + // Create HTTP client with timeout (shared across all tasks for connection reuse) + httpClient := &http.Client{ + Timeout: 53 * time.Second, // 53 seconds timeout as requested + Transport: &http.Transport{ + MaxIdleConns: 100, + MaxIdleConnsPerHost: 100, + IdleConnTimeout: 90 * time.Second, + TLSClientConfig: &tls.Config{ + InsecureSkipVerify: true, // Skip TLS verification + }, + }, + } + + // Wrap with OpenCensus tracing + httpClient = tracing.WrapHTTPClient(httpClient) + // Use a wait group to wait for all HTTP requests to complete var wg sync.WaitGroup @@ -293,19 +309,6 @@ func (s *TaskService) ExecutePendingTasks(ctx context.Context, maxTasks int) err return } - // Create HTTP client with timeout - httpClient := &http.Client{ - Timeout: 53 * time.Second, // 53 seconds timeout as requested - Transport: &http.Transport{ - TLSClientConfig: &tls.Config{ - InsecureSkipVerify: true, // Skip TLS verification - }, - }, - } - - // Wrap with OpenCensus tracing - httpClient = tracing.WrapHTTPClient(httpClient) - // Create request with tracing context endpoint := fmt.Sprintf("%s/api/tasks.execute", s.apiEndpoint) req, err := http.NewRequest(http.MethodPost, endpoint, bytes.NewBuffer(reqBody))