From ca0d42dc145380d41ba0c67cf7837c14a90bf9d4 Mon Sep 17 00:00:00 2001 From: Ran Mishael Date: Tue, 7 Jan 2025 11:05:30 +0100 Subject: [PATCH] feat: PRT - change populator behavior to be completely concurrent --- ecosystem/cache_populator/command.go | 103 +++++++++++++-------------- 1 file changed, 50 insertions(+), 53 deletions(-) diff --git a/ecosystem/cache_populator/command.go b/ecosystem/cache_populator/command.go index 9427525a54..422f1c1fed 100644 --- a/ecosystem/cache_populator/command.go +++ b/ecosystem/cache_populator/command.go @@ -67,65 +67,62 @@ func executeRequest(config CacheConfig, request Request) { client := &http.Client{} ticker := time.NewTicker(config.Interval) defer ticker.Stop() + for range ticker.C { + go func() { + // Create context with timeout + ctx, cancel := context.WithTimeout(context.Background(), config.ContextTimeout) + + // Generate new GUID for this request cycle + guid := utils.GenerateUniqueIdentifier() + + // Create request based on type + var httpReq *http.Request + var err error + + switch request.Type { + case "GET": + httpReq, err = http.NewRequestWithContext(ctx, http.MethodGet, + config.ServerAddress+request.Path, nil) + case "POST": + httpReq, err = http.NewRequestWithContext(ctx, http.MethodPost, + config.ServerAddress+request.Path, bytes.NewBufferString(request.Body)) + default: + utils.LavaFormatError("unsupported request type", fmt.Errorf("type: %s", request.Type), utils.Attribute{Key: "guid", Value: guid}) + cancel() + return + } - for { - // Create context with timeout - ctx, cancel := context.WithTimeout(context.Background(), config.ContextTimeout) - - // Generate new GUID for this request cycle - guid := utils.GenerateUniqueIdentifier() - - // Create request based on type - var httpReq *http.Request - var err error - - switch request.Type { - case "GET": - httpReq, err = http.NewRequestWithContext(ctx, http.MethodGet, - config.ServerAddress+request.Path, nil) - case "POST": - httpReq, err = http.NewRequestWithContext(ctx, http.MethodPost, - config.ServerAddress+request.Path, bytes.NewBufferString(request.Body)) - default: - utils.LavaFormatError("unsupported request type", fmt.Errorf("type: %s", request.Type), utils.Attribute{Key: "guid", Value: guid}) - cancel() - continue - } + if err != nil { + utils.LavaFormatError("failed to create request", err, utils.Attribute{Key: "guid", Value: guid}) + cancel() + return + } - if err != nil { - utils.LavaFormatError("failed to create request", err, utils.Attribute{Key: "guid", Value: guid}) - cancel() - continue - } + // Add headers to the request + for key, value := range request.Headers { + httpReq.Header.Set(key, value) + } - // Add headers to the request - for key, value := range request.Headers { - httpReq.Header.Set(key, value) - } + // Execute request + utils.LavaFormatDebug("request ", utils.Attribute{Key: "path", Value: request.Path}, utils.Attribute{Key: "body", Value: request.Body}, utils.Attribute{Key: "guid", Value: guid}) - // Execute request - utils.LavaFormatDebug("request ", utils.Attribute{Key: "path", Value: request.Path}, utils.Attribute{Key: "body", Value: request.Body}, utils.Attribute{Key: "guid", Value: guid}) + resp, err := client.Do(httpReq) + if err != nil { + utils.LavaFormatError("failed to execute request", err, utils.Attribute{Key: "guid", Value: guid}) + cancel() + // Wait for next tick before retrying + return + } + + if resp.StatusCode >= 400 { + utils.LavaFormatError("request failed", + fmt.Errorf("status code: %d, path: %s", resp.StatusCode, request.Path), utils.Attribute{Key: "guid", Value: guid}) + } - resp, err := client.Do(httpReq) - if err != nil { - utils.LavaFormatError("failed to execute request", err, utils.Attribute{Key: "guid", Value: guid}) + resp.Body.Close() + utils.LavaFormatDebug("response", utils.Attribute{Key: "status_code", Value: resp.StatusCode}, utils.Attribute{Key: "path", Value: request.Path}, utils.Attribute{Key: "guid", Value: guid}, utils.Attribute{Key: "body", Value: request.Body}) cancel() - // Wait for next tick before retrying - <-ticker.C - continue - } - - if resp.StatusCode >= 400 { - utils.LavaFormatError("request failed", - fmt.Errorf("status code: %d, path: %s", resp.StatusCode, request.Path), utils.Attribute{Key: "guid", Value: guid}) - } - - resp.Body.Close() - utils.LavaFormatDebug("response", utils.Attribute{Key: "status_code", Value: resp.StatusCode}, utils.Attribute{Key: "path", Value: request.Path}, utils.Attribute{Key: "guid", Value: guid}, utils.Attribute{Key: "body", Value: request.Body}) - cancel() - - // Wait for next tick before making the next request - <-ticker.C + }() } }