From e361de2ef303549741298a64c23a1ef2d89ca7b1 Mon Sep 17 00:00:00 2001 From: Mzack9999 Date: Mon, 1 Mar 2021 05:18:31 +0100 Subject: [PATCH] Improvement in race condition - Fixes sync - Add support to dump request once (code is redundant) --- v2/pkg/protocols/http/request.go | 48 +++++++++++++++++++++++--------- 1 file changed, 35 insertions(+), 13 deletions(-) diff --git a/v2/pkg/protocols/http/request.go b/v2/pkg/protocols/http/request.go index aef544ad..b916a9c0 100644 --- a/v2/pkg/protocols/http/request.go +++ b/v2/pkg/protocols/http/request.go @@ -27,31 +27,53 @@ const defaultMaxWorkers = 150 // executeRaceRequest executes race condition request for a URL func (r *Request) executeRaceRequest(reqURL string, previous output.InternalEvent, callback protocols.OutputEventCallback) error { + var requests []*generatedRequest + + // Requests within race condition should be dumped once and the output prefilled to allow DSL language to work + // This will introduce a delay and will populate in hacky way the field "request" of outputEvent generator := r.newGenerator() - - maxWorkers := r.RaceNumberRequests - swg := sizedwaitgroup.New(maxWorkers) - - var requestErr error - mutex := &sync.Mutex{} - - request, err := generator.Make(reqURL, nil) + requestForDump, err := generator.Make(reqURL, nil) if err != nil { return err } + r.setCustomHeaders(requestForDump) + dumpedRequest, err := dump(requestForDump, reqURL) + if err != nil { + return err + } + if r.options.Options.Debug || r.options.Options.DebugRequests { + gologger.Info().Msgf("[%s] Dumped HTTP request for %s\n\n", r.options.TemplateID, reqURL) + gologger.Print().Msgf("%s", string(dumpedRequest)) + } + previous["request"] = string(dumpedRequest) + + // Pre-Generate requests for i := 0; i < r.RaceNumberRequests; i++ { - swg.Add() + generator := r.newGenerator() + request, err := generator.Make(reqURL, nil) + if err != nil { + return err + } + requests = append(requests, request) + } + + wg := sync.WaitGroup{} + var requestErr error + mutex := &sync.Mutex{} + for i := 0; i < r.RaceNumberRequests; i++ { + wg.Add(1) go func(httpRequest *generatedRequest) { + defer wg.Done() err := r.executeRequest(reqURL, httpRequest, previous, callback) mutex.Lock() if err != nil { requestErr = multierr.Append(requestErr, err) } mutex.Unlock() - swg.Done() - }(request) + }(requests[i]) } - swg.Wait() + wg.Wait() + return requestErr } @@ -217,7 +239,7 @@ func (r *Request) executeRequest(reqURL string, request *generatedRequest, previ err error ) - // For race conditions we can't dump the request body at this point as it's already waiting the open-gate event + // For race conditions we can't dump the request body at this point as it's already waiting the open-gate event, already handled with a similar code within the race function if !request.original.Race { dumpedRequest, err = dump(request, reqURL) if err != nil {